物化视图实现 Oracle 数据库表双向同步


Oracle 数据库跨库同步表有很多种方式可以实现, 比如触发器, Materialized View(MV), Stream, Goldengate 等

Materialized View(物化视图)是包括一个查询结果的数据库对像, 它是远程数据的的本地副本, 或者用来生成基于数据表求和的汇总表. 物化视图存储基于远程表的数据, 也可以称为快照. 这个基本上就说出了物化视图的本质, 它是一组查询的结果, 这样势必为将来再次需要这组数据时大大提高查询性能.下面就介绍使用 Materialized View + Job 的方式来双向同步表,具体步骤如下:

1. 在源数据库 A 和目标数据库 B 上分别建立 table     

create table test 

    id varchar2(10) not null primary key, 
    name varchar2(20), 
    status varchar2(1), 
    updatedate date 

2. 在数据库上分别建立 dblink

create database link dblink_to_B 
connect to "userid" identified by "password" 
using '(DESCRIPTION = 
    (ADDRESS_LIST = 
        (ADDRESS = (PROTOCOL = TCP)(HOST = "ipaddress")(PORT = 1521)) 
    ) 
    (CONNECT_DATA = 
        (SERVICE_NAME = "SID") 
    ) 
)'; 
 
create database link dblink_to_A 
connect to "userid" identified by "password" 
using '(DESCRIPTION = 
    (ADDRESS_LIST = 
        (ADDRESS = (PROTOCOL = TCP)(HOST = "ipaddress")(PORT = 1521)) 
    ) 
    (CONNECT_DATA = 
        (SERVICE_NAME = "SID") 
    ) 
)'; 

3. 在源数据库 A 上建立 Materialized View 以及 Materialized view log

create materialized view log on test with rowid 
   
create materialized view mv_test refresh fast on demand with rowid 
as select * from <A href="mailto:test@dblink_to_B">test@dblink_to_B</A>

  • 1
  • 2
  • 下一页

相关内容