当前位置: 首页 > article >正文

利用物化视图刷新同步表记录

利用物化视图刷新同步表记录,适合目标端只需用来做查询的,并且需要同步的表少,比较灵活。

例如源库表t1,需要同步到目标库t2,t3,t4,测试如下:

SQL> show user;
USER is "JYC"
SQL> create table t1(id int,name varchar2(30));

Table created.

SQL> alter table t1 add constraint pk_t1 primary key(id) using index;

Table altered.

SQL> create materialized view log on t1 with primary key;

Materialized view log created.

SQL> create table t2 as select * from t1 where 1=2;

Table created.

SQL> CREATE MATERIALIZED VIEW  t2 on prebuilt table refresh fast on commit  as select * from t1;

Materialized view created.

SQL> insert into t1 values(1,'A');

1 row created.

SQL> select * from t2;

no rows selected

SQL> commit;

Commit complete.

SQL> select * from t2;

        ID NAME
---------- ------------------------------
         1 A

SQL> select * from t1;

        ID NAME
---------- ------------------------------
         1 A

SQL> insert into t1 values(2,'b');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t2;

        ID NAME
---------- ------------------------------
         1 A
         2 b

SQL> set long 99999
SQL> select dbms_metadata.get_ddl('MATERIALIZED_VIEW','T2') from dual;

DBMS_METADATA.GET_DDL('MATERIALIZED_VIEW','T2')
--------------------------------------------------------------------------------

  CREATE MATERIALIZED VIEW "JYC"."T2" ("ID", "NAME")
  ON PREBUILT TABLE WITHOUT REDUCED PRECISION
  USING INDEX
  REFRESH FAST ON COMMIT
  WITH PRIMARY KEY USING DEFAULT LOCAL ROLLBACK SEGMENT
  USING ENFORCED CONSTRAINTS DISABLE QUERY REWRITE
  AS SELECT "T1"."ID" "ID","T1"."NAME" "NAME" FROM "T1" "T1"


SQL> select dbms_metadata.get_ddl('TABLE','T2') from dual;

