Oracle9i Replication Management API Reference
Release 1 (9.0.1)

Part Number A87502-01
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback

Go to previous page Go to next page

8
Managing a Materialized View Replication Environment

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.

This chapter contains these topics:

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.

The following example refreshes the hr_refg refresh group:

execute DBMS_REFRESH.REFRESH ('hr_refg');

See Also:

"REFRESH Procedure" for more information about the DBMS_REFRESH.REFRESH procedure 

The following example refreshes the hr.departments_mv materialized view:

BEGIN
   DBMS_MVIEW.REFRESH (
     list   =>  'hr.departments_mv', 
     method =>  '?');
END;
/

See Also:

"REFRESH Procedure" for more information about the DBMS_MVIEW.REFRESH procedure 

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:

BEGIN
   DBMS_REPCAT.SWITCH_MVIEW_MASTER (
      gname => 'hr_repg',
      master => 'orc3.world');
END;
/

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 recreated if you want to base it on a different master. 


See Also:

 

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.

This section contains the following sections:

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:

Executed At:

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. 


/************************* BEGINNING OF SCRIPT ******************************

  1. Connect to the master site as the materialized view group owner.

    */
    
    CONNECT hr/hr@orc3.world
    
    /*
    
    
  2. Drop the instantiated materialized view site from the master site.

    */
    
    BEGIN
       DBMS_REPCAT_INSTANTIATE.DROP_SITE_INSTANTIATION( 
          refresh_template_name =>  'hr_refg_dt',
          site_name => 'orc3.world');
    END;
    /
    
    /*
    
    
  3. Connect to the remote materialized view site as the materialized view administrator.

    */
    
    CONNECT mviewadmin/mviewadmin@sf.world
    
    /*
    
    

    If you are not able to connect to the remote materialized view site, then the target materialized view group cannot refresh, but the existing data still remains at the materialized view site.

  4. Drop the materialized view group.

    */
    
    BEGIN
       DBMS_REPCAT.DROP_MVIEW_REPGROUP (
          gname => 'hr_repg',
          drop_contents => TRUE);
    END;
    /
    
    /*
    
    

    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.

  5. Remove the refresh group.

    */
    
    BEGIN
       DBMS_REFRESH.DESTROY (
           name => 'hr_refg');
    END;
    /
    
    
    /************************* 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:

Executed At:

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. 


/************************* BEGINNING OF SCRIPT ******************************

  1. Connect to the master site as the replication administrator.

    */
    
    CONNECT repadmin/repadmin@orc1.world
    
    /*
    
    
  2. Drop the instantiated materialized view site from the master site.

    */
    
    BEGIN
       DBMS_REPCAT_RGT.DROP_SITE_INSTANTIATION ( 
          refresh_template_name =>  'hr_refg_dt',
          user_name => 'hr',
          site_name => 'orc3.world');
    END;
    /
    
    /*
    
    
  3. Connect to the remote materialized view site as the materialized view administrator.

    */
    
    CONNECT mviewadmin/mviewadmin@sf.world
    
    /*
    
    

    If you are unable to connect to the remote materialized view site, then the target materialized view group cannot refresh, but the existing data still remains at the materialized view site.

  4. Drop the materialized view group.

    */
    
    BEGIN
       DBMS_REPCAT.DROP_MVIEW_REPGROUP (
          gname => 'hr_repg',
          drop_contents => TRUE
          gowner => 'hr');
    END;
    /
    
    /*
    
    

    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.

  5. Remove the refresh group.

    */
    
    BEGIN
       DBMS_REFRESH.DESTROY (
           name => 'hr_refg');
    END;
    /
    
    
    /************************* 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:

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

    CONNECT mviewadmin/mviewadmin@mv1.world
    
    
  2. Drop the materialized view group.

    BEGIN
       DBMS_REPCAT.DROP_MVIEW_REPGROUP (
          gname => 'hr_repg',
          drop_contents => TRUE);
    END;
    /
    
    

    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.

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

    CONNECT mviewadmin/mviewadmin@mv1.world
    
    
  2. Drop the materialized view.

    BEGIN
       DBMS_REPCAT.DROP_MVIEW_REPOBJECT (
          sname => 'hr', 
          oname => 'employees_mv1', 
          type => 'SNAPSHOT', 
          drop_objects => TRUE);
    END;
    /
    
    

    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. 


/************************* BEGINNING OF SCRIPT ******************************

  1. Connect to the master site or master materialized view site as the replication administrator.

    */
    
    CONNECT repadmin/repadmin@orc1.world
    
    /*
    
  2. Unregister the materialized view groups.

    */
    
    BEGIN
       DBMS_REPCAT.UNREGISTER_MVIEW_REPGROUP (
          gname => 'hr_repg',
          mviewsite => 'mv1.world');
    END;
    /
    
    /*
    
    
  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. 


    */
    
    BEGIN
       DBMS_MVIEW.PURGE_MVIEW_FROM_LOG (
          mviewowner => 'hr',
          mviewname => 'countries_mv1', 
          mviewsite => 'mv1.world');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.PURGE_MVIEW_FROM_LOG (
          mviewowner => 'hr',
          mviewname => 'departments_mv1', 
          mviewsite => 'mv1.world');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.PURGE_MVIEW_FROM_LOG (
          mviewowner => 'hr',
          mviewname => 'employees_mv1', 
          mviewsite => 'mv1.world');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.PURGE_MVIEW_FROM_LOG (
          mviewowner => 'hr',
          mviewname => 'jobs_mv1', 
          mviewsite => 'mv1.world');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.PURGE_MVIEW_FROM_LOG (
          mviewowner => 'hr',
          mviewname => 'job_history_mv1', 
          mviewsite => 'mv1.world');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.PURGE_MVIEW_FROM_LOG (
          mviewowner => 'hr',
          mviewname => 'locations_mv1', 
          mviewsite => 'mv1.world');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.PURGE_MVIEW_FROM_LOG (
          mviewowner => 'hr',
          mviewname => 'regions_mv1', 
          mviewsite => 'mv1.world');
    END;
    /
    
    
    /************************* END OF SCRIPT **********************************/
    

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. 


/************************* BEGINNING OF SCRIPT ******************************

  1. Connect to the master site or master materialized view site as the replication administrator.

    */
    
    CONNECT repadmin/repadmin@orc1.world
    
    /*
    
    
  2. Unregister the materialized view.

    */
    
    BEGIN
       DBMS_MVIEW.UNREGISTER_MVIEW (
          mviewowner => 'hr',
          mviewname => 'employees_mv1',
          mviewsite => 'mv1.world');
    END;
    /
    
    /*
    
    
  3. Purge the associated materialized view log of the entries that were marked for the target materialized views.


    Note:

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


    */
    
    BEGIN
       DBMS_MVIEW.PURGE_MVIEW_FROM_LOG (
          mviewowner => 'hr',
          mviewname => 'employees_mv1', 
          mviewsite => 'mv1.world');
    END;
    /
    
    
    /************************* END OF SCRIPT **********************************/
    

Managing Materialized View Logs

The following sections explain how to manage materialized view logs:

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:

Altering Materialized View Log Storage Parameters

To alter a materialized view log's storage parameters, use the ALTER MATERIALIZED VIEW LOG 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 ALTER MATERIALIZED VIEW LOG. For example, the following statement alters a materialized view log on the customers table in the sales schema:

ALTER MATERIALIZED VIEW LOG ON hr.employees
  ADD (department_id);

See Also:

Oracle9i Replication for more information about adding columns to a materialized view log 

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:

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:

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:

BEGIN
   DBMS_MVIEW.PURGE_LOG (
      master => 'hr.employees',
      num    => 1,
      flag   => 'DELETE');
END;
/

See Also:

"PURGE_LOG 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 DELETE ANY TABLE 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:

  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;
    
    
  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;
    
    
  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;
    
    
  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:

    INSERT INTO hr.MLOG$_employees SELECT * FROM hr.templog;
    
    DROP TABLE hr.templog;
    
    
  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. 


Reorganizing Master Tables that Have Materialized View Logs

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.

See Also:

"Method 2 for Reorganizing Table employees" 

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 PRESERVE MATERIALIZED VIEW LOG option is the default. Therefore, if you specify the PRESERVE MATERIALIZED VIEW LOG 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.


Note:

To ensure that any previously fast refreshable materialized view is still refreshable, follow the guidelines in "Methods of Reorganizing a Database Table"


