[20150610]使用物化檢視同步資料.txt

lfree發表於2015-06-10

[20150610]使用物化檢視同步資料.txt

--昨天聽別人的一個需求要同步一個表的資料,要求使用golden gate有點小題大作。實際上物化事務就可以了,自己以前做過一些測試,也
--許沒做記錄,這次做一個記錄。

1.建立測試環境:
--源資料庫10g  10.2.0.4.0  IP=192.168.100.89
--同步表T。
create table t ( id number CONSTRAINTS pk_t primary key , name varchar2(20));
insert into t  select rownum,'test' from dual connect by level<=10;
commit ;

--目的資料庫11g 11.2.0.3.0 . IP=192.168.100.40
CREATE  PUBLIC DATABASE LINK "TEST089.COM" CONNECT TO SCOTT  IDENTIFIED BY btbtms USING '192.168.100.89:1521/test';
--建立一樣的表:
create table t ( id number CONSTRAINTS pk_t primary key , name varchar2(20));

SCOTT@test> select * from t@test089.com where rownum=1;
        ID NAME
---------- --------------------
         1 test

--測試dblink透過。

2.測試同步:
--源資料庫10g  10.2.0.4.0 
create materialized view log on t;

--目的資料庫11g 11.2.0.3.0 .
create materialized view t on prebuilt table refresh fast start with sysdate next sysdate + 1/24/60 as select * from t@test089.com;


3.開始一次測試:
--源資料庫10g  10.2.0.4.0 做一些dml操作看看:

insert into t values (11,'aaa');
commit ;
update t set name='xxx' where id=5;
commit ;
delete from t where id=2;
commit ;

SCOTT@test> select * from t;
          ID NAME
------------ --------------------
           1 test
           3 test
           4 test
           5 xxx
           6 test
           7 test
           8 test
           9 test
          10 test
          11 aaa

10 rows selected.

--等1分鐘觀察目的資料庫情況:
--目的資料庫11g 11.2.0.3.0 .

SCOTT@test> select * from t;
        ID NAME
---------- --------------------
        11 aaa
         5 xxx

--搞錯,應該copy資料先,重做。

drop  materialized view t ;
delete from t;
commit ;

SCOTT@test> select * from t;
no rows selected

SCOTT@test> insert into t  select * from t@test089.com;
10 rows created.

SCOTT@test> commit ;
Commit complete.

create materialized view t on prebuilt table refresh fast start with sysdate next sysdate + 1/24/60 as select * from t@test089.com;


4.再重複測試:
--源資料庫10g  10.2.0.4.0 做一些dml操作看看:
insert into t values (12,'bbb');
commit ;
update t set name='yyy' where id=5;
commit ;
delete from t where id=3;
commit ;

SCOTT@test> select * from t;
          ID NAME
------------ --------------------
           1 test
           4 test
           5 yyy
           6 test
           7 test
           8 test
           9 test
          10 test
          11 aaa
          12 bbb

10 rows selected.

--等1分鐘觀察目的資料庫情況:
--目的資料庫11g 11.2.0.3.0 .


SCOTT@test> select * from t;
        ID NAME
---------- --------------------
         1 test
         4 test
         5 yyy
         6 test
         7 test
         8 test
         9 test
        10 test
        11 aaa
        12 bbb

10 rows selected.

SCOTT@test> select * from t minus select * from t@test089.com;
no rows selected

SCOTT@test> select * from t@test089.com  minus select * from t;
no rows selected

透過檢視可以觀察同步的情況:
--目的資料庫11g 11.2.0.3.0 :
SCOTT@test> SELECT JOB, LOG_USER, LAST_DATE, NEXT_DATE, FAILURES FROM USER_JOBS;
       JOB LOG_USER                       LAST_DATE           NEXT_DATE             FAILURES
---------- ------------------------------ ------------------- ------------------- ----------
       743 SCOTT                          2015-06-10 09:20:29 2015-06-10 09:21:29          0

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-1693812/,如需轉載,請註明出處,否則將追究法律責任。

相關文章