Materialized view replication provides the flexibility to build data sets to meet the needs of your users and front-end applications, while still meeting the requirements of your security configuration. This chapter describes how to manage materialized view sites with the replication management API.

Refreshing Materialized Views

Refreshing a materialized view synchronizes the data in the materialized view's master(s) and the data in the materialized view. You can either refresh all of the materialized views in a refresh group at once, or you can refresh materialized views individually. If you have applications that depend on more than one materialized view at a materialized view site, then Oracle Corporation recommends using refresh groups so that the data is transactionally consistent in all of the materialized views used by the application.

Changing a Materialized View Group's Master Site

To change the master site of a materialized view group at a level 1 materialized view site to another master site, call the SWITCH_MVIEW_MASTER procedure in the DBMS_REPCAT package, as shown in the following example:

In this example, the master site for the hr_repg replication group is changed to the orc3.world master site. You must call this procedure at the materialized view site whose master site you want to change. The new database must be a master site in the replication environment. When you call this procedure, Oracle uses the new master to perform a full refresh of each materialized view in the local materialized view group. Make sure you have set up the materialized view site to use the new master site before you run the SWITCH_MVIEW_MASTER procedure.

The entries in the SYS.SLOG$ table at the old master site for the switched materialized view are not removed. As a result, the materialized view log (MLOG$ table) of the switched updatable materialized view at the old master site has the potential to grow indefinitely, unless you purge it by calling DBMS_MVIEW.PURGE_LOG.

Note:

You cannot switch the master of materialized views that are based on other materialized views (level 2 and greater materialized views). Such a materialized view must be dropped and re-created if you want to base it on a different master.

Dropping Materialized View Groups and Objects

You may need to drop replication activity at a materialized view site for a number of reasons. Perhaps the data requirements have changed or an employee has left the company. In any case, as a DBA you will need to drop the replication support for the target materialized view site.

Dropping a Materialized View Groups Created with a Deployment Template

If a materialized view group was created with a deployment template, then, before you drop the materialized view group at the remote materialized view site, you need to execute the DROP_SITE_INSTANTIATION procedure at the target master site of the materialized view group. In addition to removing the metadata relating to the materialized view group, this procedure also removes the related deployment template data regarding this site.

The DROP_SITE_INSTANTIATION procedure has a public and a private version. The public version allows the owner of the materialized view group to drop the materialized view site, while the private version allows the replication administrator to drop a materialized view site on behalf of the materialized view group owner.

Using the Public Version of DROP_SITE_INSTANTIATION

Meet the following requirements to complete these actions:

Executed As:

Materialized View Group Owner at Master Site

Materialized View Administrator at Materialized View Site

Executed At:

Master Site for Target Materialized View Site

Materialized View Site

Replication Status: Normal

Complete the following steps to drop a materialized view group created with a deployment template.

Note:

If you are viewing this document online, then you can copy the text from the "BEGINNING OF SCRIPT" line on this page to the "END OF SCRIPT" line into a text editor and then edit the text to create a script for your environment.

Step 4 Drop the materialized view group.

If you want to physically remove the contents of the materialized view group from the materialized view database, then be sure that you specify TRUE for the drop_contents parameter.

Step 5 Remove the refresh group.

*/
BEGIN
DBMS_REFRESH.DESTROY ( name => 'hr_refg');
END;
/
SET ECHO OFF
SPOOL OFF
/************************* END OF SCRIPT **********************************/

Using the Private Version of DROP_SITE_INSTANTIATION

The following steps are to be performed by the replication administrator on behalf of the materialized view group owner. Meet the following requirements to complete these actions:

Executed As:

Replication Administrator at Master Site

Materialized View Administrator at Materialized View Site

Executed At:

Master Site for Target Materialized View Site

Materialized View Site

Replication Status: Normal

Complete the following steps to drop a materialized view group created with a deployment template.

Note:

If you are viewing this document online, then you can copy the text from the "BEGINNING OF SCRIPT" line on this page to the "END OF SCRIPT" line into a text editor and then edit the text to create a script for your environment.

Step 4 Drop the materialized view group.

If you want to physically remove the contents of the materialized view group from the materialized view database, then be sure that you specify TRUE for the drop_contents parameter.

Step 5 Remove the refresh group.