If the PURGE MATERIALIZED VIEW LOG option is specified, then the materialized view log is purged along with the master.

Examples

Either of the following two statements preserves materialized view log information when the master table named orders is truncated:

TRUNCATE TABLE hr.employees PRESERVE MATERIALIZED VIEW LOG;
TRUNCATE TABLE hr.employees;

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. 


Method 1 for Reorganizing Table employees

Complete the following steps:

  1. Call DBMS_MVIEW.BEGIN_TABLE_REORGANIZATION for table employees.

  2. Rename table employees to employees_old.

  3. Create table employees as SELECT * FROM employees_old.

  4. Call DBMS_MVIEW.END_TABLE_REORGANIZATION for new table employees.


    Caution:

    When a table is renamed, its associated PL/SQL triggers are also adjusted to the new name of the table. 


Ensure that no transaction is issued against the reorganized table between calling BEGIN_TABLE_REORGANIZATION and END_TABLE_REORGANIZATION.

Method 2 for Reorganizing Table employees

Complete the following steps:

  1. Call DBMS_MVIEW.BEGIN_TABLE_REORGANIZATION for table employees.

  2. Export table employees.

  3. Truncate table employees with PRESERVE MATERIALIZED VIEW LOG option.

  4. Import table employees using conventional path.

  5. Call DBMS_MVIEW.END_TABLE_REORGANIZATION for new table employees.


    Caution:

    When you truncate masters as part of a reorganization, you must use the PRESERVE MATERIALIZED VIEW LOG clause of the truncate table DDL.  


Ensure that no transaction is issued against the reorganized table between calling BEGIN_TABLE_REORGANIZATION and END_TABLE_REORGANIZATION.

Method 3 for Reorganizing Table employees

Complete the following steps:

  1. Call DBMS_MVIEW.BEGIN_TABLE_REORGANIZATION for table employees.

  2. Export table employees.

  3. Rename table employees to employees_old.

  4. Import table employees using conventional path.

  5. Call DBMS_MVIEW.END_TABLE_REORGANIZATION for new table employees.


    Caution:

    When a table is renamed, its associated PL/SQL triggers are also adjusted to the new name of the table. 


Ensure that no transaction is issued against the reorganized table between calling BEGIN_TABLE_REORGANIZATION and END_TABLE_REORGANIZATION.

Method 4 for Reorganizing Table employees

Complete the following steps:

  1. Call DBMS_MVIEW.BEGIN_TABLE_REORGANIZATION for table employees.

  2. Select contents of table employees to a flat file.

  3. Rename table employees to employees_old.

  4. Create table employees with the same shape as employees_old.

  5. Run SQL*Loader using conventional path.

  6. Call DBMS_MVIEW.END_TABLE_REORGANIZATION for new table employees.


    Caution:

    When a table is renamed, its associated PL/SQL triggers are also adjusted to the new name of the table. 


Ensure that no transaction is issued against the reorganized table between calling BEGIN_TABLE_REORGANIZATION and END_TABLE_REORGANIZATION.

See Also:

 

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:

Here, a master can be a master table or a master materialized view. To delete a materialized view log, execute the DROP MATERIALIZED VIEW LOG 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 DROP ANY TABLE 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:

Executed At:

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. 


