ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 实现跨库可更新物化视图

实现跨库可更新物化视图

原创 Linux操作系统 作者:Tomthe 时间:2012-04-06 15:28:32 0 删除 编辑
物化视图较为常见的一种用法是,远程数据库中存放的基表数据作为本地数据库的更新数据源,以便实现多个数据库与同一个数据源进行同步。这便是一种数据分发的表现形式。
  这里给出这种实现这种跨库可更新的物化视图的方法。要求在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/20577218/viewspace-720550/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-06-16

  • 博文量
    15
  • 访问量
    23263