*/
BEGIN
DBMS_REFRESH.DESTROY ( name => 'hr_refg');
END;
/
SET ECHO OFF
SPOOL OFF
/************************* END OF SCRIPT **********************************/

Dropping a Materialized View Group or Objects Created Manually

The most secure method of removing replication support for a materialized view site is to physically drop the replicated objects or groups at the materialized view site. The following two sections describe how to drop these objects and groups while connected to the materialized view group.

Ideally, these procedures should be executed while the materialized view is connected to its target master site or master materialized view site. A connection ensures that any related metadata at the master site or master materialized view site is removed. If a connection to the master site or master materialized view site is not possible, then be sure to complete the procedure described in "Cleaning Up a Master Site or Master Materialized View Site" to manually remove the related metadata.

Dropping a Materialized View Group Created Manually

When it becomes necessary to remove a materialized view group from a materialized view site, use the DROP_MVIEW_REPGROUP procedure to drop a materialized view group. When you execute this procedure and are connected to the target master site or master materialized view site, the metadata for the target materialized view group at the master site or master materialized view site is removed. If you cannot connect, then see "Cleaning Up a Master Site or Master Materialized View Site" for more information.

Meet the following requirements to complete these actions:

Executed As: Materialized View Administrator

Executed At: Remote Materialized View Site

Replication Status: N/A

Complete the following steps to drop a materialized view group at a materialized view site:

Step 1 Connect to the materialized view site as the materialized view administrator.

Step 2 Drop the materialized view group.

If you want to physically remove the contents of the materialized view group from the materialized view database, then be sure that you specify TRUE for the drop_contents parameter.

Dropping Objects at a Materialized View Site

When it becomes necessary to remove an individual materialized view from a materialized view site, use the DROP_MVIEW_REPOBJECT procedure API to drop a materialized view. When you execute this procedure and are connected to the target master site or master materialized view site, the metadata for the target materialized view at the master site or master materialized view site is removed. If you cannot connect, then see "Cleaning Up a Master Site or Master Materialized View Site" for more information.

Meet the following requirements to complete these actions:

Executed As: Materialized View Administrator

Executed At: Remote Materialized View Site

Replication Status: N/A

Complete the following steps to drop an individual materialized view at a materialized view site.

Step 1 Connect to the materialized view site as the materialized view administrator.

Step 2 Drop the materialized view.

If you want to physically remove the contents of the materialized view from the materialized view database, then be sure that you specify TRUE for the drop_contents parameter.

Cleaning Up a Master Site or Master Materialized View Site

If you are unable to drop a materialized view group or materialized view object while connected to the target master site or master materialized view site, then you must remove the related metadata at the master site or master materialized view site manually. Cleaning up the metadata also ensures that you are not needlessly maintaining master table or master materialized view changes to a materialized view log. The following sections describe how to clean up your master site or master materialized view site after dropping a materialized view group or object.

Cleaning Up After Dropping a Materialized View Group

If you have executed the steps described in "Dropping a Materialized View Group Created Manually" and were not connected to the master site or master materialized view site, then you are encouraged to complete the following steps to clean up the target master site or master materialized view site.

Meet the following requirements to complete these actions:

Executed As: Replication Administrator

Executed At: Master Site or Master Materialized View Site for Target Materialized View Site

Replication Status: Normal

Complete the following steps to clean up a master site or master materialized view site after dropping a materialized view group:

Note:

If you are viewing this document online, then you can copy the text from the "BEGINNING OF SCRIPT" line on this page to the "END OF SCRIPT" line into a text editor and then edit the text to create a script for your environment.

Step 2 Unregister the materialized view groups.

Step 3 Purge the materialized view logs of the entries that were marked for the target materialized views. Execute the PURGE_MVIEW_FROM_LOG procedure for each materialized view that was in the materialized view groups you unregistered in Step 2.

Note:

If for some reason unregistering the materialized view group fails, then you should still complete this step.

Cleaning Up Individual Materialized View Support

If you have executed the steps described in "Dropping Objects at a Materialized View Site" and were not connected to the master site or master materialized view site, then you are encouraged to complete the following steps to clean up the target master site or master materialized view site.

Meet the following requirements to complete these actions:

Executed As: Replication Administrator