/************************* BEGINNING OF SCRIPT ******************************

  1. In a separate terminal window, set up the new materialized view site.

    Make sure that the appropriate schema and database links have been created before you perform the offline instantiation of your materialized view.

    See Also:

    "Setting Up Materialized View Sites" 

    */
    
    PAUSE Press <RETURN> to continue the new materialized view site is set up.
    
    /*
    
    
  2. Connect to the master site as the replication administrator.

    */
    
    CONNECT repadmin/repadmin@orc1.world
    
    /*
    
    
  3. Create the necessary materialized view logs.

    If materialized view logs do not already exist for the target master tables, then create them at the target master site.

    */
    
    CREATE MATERIALIZED VIEW LOG ON hr.countries;
    CREATE MATERIALIZED VIEW LOG ON hr.departments;
    CREATE MATERIALIZED VIEW LOG ON hr.employees;
    CREATE MATERIALIZED VIEW LOG ON hr.jobs;
    CREATE MATERIALIZED VIEW LOG ON hr.job_history;
    CREATE MATERIALIZED VIEW LOG ON hr.locations;
    CREATE MATERIALIZED VIEW LOG ON hr.regions;
    
    /*
    
    
  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. 


    */
    
    CREATE MATERIALIZED VIEW hr.countries_mv 
          REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT 
          country_id, country_name, region_id 
          FROM hr.countries;
    
    CREATE MATERIALIZED VIEW hr.departments_mv 
          REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT 
          department_id, department_name, manager_id, location_id
          FROM hr.departments;
    
    CREATE MATERIALIZED VIEW hr.employees_mv 
          REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT 
          employee_id, first_name, last_name, email, phone_number, 
          hire_date, job_id, salary, commission_pct, manager_id, 
          department_id
          FROM hr.employees;
    
    CREATE MATERIALIZED VIEW hr.jobs_mv 
          REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT 
          job_id, job_title, min_salary, max_salary 
          FROM hr.jobs;
    
    CREATE MATERIALIZED VIEW hr.job_history_mv 
          REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT 
          employee_id, start_date, end_date, job_id, department_id 
          FROM hr.job_history;
    
    CREATE MATERIALIZED VIEW hr.locations_mv 
          REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT 
          location_id, street_address, postal_code, city, 
          state_province, country_id 
          FROM hr.locations;
    
    CREATE MATERIALIZED VIEW hr.regions_mv 
          REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT 
          region_id, region_name 
          FROM hr.regions;
    
    /*
    
    
  5. In a separate terminal window, connect as the owner of the materialized views to export the temporary materialized views you created in Step 4.

    Use the Oracle Export utility to generate the export file that you will transfer to the new materialized view site. The export file will contain the base tables of your temporary materialized views.


    Note:

    The following example is for Oracle8i and higher databases only. Base tables in database versions earlier than Oracle8i are preceded by the SNAP$ prefix (that is, SNAP$_employees_mv). 


    See Also:

    Oracle9i Database Utilities for additional information about exporting 

    The following is an example of an export command for the hr schema.

    exp hr/hr@orc1.world TABLES='countries_mv','departments_mv','employees_mv',
    'jobs_mv','job_history_mv','locations_mv','regions_mv'
    
    */
    
    PAUSE Press <RETURN> to continue when the export is complete.
    
    /*
    
    
  6. Connect to the new materialized view site as SYSTEM user.

    */
    
    CONNECT system/manager@mview.world
    
    /*
    
    
  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.

    */
    
    CREATE TABLESPACE demo_mview
     DATAFILE 'demo_mview.dbf' SIZE 10M AUTOEXTEND ON
     EXTENT MANAGEMENT LOCAL AUTOALLOCATE;
    
    CREATE TEMPORARY TABLESPACE temp_mview
     TEMPFILE 'temp_mview.dbf' SIZE 5M AUTOEXTEND ON;
    
    CREATE USER hr IDENTIFIED BY hr;
    
    ALTER USER hr DEFAULT TABLESPACE demo_mview
                  QUOTA UNLIMITED ON demo_mview;
    
    ALTER USER hr TEMPORARY TABLESPACE temp_mview;
    
    GRANT 
      CREATE SESSION, 
      CREATE TABLE, 
      CREATE PROCEDURE, 
      CREATE SEQUENCE, 
      CREATE TRIGGER, 
      CREATE VIEW, 
      CREATE SYNONYM, 
      ALTER SESSION,
      CREATE MATERIALIZED VIEW,
      ALTER ANY MATERIALIZED VIEW,
      CREATE DATABASE LINK,
    TO hr;
    
    CONNECT hr/hr@mview.world
    
    CREATE DATABASE LINK orc1.world CONNECT TO hr IDENTIFIED by hr;
    
    /*
    
    
  8. Connect to the new materialized view site as the materialized view administrator.

    */
    
    CONNECT mviewadmin/mviewadmin@mview.world
    
    /*
    
    
  9. Create an empty materialized view group.

    Run the DBMS_REPCAT.CREATE_MVIEW_REPGROUP procedure at the new materialized view site to create an empty materialized view group to which you will add your materialized views.

    */
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPGROUP (
          gname => 'hr_repg',
          master => 'orc1.world',
          propagation_mode => 'ASYNCHRONOUS');
    END;
    /
    
    /*
    
    
  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.

    */
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.BEGIN_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          master_site => 'orc1.world',
          snapshot_oname => 'countries_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.BEGIN_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          master_site => 'orc1.world',
          snapshot_oname => 'departments_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.BEGIN_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          master_site => 'orc1.world',
          snapshot_oname => 'employees_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.BEGIN_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          master_site => 'orc1.world',
          snapshot_oname => 'jobs_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.BEGIN_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          master_site => 'orc1.world',
          snapshot_oname => 'job_history_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.BEGIN_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          master_site => 'orc1.world',
          snapshot_oname => 'locations_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.BEGIN_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          master_site => 'orc1.world',
          snapshot_oname => 'regions_mv');
    END;
    /
    
    /*
    
    
  11. In a separate terminal window, connect as the owner of the materialized views to import at the new materialized view site.

    Use the Oracle Import utility to import the file that you exported in Step 5. Make sure that you import your data as the same user who exported the data. This user hr in the following example:

    imp hr/hr@mview.world FULL=y IGNORE=y
    
    */
    
    PAUSE Press <RETURN> to continue when the import is complete.
    
    /*
    
    
  12. Complete the offline instantiation.

    Execute the DBMS_OFFLINE_SNAPSHOT.END_LOAD procedure to finish the offline instantiation of the imported materialized views.

    */
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.END_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          snapshot_oname => 'countries_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.END_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          snapshot_oname => 'departments_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.END_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          snapshot_oname => 'employees_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.END_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          snapshot_oname => 'jobs_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.END_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          snapshot_oname => 'job_history_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.END_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          snapshot_oname => 'locations_mv');
    END;
    /
    
    BEGIN
       DBMS_OFFLINE_SNAPSHOT.END_LOAD (
          gname => 'hr_repg',
          sname => 'hr',
          snapshot_oname => 'regions_mv');
    END;
    /
    
    /*
    
    
  13. Connect as the owner of the materialized views at the materialized view site.

    */
    
    CONNECT hr/hr@mview.world
    
    /*
    
    
  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.

    */
    
    BEGIN
       DBMS_MVIEW.REFRESH ('countries_mv');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.REFRESH ('departments_mv');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.REFRESH ('employees_mv');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.REFRESH ('jobs_mv');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.REFRESH ('job_history_mv');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.REFRESH ('locations_mv');
    END;
    /
    
    BEGIN
       DBMS_MVIEW.REFRESH ('regions_mv');
    END;
    /
    
    /*
    
    
  15. Connect to the master site as the replication administrator.

    */
    
    CONNECT repadmin/repadmin@orc1.world
    
    /*
    
    
  16. Delete the temporary materialized views you created in Step 4 at the master site.

    */
    
    DROP MATERIALIZED VIEW hr.countries_mv;
    DROP MATERIALIZED VIEW hr.departments_mv;
    DROP MATERIALIZED VIEW hr.employees_mv;
    DROP MATERIALIZED VIEW hr.jobs_mv;
    DROP MATERIALIZED VIEW hr.job_history_mv;
    DROP MATERIALIZED VIEW hr.locations_mv;
    DROP MATERIALIZED VIEW hr.regions_mv;
    
    /*
    
    
    /************************* END OF SCRIPT **********************************/
    

