【MV】實現跨庫可更新物化檢視

secooler發表於2011-05-20
  物化檢視較為常見的一種用法是,遠端資料庫中存放的基表資料作為本地資料庫的更新資料來源,以便實現多個資料庫與同一個資料來源進行同步。這便是一種資料分發的表現形式。
  這裡給出這種實現這種跨庫的可更新的物化檢視的方法。要求在secdb資料庫例項上建立可更新的物化檢視mv_t,物化檢視對應的基表資料存放在另外一套資料庫例項secgc中。

1.首先在secgc資料庫例項上建立基表T
secdb@secdb1 /home/oracle$ export ORACLE_SID=secgc
secgc@secdb1 /home/oracle$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Fri May 20 20:24:43 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> create user user_secgc identified by user_secgc;

User created.

SQL> grant connect,resource to user_secgc;

Grant succeeded.

SQL> conn user_secgc/user_secgc
Connected.
SQL> create table t (x varchar2(10));

Table created.

SQL> insert into t values ('secooler');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t;

X
----------
secooler

2.在T表所在例項secgc上建立物化檢視日誌
SQL> create materialized view log on t with rowid;

Materialized view log created.

3.在物化檢視所在例項secdb上建立database link
secgc@secdb1 /home/oracle$ export ORACLE_SID=secdb
1)調整global_names引數為FALSE
此步驟是為了防止在使用database link時報ORA-02085錯誤。關於ORA-02085錯誤請參考文章《【DBLINK】“ORA-02085: database link %s connects to %s”故障排查及處理策略兩則》(http://space.itpub.net/519536/viewspace-695851)。
secdb@secdb1 /home/oracle$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Fri May 20 20:31:06 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> show parameter global_names

NAME                                 TYPE        VALUE
------------------------------------ ----------- -------------
global_names                         boolean     TRUE
SQL> alter system set global_names=FALSE;

System altered.

SQL> show parameter global_names

NAME                                 TYPE        VALUE
------------------------------------ ----------- -------------
global_names                         boolean     FALSE

2)建立連線到secgc例項的database link
(1)在本地secdb例項上建立到secgc例項的連線串
secdb@secdb1 /home/oracle$ vi $ORACLE_HOME/network/admin/tnsnames.ora
SECGC =
  (DESCRIPTION =
    (address = (protocol = tcp)(host = secdb1)(port = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = secgc)
    )
  )

(2)建立database link
SQL> create public database link dblink_to_secgc connect to user_secgc identified by user_secgc using 'SECGC';

Database link created.

4.在secdb例項上使用者user_secdb下建立物化檢視
SQL> create user user_secdb identified by user_secdb;

User created.

SQL> grant connect,resource to user_secdb;

Grant succeeded.

SQL> conn user_secdb/user_secdb
Connected.

SQL> create materialized view mv_t refresh fast with rowid as select * from t@dblink_to_secgc;

Materialized view created.

5.對物化檢視進行快速重新整理和完全重新整理
1)快速重新整理方法
SQL> exec dbms_mview.refresh('mv_t','f');

PL/SQL procedure successfully completed.

2)完全重新整理方法
SQL> exec dbms_mview.refresh('mv_t','c');

PL/SQL procedure successfully completed.

6.測試物化檢視重新整理效果
1)在基表T中插入一條資料
secdb@secdb1 /home/oracle$ export ORACLE_SID=secgc
secgc@secdb1 /home/oracle$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Fri May 20 21:09:03 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> conn user_secgc/user_secgc
Connected.
SQL> select * from t;

X
----------
secooler

SQL> insert into t values ('Andy');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t;

X
----------
secooler
Andy

2)對物化檢視進行快速重新整理(或完全重新整理)前資料
SQL> select * from mv_t;

X
----------
secooler

3)對物化檢視進行快速重新整理(或完全重新整理)後資料
SQL> exec dbms_mview.refresh('mv_t','f');

PL/SQL procedure successfully completed.

SQL> select * from mv_t;

X
----------
secooler
Andy

物化檢視建立成功。

7.小結
  透過一系列配置,我們便實現了跨庫可更新物化檢視。這裡我們使用Database Link技術達到了“跨庫”這個目的。

Good luck.

secooler
11.05.20

-- The End --

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

相關文章