Executed At: Master Site or Master Materialized View Site for Target Materialized View Site

Replication Status: Normal

Complete the following steps to clean up a master site or master materialized view site after dropping an individual materialized view.

Note:

If you are viewing this document online, then you can copy the text from the "BEGINNING OF SCRIPT" line on this page to the "END OF SCRIPT" line into a text editor and then edit the text to create a script for your environment.

Altering Materialized View Logs

After creating a materialized view log, you can alter its storage parameters and support for corresponding materialized views. The following sections explain more about altering materialized view logs. Only the following users can alter a materialized view log:

The owner of the master table or master materialized view

A user with the SELECT privilege for the master table or master materialized view and ALTER privilege on the MLOG$_master_name, where master_name is the name of the master for the materialized view log. For example, if the master table is employees, then the materialized view log table name is MLOG$_employees.

Altering Materialized View Log Storage Parameters

To alter a materialized view log's storage parameters, use the ALTERMATERIALIZEDVIEWLOG statement. For example, the following statement alters a materialized view log on the employees table in the hr schema:

ALTER MATERIALIZED VIEW LOG ON hr.employees
PCTFREE 25
PCTUSED 40;

Altering a Materialized View Log to Add Columns

To add new columns to a materialized view log, use the SQL statement ALTERMATERIALIZEDVIEWLOG. For example, the following statement alters a materialized view log on the customers table in the sales schema:

Managing Materialized View Log Space

Oracle automatically tracks which rows in a materialized view log have been used during the refreshes of materialized views, and purges these rows from the log so that the log does not grow endlessly. Because multiple simple materialized views can use the same materialized view log, rows already used to refresh one materialized view may still be needed to refresh another materialized view. Oracle does not delete rows from the log until all materialized views have used them.

For example, suppose two materialized views were created against the customers table in a master site. Oracle refreshes the customers materialized view at the spdb1 database. However, the server that manages the master table and associated materialized view log does not purge the materialized view log rows used during the refresh of this materialized view until the customers materialized view at the spdb2 database also refreshes using these rows.

Because Oracle must wait for all dependent materialized views to refresh before purging rows from a materialized view log, unwanted situations can occur that cause a materialized view log to grow indefinitely when multiple materialized views are based on the same master table or master materialized view. For example, such situations can occur when more than one materialized view is based on a master table or master materialized view and one of the following conditions is true:

One materialized view is not configured for automatic refreshes and has not been manually refreshed for a long time.

One materialized view has an infrequent refresh interval, such as every year (365 days).

A network failure has prevented an automatic refresh of one or more of the materialized views based on the master table or master materialized view.

A network or site failure has prevented a master table or master materialized view from becoming aware that a materialized view has been dropped.

Note:

If you purge or TRUNCATE a materialized view log before a materialized view has refreshed the changes that were deleted, then the materialized view must perform a complete refresh.

Purging Rows from a Materialized View Log

Always try to keep a materialized view log as small as possible to minimize the database space that it uses. To remove rows from a materialized view log and make space for newer log records, you can perform one of the following actions:

Refresh the materialized views associated with the log so that Oracle can purge rows from the materialized view log.

Manually purge records in the log by deleting rows required only by the nth least recently refreshed materialized views.

To manually purge rows from a materialized view log, execute the PURGE_LOG procedure of the DBMS_MVIEW package at the database that contains the log. For example, to purge entries from the materialized view log of the customers table that are necessary only for the least recently refreshed materialized view, execute the following procedure:

Only the owner of a materialized view log or a user with the EXECUTE privilege for the DBMS_MVIEW package can purge rows from the materialized view log by executing the PURGE_LOG procedure.

Truncating a Materialized View Log

If a materialized view log grows and allocates many extents, then purging the log of rows does not reduce the amount of space allocated for the log. In such cases, you should truncate the materialized view log. Only the owner of a materialized view log or a user with the DELETEANYTABLE system privilege can truncate a materialized view log.

To reduce the space allocated for a materialized view log by truncating it, complete the following steps:

Step 1 Acquire an exclusive lock on the master table or master materialized view to prevent updates from occurring during the following process. For example, issue a statement similar to the following:

LOCK TABLE hr.employees IN EXCLUSIVE MODE;

