首页 > 代码库 > MATERIALIZED VIEW
MATERIALIZED VIEW
Oracle的物化视图增量刷新机制定时将数据库A上的某个表的数据同步到另一个数据库B上
首先在数据库A上确认该表有主键,然后建立在这个表上建立物化视图日志如
“CREATE MATERIALIZED VIEW LOG ON T_tablename;”
再到数据库B上创建数据库链接和快速刷新的物化视图如“
create materialized view mv_tablename refresh fast on demand start with sysdate next sysdate+1/288 as select * from T_tablename@dblink_name;”
然后赋权
grant CREATE PUBLIC DATABASE LINK,DROP PUBLIC DATABASE LINK to scott;
grant CREATE MATERIALIZED VIEW to scott;
eg.
库A
--scott_a
create table T_MV_TEST ( A DATE, B DATE, C DATE );
alter table T_MV_TEST
add constraint PK_T_MV_TEST primary key (A,B);
CREATE MATERIALIZED VIEW LOG ON T_MV_TEST ;
但是这个日志表只能捕获到主键字段的删除和增加操作,如果非主键字段值发生改变,则不会同步过去
库B
--scott_b
在数据库B上创建定时增量更新的物化视图,方法如下:
-- Drop existing database link
drop public database link connA;
-- Create database link
create public database link connA
connect to connA
using ‘(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1522)) (CONNECT_DATA = http://www.mamicode.com/(SERVER = DEDICATED) (SERVICE_NAME = >
这是增量更新机制的物化视图的最简单的创建方法,定时检查数据库A上此表的主键字段的删除和插入操作而将变化的记录同步到另一个数据库B中
create materialized view T_MV_TEST REFRESH fast on demand START with sysdate next sysdate+1/1440
AS SELECT * from T_MV_TEST@A
select * from T_MV_TEST