创建Writable Materialized View在DB之间增量同步数据

数据库之间可以通过DBLINK+Materialized View实现数据同步; 若需要对物化视图做UPDATE更新操作,则需要建立Writable Materialized View,在建立物化视图时指定FOR UPDATE子句。由于Writable Materialized View要求物化视图能够快速刷新,所以建立必要的物化视图日志Materialized View Log。

 

以下为创建远程可更新物化视图及其管理示例:

 

 

 

source 源端

 

 

SQL> conn c##maclean/oracle

Connected.

SQL>

SQL> create table sourcet as select * from dba_objects;

 

Table created.

 

 

 

 

SQL> select count(*) from sourcet;

 

COUNT(*)

———-

89134

必须建立基于rowid的物化视图日志

 

SQL> create MATERIALIZED VIEW  log on  sourcet with rowid including new values;

 

Materialized view log created.

 

 

 

 

 

target  目标端 ,首先创建必要的DBLINK

 

create database link remote connect to c##maclean identified by oracle using ‘C12’;

 

创建远程物化视图,NEXT SYSDATE + 1/1440 为 每分钟刷新一次

create MATERIALIZED VIEW  target refresh fast with rowid START WITH sysdate NEXT SYSDATE + 1/1440 for update as select * from sourcet@remote;

 

SQL> select count(*) from target;

 

COUNT(*)

———-

89134

Writeable Materialiezd View 可以正常更新

 

 

SQL> delete target where rownum<100;

 

已删除99行。

 

SQL> commit;

 

提交完成。

 

 

Materialiezd View 上可以创建索引和视图

 

 

 

SQL> create index objd_ind on target(object_id);

 

索引已创建。

 

SQL> create view view_objd as select * from target where object_id>1000;

 

视图已创建。

 

 

Materialiezd View 指定了 NEXT SYSDATE + 1/1440 后会 每分钟自动刷新一次

 

 

 

SQL> select count(*) from target;

 

COUNT(*)

———-

89134

可以将Materialiezd View 修改为手动刷新

 

 

ALTER MATERIALIZED VIEW target  refresh on demand;

 

将Materialiezd View  改为自动刷新

 

ALTER MATERIALIZED VIEW target   refresh START WITH sysdate NEXT SYSDATE + 1/1440;

 

 

手动COMPLETE刷新 物化视图

 

exec dbms_mview.refresh(‘MVIEWA’,’C’);

 

 

手动FAST刷新 物化视图

 

exec dbms_mview.refresh(‘MVIEWA’,’F’);

 

 

重新编译 物化视图

 

alter MATERIALIZED VIEW target compile;

 


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *