HP’s Oracle Blog

Notes of a Junior on the Path to Oracle Mastery


Notes of a Junior on the Path to Oracle Mastery
On this blog I am writing about thoughts, solutions, failures and pitfalls on the path to Oracle Mastery. It’s still a long way to go. On this blog, the main focus is learning Oracle.

Materialized View Replication

Author: HP Fuchs | Category: Oracle Database, PL/SQL

This post is for my friend Gerry, who - as a newby to Oracle - has to support a replicated environment. Gerry is my successor at Kanton Luzern. Management didn’t look for a new DBA because they said it’s hopeless to find somebody. Instead they decided to give somebody inhouse the opportunity. Gerry is highly motivated to learn Oracle. He took the chance.

Kanton Luzern uses LiveLink for web content management. LiveLink websites are stored in an Oracle database. Kanton Luzern hosts 144 websites.
There is a staging server where content is being added/edited and a live server where websites are accessed from the internet. New content is replicated from stage to live.

Whenever a content manager wishes to publish new or edited content to the live webserver he may trigger replication just by clicking on a button inside the content management tool. This is not a standard feature of LiveLink Content Manager. I wrote an extension for Content Manager, and a stored procedure to control replication.

Materialized View Replication has been introduced with Oracle 8i. Back then it was called Snapshot Replication. For a quick overview of materialized views read Duncan Davies’ post.
To set up materialized view replication is rather simple. But it takes a few steps.

hpfuchs@hplnbk:~> sqlplus /nolog

SQL*Plus: Release 11.1.0.6.0 - Production on Sun Jan 13 10:18:18 2008