Using a Group Owner for a Materialized View Group

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 

/************************* BEGINNING OF SCRIPT ******************************

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

    */
    
    CONNECT mviewadmin/mviewadmin@mv1.world
    
    /*
    
    
  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.

    */
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPGROUP (
          gname => 'hr_repg',
          master => 'orc1.world',
          propagation_mode => 'ASYNCHRONOUS',
          gowner => 'bob');
    END;
    /
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPGROUP (
          gname => 'hr_repg',
          master => 'orc1.world',
          propagation_mode => 'ASYNCHRONOUS',
          gowner => 'jane');
    END;
    /
    
    /*
    
    
  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, create materialized view hr.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.

    */
    
    CREATE MATERIALIZED VIEW hr.countries_bob 
      REFRESH FAST WITH PRIMARY KEY FOR UPDATE 
      AS SELECT * FROM hr.countries@orc1.world;
    
    CREATE MATERIALIZED VIEW hr.departments_bob 
      REFRESH FAST WITH PRIMARY KEY FOR UPDATE 
      AS SELECT * FROM hr.departments@orc1.world;
    
    CREATE MATERIALIZED VIEW hr.employees_bob 
      REFRESH FAST WITH PRIMARY KEY FOR UPDATE 
      AS SELECT * FROM hr.employees@orc1.world;
    
    CREATE MATERIALIZED VIEW hr.jobs_bob 
      REFRESH FAST WITH PRIMARY KEY FOR UPDATE 
      AS SELECT * FROM hr.jobs@orc1.world;
    
    CREATE MATERIALIZED VIEW hr.job_history_bob 
      REFRESH FAST WITH PRIMARY KEY FOR UPDATE 
      AS SELECT * FROM hr.job_history@orc1.world;
    
    CREATE MATERIALIZED VIEW hr.locations_bob 
      REFRESH FAST WITH PRIMARY KEY FOR UPDATE 
      AS SELECT * FROM hr.locations@orc1.world;
    
    CREATE MATERIALIZED VIEW hr.regions_bob 
      REFRESH FAST WITH PRIMARY KEY FOR UPDATE 
      AS SELECT * FROM hr.regions@orc1.world;
    
    /*
    
    
  4. Create the materialized views owned by jane.

    */
    
    CREATE MATERIALIZED VIEW hr.departments_jane 
      REFRESH FAST WITH PRIMARY KEY FOR UPDATE 
      AS SELECT * FROM hr.departments@orc1.world;
    
    CREATE MATERIALIZED VIEW hr.employees_jane 
      REFRESH FAST WITH PRIMARY KEY FOR UPDATE 
      AS SELECT * FROM hr.employees@orc1.world;
    
    /*
    
  5. Add the materialized views owned by bob to the materialized view group.

    */
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPOBJECT (
          gname => 'hr_repg',
          sname => 'hr',
          oname => 'countries_bob',
          type => 'SNAPSHOT',
          min_communication => TRUE,
          gowner => 'bob');
    END;
    /
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPOBJECT (
          gname => 'hr_repg',
          sname => 'hr',
          oname => 'departments_bob',
          type => 'SNAPSHOT',
          min_communication => TRUE,
          gowner => 'bob');
    END;
    /
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPOBJECT (
          gname => 'hr_repg',
          sname => 'hr',
          oname => 'employees_bob',
          type => 'SNAPSHOT',
          min_communication => TRUE,
          gowner => 'bob');
    END;
    /
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPOBJECT (
          gname => 'hr_repg',
          sname => 'hr',
          oname => 'jobs_bob',
          type => 'SNAPSHOT',
          min_communication => TRUE,
          gowner => 'bob');
    END;
    /
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPOBJECT (
          gname => 'hr_repg',
          sname => 'hr',
          oname => 'job_history_bob',
          type => 'SNAPSHOT',
          min_communication => TRUE,
          gowner => 'bob');
    END;
    /
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPOBJECT (
          gname => 'hr_repg',
          sname => 'hr',
          oname => 'locations_bob',
          type => 'SNAPSHOT',
          min_communication => TRUE,
          gowner => 'bob');
    END;
    /
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPOBJECT (
          gname => 'hr_repg',
          sname => 'hr',
          oname => 'regions_bob',
          type => 'SNAPSHOT',
          min_communication => TRUE,
          gowner => 'bob');
    END;
    /
    
    /*
    
    
  6. Add the materialized views owned by jane to the materialized view group.

    */
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPOBJECT (
          gname => 'hr_repg',
          sname => 'hr',
          oname => 'departments_jane',
          type => 'SNAPSHOT',
          min_communication => TRUE,
          gowner => 'jane');
    END;
    /
    
    BEGIN
       DBMS_REPCAT.CREATE_MVIEW_REPOBJECT (
          gname => 'hr_repg',
          sname => 'hr',
          oname => 'employees_jane',
          type => 'SNAPSHOT',
          min_communication => TRUE,
          gowner => 'jane');
    END;
    /
    
    
  7. Add your materialized views to a refresh group.

    See Also:

    Chapter 5, "Create Materialized View Group" (Step 6) for more information about adding materialized views to a refresh group 

    /************************* END OF SCRIPT **********************************/
    
    
    
    
    

Go to previous page Go to next page
Oracle
Copyright © 1996-2001, Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback