snaptime$$: 用于表示刷新时间。 dmltype$$: 用于表示dml操作类型,i表示insert,d表示delete,u表示update。 old_new$$: 用于表示这个值是新值还是旧值。n(ew)表示新值,o(ld)表示旧值,u表示update操作。 change_vector$$ 表示修改矢量,用来表示被修改的是哪个或哪几个字段。
1.
primary key drop table test_id; --删除表 create table test_id(id number,name varchar2(30),mark number); --创建表 alter table test_id add constraint pk_test_id primary key (id); --增加主键 drop materialized view log on test_id; --删除物化视图日志 create materialized view log on test_id tablespace ttts with primary key; --依据主键创建物化视图日志 --系统针对日志建表 sql> desc mlog$_test_id; name type nullable default comments --------------- ----------- -------- ------- -------- id number y 用主键记录发生dml操作的行 snaptime$$ date y snaptime$$列记录了刷新操作的时间。 dmltype$$ varchar2(1) y dmltype$$的记录值i、u和d,表示操作是insert、update还是delete。 old_new$$ varchar2(1) y old_new$$表示物化视图日志中保存的信息是dml操作之前的值(旧值)还是dml操作之后的值(新值)。除了o和n这两种类型外,对于update操作,还可能表示为u。 change_vector$$ raw(255) y change_vector$$记录dml操作发生在那个或那几个字段上 --当创建物化视图日志使用primary key时,oracle创建临时表 RUPD$_基础表 sql> desc rupd$_test_id; name type nullable default comments --------------- ----------- -------- ------- -------- id number y dmltype$$ varchar2(1) y snapid integer y change_vector$$ raw(255) y
2.
rowid drop table test_rowid; --删除表 create table test_rowid(id number,name varchar2(30),mark number); --创建表 drop materrialized view log on test_rowid; --create materialized view log on test_rowid with rowid, sequence (id, name) including new values ; create materialized view log on test_rowid with rowid;--依据rowid创建物化视图日志 sql> desc mlog$_test_rowid; name type nullable default comments --------------- ------------- -------- ------- -------- m_row$$ varchar2(255) y snaptime$$ date y dmltype$$ varchar2(1) y old_new$$ varchar2(1) y change_vector$$ raw(255) y
3.
object id create type test_object as object (id number, name varchar2(30), num number);--创建类型 create table test_objid of test_object; --创建表 create materialized view log on test_objid with object id;--依据object id创建物化视图日志 sql> desc mlog$_test_objid; name type nullable default comments --------------- ----------- -------- ------- -------- sys_nc_oid$ raw(16) y snaptime$$ date y dmltype$$ varchar2(1) y old_new$$ varchar2(1) y change_vector$$ raw(255) y
4.
sequence+rowid+(属性列) drop table test_sq; --删除表 create table test_sq(id number,name varchar2(30),mark number); --创建表 drop materialized view log on test_sq;--删除物化视图日志 create materialized view log on test_sq tablespace ttts with sequence; --依据sequence创建物化视图日志 --ora-12014: 表 'test_sq' 不包含主键约束条件 create materialized view log on test_sq with sequence (id, name,num) including new values;--包含基础表的所有列 --ora-12014: 表 'test_sq' 不包含主键约束条件 alter table test_sq add constraint uk_test_sq unique (id,name); --增加uk create materialized view log on test_sq with sequence (id,name) including new values; --ora-12014: 表 'test_sq' 不包含主键约束条件 即主键、rowid或object id用来唯一表示物化视图日志中的记录,sequence不能唯一标识记录,故不能单独用来建日志。 create materialized view log on test_sq with rowid,sequence (id, name) including new values ; sql> desc mlog$_test_sq; name type nullable default comments --------------- ------------- -------- ------- -------- id number y 建立物化视图时指明的列会在物化视图日志中进行记录。 name varchar2(30) y m_row$$ varchar2(255) y sequence$$ number y sequence会根据操作发生的顺序对物化视图日志中的记录编号。 snaptime$$ date y dmltype$$ varchar2(1) y old_new$$ varchar2(1) y change_vector$$ raw(255) y
insert into test_id values (1, 'a', 5); insert into test_rowid values (1, 'a', 5); insert into test_objid values (1, 'a', 5); insert into test_sq values (1, 'a', 5); commit;
update test_id set name = 'c' where id = 1; update test_rowid set name = 'c' where id = 1; update test_objid set name = 'c' where id = 1; update test_sq set name = 'c' where id = 1; commit;
delete test_id ; delete test_rowid; delete test_objid; delete test_sq ; commit;
create materialized view mv_test_id refresh fast on commit as select * from test_id; --commit时物化视图被刷新
create materialized view mv_test_rowid refresh fast as select name, count(*) from test_rowid group by name; --ORA-32401: "TT"."TEST_ROWID" 上的实体化视图日志没有新值 alter materialized view log on test_rowid add including new values; create materialized view mv_test_rowid refresh fast as select name, count(*) from test_rowid group by name; --ORA-12033: 不能使用 "TT"."TEST_ROWID" 上实体化视图日志中的过滤器列 alter materialized view log on test_rowid add (name); create materialized view mv_test_rowid refresh fast as select name, count(*) from test_rowid group by name;3.物化视图mv_test_objid
create materialized view mv_test_objid refresh fast as select * from test_objid; --ORA-12014: 表 'TEST_OBJID' 不包含主键约束条件 alter table test_objid add constraint pk_test_objid primary key (id); --增加主键 create materialized view mv_test_objid refresh fast as select * from test_objid; --ORA-23415: "TT"."TEST_OBJID" 的实体化视图日志不记录主键 alter materialized view log on test_objid add (id); alter materialized view log on test_objid add primary key (id); drop materialized view log on test_objid; create materialized view log on test_objid tablespace ttts with primary key including new values; create materialized view mv_test_objid refresh fast as select * from test_objid;
create materialized view mv_test_sq refresh fast as select name, count(*) from test_sq group by name; --需要用exec dbms_mview.refresh('mv_test_sq')来刷新
exec dbms_mview.refresh('mv_test_rowid'); exec dbms_mview.refresh('mv_test_objid'); exec dbms_mview.refresh('mv_test_sq');
refresh fast as 调用exec dbms_mview.refresh('mv_基本表')时物化视图刷新 refresh fast on commit as 在commit时物化视图刷新 refresh fast on demand 定时物化视图刷新 create materialized view mv_test_sq2 refresh fast on demand with rowid start with to_date('22-04-2011 16:30:01', 'dd-mm-yyyy hh24:mi:ss') next /*1:hrs*/ sysdate + 1/(24*60) as select id,count(*) from test_sq group by id;
alter materialized view log on test_rowid add including new values;--ORA-12033: 不能使用 "TT"."TEST_ROWID" 上实体化视图日志中的过滤器列
alter materialized view log on test_rowid add (name);
alter table test_objid add constraint pk_test_objid primary key (id); --增加主键
drop materialized view log on test_objid; create materialized view log on test_objid tablespace ttts with primary key including new values;
create {materialized view | snapshot} log on <tab> [tablespace <ts>] [storage (…)] [pctfree <10>] [pctused <40>] [initrans <1>] [maxtrans <n>] [logging | nologging] [cache | nocache] [noparallel | parallel [<n>]] [partition…] [lob…] [using index…] [with [primary key] [, rowid] [(<col> [, …])] ] [{including | excluding} new values]; alter {materialized view | snapshot} log on <tab> [add [primary key] [, rowid] [(<col> [, …])] ] […]; drop {materialized view | snapshot} log on <tab>; create {materialized view | snapshot} <mview>[tablespace <ts>] [storage (…)] [pctfree <10>] [pctused <40>] [initrans <1>] [maxtrans <n>] [logging | nologging] [cache | nocache] [noparallel | parallel [<n>]] [cluster <clust> (<col> [, …])] [lob…] [partition…] [build {immediate | deferred}] [on prebuilt table [{with | without} reduced precision]] [using index…] [ refresh [fast | complete | force] [on commit | on demand] [start with ‘<date>’] [next ‘<date>’] [with {primary key | rowid}] [using [default] [master | local] rollback segment [<rbs>]] ] | never refresh ] [for update] [{enable | disable} query rewrite] as <query>; alter {materialized view | snapshot} <mview> … [compile]; drop {materialized view | snapshot} <mview>;
connect pubr/bit@datasource ; drop materialized view log on pubr.allactive; --删除物化视图日志 create materialized view log on pubr.allactive tablespace logts with primary key; --创建物化视图日志 connect ttowb/bit; drop materialized view allactive_tt; --删除物化视图 create materialized view allactive_tt refresh fast as select ID,CATEGORY,FLOWID,MASTATUS,BASTATUS,APPLYDATETIME,CREATEDATETIME,COMMITDATETIME,BITSPNO,ARCHIVETIME, DESCRIPTION,OPERTYPE,ISVALID,INVALIDREASON,INVALIDDATETIME,INVALIDPNO,ACTIVETABLENAME,PARENTID,STANID,REALTYPEID, CORRECTID,to_date('1900-01-01') allactive_rtime from pubr.allactive@pubrowb; --创建物化视图