DBMS_METADATA.GET_DDL('TABLE','T2')
--------------------------------------------------------------------------------

  CREATE TABLE "JYC"."T2"
   (    "ID" NUMBER(*,0),
        "NAME" VARCHAR2(30)
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"

DBMS_METADATA.GET_DDL('TABLE','T2')
--------------------------------------------------------------------------------


SQL> SELECT STALENESS from user_mviews;

STALENESS
-------------------
UNKNOWN

SQL> /

STALENESS
-------------------
UNKNOWN

SQL>                                                                                              
SQL> 
SQL> 
SQL> 
SQL> 
SQL> 
SQL> create database link t1_t3 connect to jyc identified by jyc using '192.168.205.57:1521/lndb';

Database link created.

SQL> create table t3 as select * from t1 where 1=2;

Table created.

SQL> alter table t3 add constraint pk_t3 primary key(id) using index;

Table altered.

SQL> CREATE MATERIALIZED VIEW  t3 on prebuilt table refresh fast on commit as select * from t1;

Materialized view created.

SQL> select * from t3;

no rows selected

SQL> /

no rows selected

SQL> exec dbms_mview.refresh('T3','c');

PL/SQL procedure successfully completed.

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         1 A
         2 b

SQL> insert into t1 values(3,'c');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         1 A
         2 b
         3 c

SQL> select * from t2;

        ID NAME
---------- ------------------------------
         1 A
         2 b
         3 c

SQL> exec dbms_mview.refresh('T3','f');

PL/SQL procedure successfully completed.

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         1 A
         2 b
         3 c

SQL> insert into t4 values(4,'c');
insert into t4 values(4,'c')
            *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> insert into t3 values(4,'d');
insert into t3 values(4,'d')
            *
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view


SQL> insert into t2 values(4,'d');
insert into t2 values(4,'d')
            *
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view


SQL> insert into t1 values(4,'d');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         1 A
         2 b
         3 c
         4 d

SQL> select * from t2;

        ID NAME
---------- ------------------------------
         1 A
         2 b
         3 c
         4 d

SQL> delete from t1 where id=1;

1 row deleted.

SQL> commit;

Commit complete.

SQL> select * from t2;

        ID NAME
---------- ------------------------------
         2 b
         3 c
         4 d

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 c
         4 d

SQL> update t1 set name='a' where id=3;

1 row updated.

SQL> commit;

Commit complete.

SQL> select * from t2;

        ID NAME
---------- ------------------------------
         2 b
         4 d
         3 a

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         3 a
         2 b
         4 d

SQL> drop table t3;      
drop table t3
           *
ERROR at line 1:
ORA-12083: must use DROP MATERIALIZED VIEW to drop "JYC"."T3"


SQL> DROP MATERIALIZED VIEW t3;

Materialized view dropped.

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         3 a
         2 b
         4 d

SQL> drop table t3;

Table dropped.

SQL> create table t3 as select * from t1 where 1=2;

Table created.

SQL> alter table t3 add constraint pk_t3 primary key(id) using index;

Table altered.

SQL> CREATE MATERIALIZED VIEW  t3 on prebuilt table refresh fast on commit as select * from t1@t1_t3;
CREATE MATERIALIZED VIEW  t3 on prebuilt table refresh fast on commit as select * from t1@t1_t3
      *
ERROR at line 1:
ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view


SQL> select * from t1@t1_t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d

SQL> !oerr ora 12054
12054, 00000, "cannot set the ON COMMIT refresh attribute for the materialized view"
// *Cause:  The materialized view did not satisfy conditions for refresh at
//          commit time.
// *Action: Specify only valid options.
//

SQL> CREATE MATERIALIZED VIEW  t3 on prebuilt table refresh fast on demand as select * from t1@t1_t3;

Materialized view created.

SQL> select * from t3;

no rows selected

SQL> exec dbms_mview.refresh('T3','f');

PL/SQL procedure successfully completed.

SQL> select * from t3;

no rows selected

SQL> exec dbms_mview.refresh('T3','c');

PL/SQL procedure successfully completed.

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d

SQL> insert into t1 values(5,'e');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t2;

        ID NAME
---------- ------------------------------
         2 b
         4 d
         3 a
         5 e

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d

SQL> exec dbms_mview.refresh('T3','f');

PL/SQL procedure successfully completed.

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e

SQL> create table t4 as select * from t1 where 1=2;

Table created.

SQL> alter table t4 add constraint pk_t4 primary key(id) using index;

Table altered.

SQL> CREATE MATERIALIZED VIEW  t4 on prebuilt table refresh fast as select * from t1@t1_t3;

Materialized view created.

SQL> select * from t4;

no rows selected

SQL> exec dbms_mview.refresh('T4','f');

PL/SQL procedure successfully completed.

SQL> select * from t4;

no rows selected

SQL> exec dbms_mview.refresh('T4','c');

PL/SQL procedure successfully completed.

SQL>  select * from t4;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e

SQL> insert into t1 values(6,'f');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t4;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e

SQL> exec dbms_mview.refresh('T4','f');

PL/SQL procedure successfully completed.

SQL> select * from t4;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e
         6 f

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e

SQL> exec dbms_mview.refresh('T3','f');

PL/SQL procedure successfully completed.

SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e
         6 f

SQL> 
SQL> --设定刷新任务T4表,每分钟刷新一次
SQL> 
SQL> declare jobid number;
  2   begin
  3   sys.dbms_job.submit(job => jobid,
  4   what => 'dbms_mview.refresh(''T4'');',
  5   next_date => sysdate,
  6   interval => 'sysdate+1/1440');
  7   commit;
  8   end;
  9  /

PL/SQL procedure successfully completed.

--设定job任务

declare jobid number;
 begin
 sys.dbms_job.submit(job => jobid,
 what => 'dbms_mview.refresh(''T4'');',
 next_date => sysdate,
 interval => 'sysdate+1/1440');
 commit;
 end;
/

SQL> set time on
15:16:12 SQL> insert into t1 values(1,'a');

1 row created.

15:16:25 SQL> commit;

Commit complete.

15:16:27 SQL> 
15:16:28 SQL> select * from t4;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e
         6 f

15:16:37 SQL> /

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e
         6 f

15:16:52 SQL> /

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e
         6 f

15:17:02 SQL> /

        ID NAME
---------- ------------------------------
         1 a
         2 b
         3 a
         4 d
         5 e
         6 f

6 rows selected.

15:17:26 SQL> select * from t4;

        ID NAME
---------- ------------------------------
         1 a
         2 b
         3 a
         4 d
         5 e
         6 f

6 rows selected.

15:17:38 SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e
         6 f

15:17:42 SQL> 

15:17:42 SQL>     
15:28:31 SQL> create table t5 as select * from t1 where 1=2;

Table created.

15:28:45 SQL> alter table t5 add constraint pk_t5 primary key(id) using index;

Table altered.

15:29:04 SQL> create  materialized view t5 refresh force on demand start with sysdate next sysdate+1/1440 with rowid enable query rewrite as select * from  t1@t1_t3;
create  materialized view t5 refresh force on demand start with sysdate next sysdate+1/1440 with rowid enable query rewrite as select * from  t1@t1_t3
                                                                                                                                              *
ERROR at line 1:
ORA-00955: name is already used by an existing object


15:30:41 SQL> !oerr ora 955
00955, 00000, "name is already used by an existing object"
// *Cause:
// *Action:

15:30:53 SQL> 
15:31:59 SQL> create  materialized view t5 on prebuilt table refresh force on demand start with sysdate next sysdate+1/1440 with rowid enable query rewrite as select * from t1@t1_t3;
create  materialized view t5 on prebuilt table refresh force on demand start with sysdate next sysdate+1/1440 with rowid enable query rewrite as select * from t1@t1_t3
      *
ERROR at line 1:
ORA-12058: materialized view cannot use prebuilt table


15:32:01 SQL> 
15:32:40 SQL> 
15:32:40 SQL> create  materialized view t5 refresh force on demand start with sysdate next sysdate+1/1440 as select * from t1@t1_t3;
create  materialized view t5 refresh force on demand start with sysdate next sysdate+1/1440 as select * from t1@t1_t3
                                                                                                             *
ERROR at line 1:
ORA-00955: name is already used by an existing object


15:32:41 SQL> 
15:33:27 SQL> create  materialized view t5 on prebuilt table refresh fast on demand start with sysdate next sysdate+1/1440 as select * from t1@t1_t3;

Materialized view created.

15:33:28 SQL> select * from t5;

no rows selected

15:33:35 SQL> /

no rows selected

15:33:44 SQL> /

no rows selected

15:33:57 SQL> insert into t1 values(7,'g');

1 row created.

15:34:17 SQL> commit;

Commit complete.

15:34:19 SQL> select * from t5;

no rows selected

15:34:22 SQL> select * from t4;

        ID NAME
---------- ------------------------------
         1 a
         2 b
         3 a
         4 d
         5 e
         6 f

6 rows selected.

15:34:26 SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e
         6 f

15:34:34 SQL> select * from t4;

        ID NAME
---------- ------------------------------
         1 a
         2 b
         3 a
         4 d
         5 e
         6 f

6 rows selected.

15:34:44 SQL> select * from t4;

        ID NAME
---------- ------------------------------
         1 a
         2 b
         3 a
         4 d
         5 e
         6 f

6 rows selected.

15:35:09 SQL> select * from t5;

        ID NAME
---------- ------------------------------
         7 g

15:35:16 SQL> exec dbms_mview.refresh('T5','c');

PL/SQL procedure successfully completed.

15:35:44 SQL> select * from t5;

        ID NAME
---------- ------------------------------
         6 f
         2 b
         3 a
         4 d
         5 e
         1 a
         7 g

7 rows selected.

15:35:49 SQL> select * from t4;

        ID NAME
---------- ------------------------------
         1 a
         7 g
         2 b
         3 a
         4 d
         5 e
         6 f

7 rows selected.

15:35:53 SQL> delete from t1 where id>3;

4 rows deleted.

15:36:26 SQL> commit;

Commit complete.

15:36:28 SQL> select * from t5;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         1 a

15:37:06 SQL> select * from t4;

        ID NAME
---------- ------------------------------
         1 a
         2 b
         3 a

15:37:15 SQL> 
15:37:22 SQL>               
15:37:26 SQL> select * from t2;

        ID NAME
---------- ------------------------------
         2 b
         1 a
         3 a

15:37:31 SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         4 d
         5 e
         6 f

15:37:41 SQL> exec dbms_mview.refresh('T3');

PL/SQL procedure successfully completed.

15:37:53 SQL> select * from t3;

        ID NAME
---------- ------------------------------
         2 b
         3 a
         1 a

15:37:58 SQL> 

相关参考:https://www.cnblogs.com/fyq891014/archive/2012/05/01/3294823.html

使用物化视图同步表.txt - 墨天轮文档 


http://www.kler.cn/a/302392.html

相关文章:

  • 【FFmpeg】FFmpeg 函数简介 ③ ( 编解码相关函数 | FFmpeg 源码地址 | FFmpeg 解码器相关 结构体 和 函数 )
  • 浅谈:基于三维场景的视频融合方法
  • 向日葵软件Windows系统连接苹果系统(MacOS)的无反应问题解决办法
  • 前端:块级元素和行内元素
  • 结构体(c语言)
  • 类别变量分析——卡方独立性检验卡方拟合优度检验
  • 从概念到现实,国际数字影像产业园如何打造数字文创产业标杆?
  • Android 开发避坑经验(2):深入理解Fragment与Activity交互
  • 宽哥之家小程序任务脚本
  • 服务器深度解析:五大关键问题一网打尽
  • CentOS 7 上安装 Docker
  • 【Three.js】实现护罩(防御罩、金钟罩、护盾)效果
  • 【PGCCC】PostgreSQL重做日志内幕!如何掌握事务日志记录的“黑魔法”
  • 9月13日星期五今日早报简报微语报早读
  • ssm“健康早知道”微信小程序 LW PPT源码调试讲解
  • P1544 三倍经验 (记忆化搜索)
  • SpringBoot 整合 Guava Cache 实现本地缓存
  • 算法day23| 93.复原IP地址、78.子集、90.子集II
  • 数据库安全性控制
  • 深入MySQL的索引实践及优化
  • 【开源风云】从若依系列脚手架汲取编程之道(四)
  • 【自然语言处理】实验一:基于NLP工具的中文分词
  • yolov5实战全部流程
  • 【Hot100】LeetCode—64. 最小路径和
  • GaN挑战Si价格底线?英飞凌推出全球首个12英寸GaN晶圆技术
  • 高效+灵活,万博智云全球发布AWS无代理跨云容灾方案!