Step 2 Using a second database session, copy the rows in the materialized view log (in other words, the MLOG$ base table) to a temporary table. For example, issue a statement similar to the following:

CREATE TABLE hr.templog AS SELECT * FROM hr.MLOG$_employees;

Step 3 Using the second session, truncate the log using the SQL statement TRUNCATE. For example, issue a statement similar to the following:

TRUNCATE hr.MLOG$_employees;

Step 4 Using the second session, reinsert the old rows so that you do not have to perform a complete refresh of the dependent materialized views. For example, issue statements similar to the following:

Step 5 Using the first session, release the exclusive lock on the master table or master materialized view by performing a rollback:

ROLLBACK;

Note:

Any changes made to the master table or master materialized view between the time you copy the rows to a new location and when you truncate the log do not appear until after you perform a complete refresh.

To improve performance and optimize disk use, you can periodically reorganize master tables. This section describes how to reorganize a master and preserve the fast refresh capability of associated materialized views.

Note:

These sections do not discuss online redefinition of tables. Online redefinition is not allowed on master tables with materialized view logs, master materialized views, or materialized views. Online redefinition is allowed only on master tables that do not have materialized view logs. See the Oracle9i Database Administrator's Guide for more information about online redefinition of tables.

Reorganization Notification

When you reorganize a table, any ROWID information of the materialized view log must be invalidated. Oracle detects a table reorganization automatically only if the table is truncated as part of the reorganization.

If the table is not truncated, then Oracle must be notified of the table reorganization. To support table reorganizations, two procedures in the DBMS_MVIEW package, BEGIN_TABLE_REORGANIZATION and END_TABLE_REORGANIZATION, notify Oracle that the specified table has been reorganized. The procedures perform clean-up operations, verify the integrity of the logs and triggers that the fast refresh mechanism needs, and invalidate the ROWID information in the table's materialized view log. The inputs are the owner and name of the master to be reorganized. There is no output.

Truncating Masters

When a table is truncated, its materialized view log is also truncated. However, for primary key materialized views, you can preserve the materialized view log, allowing fast refreshes to continue. Although the information stored in a materialized view log is preserved, the materialized view log becomes invalid with respect to rowids when the master is truncated. The rowid information in the materialized view log will seem to be newly created and cannot be used by rowid materialized views for fast refresh.

The PRESERVEMATERIALIZEDVIEWLOG option is the default. Therefore, if you specify the PRESERVEMATERIALIZEDVIEWLOG option or no option, then the information in the master's materialized view log is preserved, but current rowid materialized views can use the log for a fast refresh only after a complete refresh has been performed.

The following statement truncates the materialized view log along with the master table:

TRUNCATE TABLE hr.employees PURGE MATERIALIZED VIEW LOG;

Methods of Reorganizing a Database Table

Oracle provides four table reorganization methods that preserve the capability for fast refresh. These appear in the following sections. Other reorganization methods require an initial complete refresh to enable subsequent fast refreshes.

Note:

Do not use Direct Loader during a reorganization of a master. Direct Loader can cause reordering of the columns, which could invalidate the log information used in subquery and LOB materialized views.

Dropping a Materialized View Log

You can delete a materialized view log regardless of its master or any existing materialized views. For example, you might decide to drop a materialized view log if one of the following conditions is true:

All materialized views of a master have been dropped.

All materialized views of a master are to be completely refreshed, not fast refreshed.

Here, a master can be a master table or a master materialized view. To delete a materialized view log, execute the DROPMATERIALIZEDVIEWLOG statement in SQL*Plus. For example, the following statement deletes the materialized view log for a table named customers in the sales schema:

DROP MATERIALIZED VIEW LOG ON hr.employees;

Only the owner of the master or a user with the DROPANYTABLE system privilege can drop a materialized view log.

Performing an Offline Instantiation of a Materialized View Site Using Export/Import

Expanding established replication environments can cause network traffic when you add a new materialized view site to your replication environment. This is caused by propagating the entire contents of the table or materialized view through the network to the new replicated site.

To minimize such network traffic, you can expand your replication environment by using the offline instantiation procedure. Offline instantiation takes advantage of Oracle's Export and Import utilities, which allow you to create an export file and transfer the data to the new site through another storage medium, such as CD-ROM, tape, and so on.

For the same reasons that you might want to perform an offline instantiation of a master site, you may also want to create a new materialized view group at a materialized view site using the offline instantiation process. In some cases, it is even more useful for materialized views considering that the target computer could very well be a laptop using a modem connection.

The following script performs an offline instantiation for a new materialized view group. Meet the following requirements to complete these actions:

Executed As:

Replication Administrator at Master Site

Materialized View Administrator at New Materialized View Site

Executed At:

Master Site for Target Materialized View Site

New Materialized View Site

Replication Status: Normal

Complete the following steps to perform an offline instantiation of a materialized view site using export/import.

Note:

If you are viewing this document online, then you can copy the text from the "BEGINNING OF SCRIPT" line on this page to the "END OF SCRIPT" line into a text editor and then edit the text to create a script for your environment.

Step 4 Create temporary materialized views at the master site. These materialized views contain the data that you transfer to your new materialized view site using the export file.

Note:

If you added any of the conflict resolution routines described in Chapter 6, "Configure Conflict Resolution", then you may have additional columns in your tables. Be certain to include these additional columns in the SELECT statements below. Updatable materialized views require that you explicitly select all columns in the master table. So, do not use SELECT* statements.

Step 6 Connect to the new materialized view site as SYSTEM user.

*/
CONNECT system/manager@mview.world
/*

Step 7 Create necessary schema and database link at the materialized view site, if they do not exist.

Before you perform the offline instantiation of your materialized views, create the schema that will contain the materialized views at the new materialized view site and the database link from the materialized view site to the master site. The materialized views must be in the same schema that contains the master objects at the master site.

Step 10 Prepare the materialized view site for offline instantiation.

The DBMS_OFFLINE_SNAPSHOT.BEGIN_LOAD procedure creates the necessary support mechanisms for the new materialized views. This step also adds the new materialized views to the materialized view group that you created in the previous step. Be sure to execute the BEGIN_LOAD procedure for each materialized view that you will be importing.

Step 13 Connect as the owner of the materialized views at the materialized view site.

*/
CONNECT hr/hr@mview.world
/*

Step 14 Refresh materialized views to register them at master site.

In addition to retrieving the latest changes from the master tables, refreshing the materialized views at the new materialized view site registers the offline instantiated materialized views at the target master site.

Specifying a group owner when you define a new materialized view group and its related objects enables you to create multiple materialized view groups based on the same replication group at a single materialized view site. At a materialized view site, specifying group owners enables you to create multiple materialized view groups that are based on the same replication group at a master site or master materialized view site. You accomplish this by creating the materialized view groups under different schemas at the materialized view site.

Complete the following steps to use a group owner.

Note:

If you are viewing this document online, then you can copy the text from the "BEGINNING OF SCRIPT" line on this page to the "END OF SCRIPT" line into a text editor and then edit the text to create a script for your environment.

See Also:

Oracle9i Replication for a complete description of using group owners and the advantages of using multiple data sets

Step 1 Connect to the materialized view site as the materialized view administrator.

Step 2 Create materialized view group with group owner (gowner) bob using the CREATE_MVIEW_REPGROUP procedure.

The replication group that you specify in the gname parameter must match the name of the replication group that you are replicating at the target master site or master materialized view site. The gowner parameter enables you to specify an additional identifier that lets you create multiple materialized view groups based on the same replication group at the same materialized view site.

In this example, materialized view groups are created for the group owners bob and jane, and these two materialized view groups are based on the same replication group.

Step 3 Create the materialized views owned by bob.

The gowner value used when creating your materialized view objects must match the gowner value specified when you created the materialized view group in the previous procedures. After creating the materialized view groups, you can create materialized views based on the same master in the hr_repg materialized view group owned by bob and jane.

Caution:

Each object must have a unique name. When using a gowner to create multiple materialized view groups, duplicate object names could become a problem. To avoid any object-naming conflicts, you may want to append the gowner value to the end of the object name that you create, as illustrated in the following procedures (that is, creatematerializedviewhr.countries_bob). Such a naming method ensures that you do not create any objects with conflicting names.

Whenever you create a materialized view, always specify the schema name of the table owner in the query for the materialized view. In the examples below, hr is specified as the owner of the table in each query.