Copyright (c) 1982, 2007, Oracle. All rights reserved.
SQL> connect hpfuchs/*****
Connected.

SQL> CREATE TABLE tab1 AS SELECT * FROM dict;

Table created.

Step 1 - Materialized View Log at Master Site
All changes in a base table are written to its materialized view log.

SQL> CREATE MATERIALIZED VIEW LOG ON tab1;
create materialized view log on tab1
*
ERROR at line 1:
ORA-12014: table ‘TAB1′ does not contain a primary key constraint

Ups! Materialized view log can’t be created on tables with no primary key. Let’s create primary key for tab1.

SQL> desc tab1;
Name Null? Type
—————————————– ——– —————————-
TABLE_NAME VARCHAR2(30)
COMMENTS VARCHAR2(4000)

SQL> ALTER TABLE tab1 ADD CONSTRAINT pk_tab1 PRIMARY KEY (table_name);

Table altered.

SQL> CREATE MATERIALIZED VIEW LOG ON tab1;

Materialized view log created.

Step 2 - Set up Materialized View Site
Most likely the materialized view site is another Oracle database. I have only one database on my notebook. Therefore I just create another user who is the owner of the replication site.

SQL> connect system/*****
Connected.
SQL> CREATE USER otherdb IDENTIFIED BY ***** DEFAULT TABLESPACE users QUOTA UNLIMITED ON users;

User created.

SQL> GRANT connect, resource, create materialized view, create database link TO otherdb;

Grant succeeded.

Step 3 - Database Link
To access objects in another database you need a database link between the two databases. Because I have only one database on my notebook, I will create a loopback database link. Omit the @{connect identifier} - in my case: @orcl - to create a real database link.

SQL> connect otherdb/*****
Connected.

SQL> CREATE DATABASE LINK orcl.kt.lunet.ch@orcl CONNECT TO hpfuchs IDENTIFIED BY ***** USING ‘orcl’;

Database link created.

Step 4 - Materialized View

SQL> CREATE MATERIALIZED VIEW mv1 REFRESH FAST AS SELECT * FROM tab1@orcl.kt.lunet.ch@orcl;

Materialized view created.

Materialized views can be indexed.

SQL> CREATE INDEX ix_mv1_objecttype ON mv1 (table_name);

Index created.

Step 5 - Refresh Group
Materialized views can be grouped together into refresh groups. Refresh groups are replicated as a whole. My replication interval is set to one hour. Materialized view replication uses DBMS_JOBS to schedule its intervals. To switch off all scheduled replication activity set JOB_QUEUE_PROCESSES to 0 at the replication site. Make sure all non-replication jobs use DBMS_SCHEDULER. You better check V$JOBS before changing that parameter to 0…

SQL> BEGIN
dbms_refresh.make(
name => ‘mv1_refgroup’,
list => ‘MV1′,
next_date => sysdate,
interval => ’sysdate + 1′,
implicit_destroy => true,
lax => true);
END;
/

PL/SQL procedure successfully completed.

Step 6 - Refresh At Will
The following command refreshes group “mv1_refgroup”.

SQL> exec dbms_refresh.refresh(name => ‘mv1_refgroup’);

PL/SQL procedure successfully completed.

February 6th, 2008.


10 Responses to “Materialized View Replication”

  1. Gerry Bammert Says:

    Hi HP

    Your description about materialized view replication is just great.
    I’m very happy to have you as a personal trainer. Without your help I would be lost, you know. Thanks a lot!
    I wish you good luck in your interesting new job and I’m glad to see you soon …

    Gerry

  2. Mahesh Says:

    It is really good one ! But Is it two-way replication or one-way replication using MV ? Is it working fine now ? Because i got stuck with Step 4. :(

  3. HP Fuchs Says:

    Hallo Mahesh
    Did you leave out the second “@orcl” when you have two databases?

  4. Mahesh Says:

    Hey, I thought that the steps provided here which i can use for my scenario. It is like,

    I want to setup replication using Mviews within a database.

    The steps provided here seems, similar to my requirement. So, started implementing it. But while creating DB Link I faced an issue.

    SQL> CREATE DATABASE LINK R5DEV10G.AIG.COM CONNECT TO gens1 IDENTIFIED BY gens1 USING ‘R5DEV10G.AIG.COM’;
    CREATE DATABASE LINK R5DEV10G.AIG.COM CONNECT TO gens1 IDENTIFIED BY gens1 USING ‘R5DEV10G.AIG.COM’
    *
    ERROR at line 1:
    ORA-02082: a loopback database link must have a connection qualifier

    As i am quite new to MView, I am not for sure how to resolve it and tried my level best to find the solution finally I opened ticket to Oracle to get some help.

    Now, as per oracle support, I skipped dblink creation step and created Mview. Now, getting new error as below while creating index
    SQL> CREATE INDEX indx_mv_objecttype ON TESTMV(gens2.emp);
    CREATE INDEX indx_mv_objecttype ON TESTMV(gens2.emp)
    *
    ERROR at line 1:
    ORA-00904: “GENS2″.”EMP”: invalid identifier

    Tell me onething,
    1. is this steps for Mview replication within a database ?
    2. Is this replication bi-directional replication (replicate each other) ?

    Do you have any clue if i am wrong anywhere !?

  5. HP Fuchs Says:

    Hallo Mahesh

    The steps describe a materialized view replication (not bi-directional) within a single database. You get an error when trying to create a database link from instance r5dev10g to itself. You need a loopback database link. Use this syntax:

    CREATE DATABASE LINK instance.kt.lunet.ch@instance CONNECT TO user IDENTIFIED BY password USING ‘instance’;

    Did it work?

  6. dina Says:

    is Materialized View in master table DB??

  7. HP Fuchs Says:

    Hello Dina

    Materialized Views are in the materialized view site. At the Replicated site. In my example it’s the same DB as the Master DB, because I used a loopback DB link in my example. In real life you have a master site (tables and snapshot logs) and a replication site (materialized views).

  8. Kumar Says:

    Hi There,
    Nice Article on Materialized Views. I am working on Oracle Incentive Compensation as a technical person. My responsibility is to interface the transaction data to incentive module to calculate commission. Compensation will be calcuated every month. I have developed a PL/SQL program to extract transactions every month to put the transactions in the staging area where incentive module picks up the data from the staging table. This is the basic overview of my work.

    The problem i am facing is for every month there are about 6 million transactions needs to be send to the staging table. It is a nightmare if i use the PL/SQL program to load the staging table. For this situation, can i use materialized view instead of using the PL/SQL program to load the staging table? Any help greatly appreciated..

  9. shreekanth Says:

    Hi may i know how to find whether my database has materilized view or not.any sql statement is there

  10. HP Fuchs Says:

    Hallo Shreekanth

    SQL> SELECT owner || ‘.’ || mview_name FROM dba_mviews;

    no rows selected

    SQL>

    My database doesn’t have materialized views at the moment.

Your comment