A materialized view, or snapshot as they were previously known, is a table segment whose contents are periodically refreshed based on a query, either against a local or remote table. Using materialized views against remote tables is the simplest way to achieve replication of data between sites.
Overview
Oracle uses materialized views (also known as snapshots in prior releases) to replicate data to non-master sites in a replication environment and to cache expensive queries in a data warehouse environment.
A materialized view is a replica of a target master from a single point in time. The master can be either a master table at a master site or a master materialized view at a materialized view site. Whereas in multi-master replication tables are continuously updated by other master sites, materialized views are updated from one or more masters through individual batch updates, known as a refreshes, from a single master site or master materialized view site.
-- Master Database Site SELECT * FROM DBA_MVIEW_LOGS WHEREMASTER='<BASE TABLE NAME>', CREATEMATERIALIZEDVIEWLOGON <BASE TABLE> TABLESPACE <TABLESPACENAME> WITH <PRIMARY KEY>/<ROWID>; DROPMATERIALIZEDVIEWLOGON <Schema>.<Table_Name>;
-- MView Database Site CREATEDATABASELINK <NAME> CONNECTTO <Master DB SchemaName> IDENTIFIEDBY < Master DB SchemaPassword> USING'<Master DB TNS>';
CREATEMATERIALIZEDVIEW"USERNAME"."MVIEW_NAME" ORGANIZATIONHEAP PCTFREE 10PCTUSED0INITRANS2MAXTRANS255NOCOMPRESS NOLOGGING STORAGE(INITIAL1048576NEXT1048576MINEXTENTS1MAXEXTENTS2147483645 PCTINCREASE0FREELISTS1FREELISTGROUPS1BUFFER_POOLDEFAULT) TABLESPACE"<Tablespace Name>" BUILDIMMEDIATE USINGINDEX PCTFREE 10INITRANS2MAXTRANS255 STORAGE(INITIAL1048576NEXT1048576MINEXTENTS1MAXEXTENTS2147483645 PCTINCREASE0FREELISTS1FREELISTGROUPS1BUFFER_POOLDEFAULT) TABLESPACE""<TablespaceName>" REFRESH FAST ON DEMAND START WITH sysdate+0 NEXT TRUNC(SYSDATE+1)+6/24 -- Morning 6 AM -- REFRESH FAST ON DEMAND START WITH sysdate+0 NEXT SYSDATE + 5/(60*24) -- Every 5 Mins -- REFRESH COMPLETE ON DEMAND START WITH sysdate+0 NEXT SYSDATE + 30/(60*24) -- Every 30 Mins -- REFRESH COMPLETE ON DEMAND START WITH sysdate+0 NEXT SYSDATE + 1 WITH PRIMARY KEY USING DEFAULT LOCAL ROLLBACK SEGMENT DISABLE QUERY REWRITE --WITH ROWID USING DEFAULT LOCAL ROLLBACK SEGMENT DISABLE QUERY REWRITE AS SELECT COLUMN_1 Col_Alias_Name , COLUMN_2 Col_Alias_Name, COLUMN_3 Col_Alias_Name, --NULL Col_Alias_Name,, --CAST(NULL as varchar2(1)) Col_Alias_Name,, FROM <Master Schema>.<Master Base Table>@<DB_LINK Name>;
-- Find All JOBs/MViews Related to Master DB col JOB FOR 9999 col OWNER for a8 col MASTER_LINK for a15 col HOST for a10 col FAST_REFRESHABLE for a8 col STALENESS for a9 col COMPILE_STATE for a8 col MVIEW_NAME for a20 col "REFRH MOD-METHOD BLD_MOD" for a22 col INTERVAL FOR a24
SELECT JOB ,a.OWNER,MVIEW_NAME ,UPDATABLE ,REWRITE_ENABLED ,MASTER_LINK ,c.HOST ,REFRESH_MODE||'-'||REFRESH_METHOD||'-'||BUILD_MODE "REFRH MOD-METHOD BLD_MOD" ,FAST_REFRESHABLE ,LAST_REFRESH_TYPE ,to_char(LAST_REFRESH_DATE,'dd-Mon-yy hh24:mi:ss') LAST_REFRESH_DATE ,COMPILE_STATE ,INTERVAL ,USE_NO_INDEX FROM DBA_MVIEWS a, DBA_REFRESH_CHILDREN b, DBA_DB_LINKS c WHERE a.MVIEW_NAME=b.NAME AND a.OWNER=b.OWNER And MASTER_LINK in (SELECT'@'||DB_LINK from dba_db_links whereUPPER(HOST) ='<Master DB Name>') ANDSUBSTR(a.MASTER_LINK,2) = c.DB_LINK ANDUPPER(c.HOST) ='<Master DB Name>' Orderby MASTER_LINK, MVIEW_NAME;
-- Query Materialized View Log in Master DB Site SELECT * FROM DBA_MVIEW_LOGS WHEREMASTER='<BASE TABLE NAME>';