﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-范范｀blog-文章分类-oracle</title><link>http://www.blogjava.net/fyq891014/category/51579.html</link><description>热爱生活，热爱编程！</description><language>zh-cn</language><lastBuildDate>Sun, 29 Apr 2012 00:53:54 GMT</lastBuildDate><pubDate>Sun, 29 Apr 2012 00:53:54 GMT</pubDate><ttl>60</ttl><item><title>oracle物化视图</title><link>http://www.blogjava.net/fyq891014/articles/377010.html</link><dc:creator>执着那份情</dc:creator><author>执着那份情</author><pubDate>Sun, 29 Apr 2012 00:38:00 GMT</pubDate><guid>http://www.blogjava.net/fyq891014/articles/377010.html</guid><wfw:comment>http://www.blogjava.net/fyq891014/comments/377010.html</wfw:comment><comments>http://www.blogjava.net/fyq891014/articles/377010.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/fyq891014/comments/commentRss/377010.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/fyq891014/services/trackbacks/377010.html</trackback:ping><description><![CDATA[<div>一、oracle物化视图基本概念</div><div>&nbsp;物化视图首先需要创建物化视图日志,</div><div>&nbsp;oracle依据用户创建的物化视图日志来创建物化视图日志表，</div><div>&nbsp;物化视图日志表的名称为mlog$_后面跟基表的名称，</div><div>&nbsp;如果表名的长度超过20位，则只取前20位，当截短后出现名称重复时，oracle会自动在物化视图日志名称后面加上数字作为序号。</div><div>&nbsp;创建物化视图日志在建立时有多种选项：可以指定为rowid、primary key和object id几种类型，同时还可以指定sequence或明确指定列名。</div><div>&nbsp;上面这些情况产生的物化视图日志的结构都不相同。</div><div>&nbsp;任何物化视图都会包括的列：</div><div>&nbsp; snaptime$$：用于表示刷新时间。</div><div>&nbsp; dmltype$$：用于表示dml操作类型，i表示insert，d表示delete，u表示update。</div><div>&nbsp; old_new$$：用于表示这个值是新值还是旧值。n（ew）表示新值，o（ld）表示旧值，u表示update操作。</div><div>&nbsp; change_vector$$表示修改矢量，用来表示被修改的是哪个或哪几个字段。</div><div>&nbsp;如果with后面跟了primary key，则物化视图日志中会包含主键列。</div><div>&nbsp;如果with后面跟了rowid，则物化视图日志中会包含： m_row$$：用来存储发生变化的记录的rowid。</div><div>&nbsp;如果with后面跟了object id，则物化视图日志中会包含：sys_nc_oid$：用来记录每个变化对象的对象id。</div><div>&nbsp;如果with后面跟了sequence，则物化视图日子中会包含：sequence$$：给每个操作一个sequence号，从而保证刷新时按照顺序进行刷新。</div><div>&nbsp;如果with后面跟了一个或多个column名称，则物化视图日志中会包含这些列。</div><div></div><div>二、oracle物化视图日志</div><div>&nbsp;1.primary key</div><div>&nbsp; drop table test_id; &nbsp; &nbsp;--删除表</div><div>&nbsp; create table test_id(id number,name varchar2(30),mark number); --创建表</div><div>&nbsp; alter table test_id add constraint pk_test_id primary key (id); &nbsp;--增加主键</div><div>&nbsp; drop materialized view log on test_id;--删除物化视图日志</div><div>&nbsp; create materialized view log on test_id tablespace ttts with primary key; --依据主键创建物化视图日志</div><div>&nbsp; --系统针对日志建表</div><div>&nbsp; sql&gt; desc mlog$_test_id;</div><div>&nbsp; name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type &nbsp; &nbsp; &nbsp; &nbsp;nullable default comments</div><div>&nbsp; --------------- ----------- -------- ------- --------</div><div>&nbsp; id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;number &nbsp; &nbsp; &nbsp;y &nbsp; &nbsp; &nbsp; &nbsp;用主键记录发生dml操作的行</div><div>&nbsp; snaptime$$ &nbsp; &nbsp; &nbsp;date &nbsp; &nbsp; &nbsp; &nbsp;y &nbsp; &nbsp; &nbsp; &nbsp;snaptime$$列记录了刷新操作的时间。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; dmltype$$ &nbsp; &nbsp; &nbsp; varchar2(1) y &nbsp; &nbsp; &nbsp; &nbsp;dmltype$$的记录值i、u和d，表示操作是insert、update还是delete。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; old_new$$ &nbsp; &nbsp; &nbsp; varchar2(1) y &nbsp; &nbsp; &nbsp; &nbsp;old_new$$表示物化视图日志中保存的信息是dml操作之前的值（旧值）还是dml操作之后的值（新值）。除了o和n这两种类型外，对于update操作，还可能表示为u。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; change_vector$$ raw(255) &nbsp; &nbsp;y &nbsp; &nbsp; &nbsp; &nbsp;change_vector$$记录dml操作发生在那个或那几个字段上</div><div>&nbsp; --当创建物化视图日志使用primary key时，oracle创建临时表 RUPD$_基础表</div><div>&nbsp; sql&gt; desc rupd$_test_id;</div><div>&nbsp; name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type &nbsp; &nbsp; &nbsp; &nbsp;nullable default comments</div><div>&nbsp; --------------- ----------- -------- ------- --------</div><div>&nbsp; id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;number &nbsp; &nbsp; &nbsp;y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; dmltype$$ &nbsp; &nbsp; &nbsp; varchar2(1) y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; snapid &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;integer &nbsp; &nbsp; y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; change_vector$$ raw(255) &nbsp; &nbsp;y &nbsp;</div><div></div><div>&nbsp;2.rowid</div><div>&nbsp; drop table test_rowid; &nbsp; &nbsp;--删除表</div><div>&nbsp; create table test_rowid(id number,name varchar2(30),mark number); --创建表</div><div>&nbsp; drop materrialized view log on test_rowid;</div><div>&nbsp; --create materialized view log on test_rowid with rowid, sequence (id, name) including new values ;</div><div>&nbsp; create materialized view log on test_rowid with rowid;--依据rowid创建物化视图日志</div><div></div><div>&nbsp; sql&gt; desc mlog$_test_rowid;</div><div>&nbsp; name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nullable default comments</div><div>&nbsp; --------------- ------------- -------- ------- --------</div><div>&nbsp; m_row$$ &nbsp; &nbsp; &nbsp; &nbsp; varchar2(255) y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; snaptime$$ &nbsp; &nbsp; &nbsp;date &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; dmltype$$ &nbsp; &nbsp; &nbsp; varchar2(1) &nbsp; y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; old_new$$ &nbsp; &nbsp; &nbsp; varchar2(1) &nbsp; y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; change_vector$$ raw(255) &nbsp; &nbsp; &nbsp;y</div><div></div><div>&nbsp;3.object id</div><div>&nbsp; create type test_object as object (id number, name varchar2(30), num number);--创建类型</div><div>&nbsp; create table test_objid of test_object; --创建表</div><div>&nbsp; create materialized view log on test_objid with object id;--依据object id创建物化视图日志</div><div>&nbsp; sql&gt; desc mlog$_test_objid;</div><div>&nbsp; name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type &nbsp; &nbsp; &nbsp; &nbsp;nullable default comments</div><div>&nbsp; --------------- ----------- -------- ------- --------</div><div>&nbsp; sys_nc_oid$ &nbsp; &nbsp; raw(16) &nbsp; &nbsp; y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; snaptime$$ &nbsp; &nbsp; &nbsp;date &nbsp; &nbsp; &nbsp; &nbsp;y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; dmltype$$ &nbsp; &nbsp; &nbsp; varchar2(1) y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; old_new$$ &nbsp; &nbsp; &nbsp; varchar2(1) y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; change_vector$$ raw(255) &nbsp; &nbsp;y&nbsp;</div><div></div><div>&nbsp;4.sequence+rowid+(属性列)</div><div>&nbsp; drop table test_sq; &nbsp; &nbsp;--删除表</div><div>&nbsp; create table test_sq(id number,name varchar2(30),mark number); --创建表</div><div>&nbsp; drop materialized view log on test_sq;--删除物化视图日志</div><div>&nbsp; create materialized view log on test_sq tablespace ttts with sequence; --依据sequence创建物化视图日志</div><div>&nbsp; --ora-12014: 表 'test_sq' 不包含主键约束条件</div><div>&nbsp; create materialized view log on test_sq with sequence (id, name,num) including new values;--包含基础表的所有列</div><div>&nbsp; --ora-12014: 表 'test_sq' 不包含主键约束条件</div><div>&nbsp; alter table test_sq add constraint uk_test_sq unique (id,name); &nbsp;--增加uk</div><div>&nbsp; create materialized view log on test_sq with sequence (id,name) including new values;</div><div>&nbsp; --ora-12014: 表 'test_sq' 不包含主键约束条件</div><div>&nbsp; 即主键、rowid或object id用来唯一表示物化视图日志中的记录，sequence不能唯一标识记录，故不能单独用来建日志。</div><div>&nbsp; create materialized view log on test_sq with rowid,sequence (id, name) including new values ;</div><div>&nbsp; sql&gt; desc mlog$_test_sq;</div><div>&nbsp; name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nullable default comments</div><div>&nbsp; --------------- ------------- -------- ------- --------</div><div>&nbsp; id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;number &nbsp; &nbsp; &nbsp; &nbsp;y &nbsp; &nbsp;建立物化视图时指明的列会在物化视图日志中进行记录。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;varchar2(30) &nbsp;y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; m_row$$ &nbsp; &nbsp; &nbsp; &nbsp; varchar2(255) y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; sequence$$ &nbsp; &nbsp; &nbsp;number &nbsp; &nbsp; &nbsp; &nbsp;y &nbsp; &nbsp;sequence会根据操作发生的顺序对物化视图日志中的记录编号。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; snaptime$$ &nbsp; &nbsp; &nbsp;date &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; dmltype$$ &nbsp; &nbsp; &nbsp; varchar2(1) &nbsp; y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; old_new$$ &nbsp; &nbsp; &nbsp; varchar2(1) &nbsp; y &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; change_vector$$ raw(255) &nbsp; &nbsp; &nbsp;y&nbsp;</div><div></div><div>三、oracle物化视图日志表</div><div>&nbsp;基础表：test_id，test_rowid，test_objid，test_sq</div><div>&nbsp;日志表：mlog$_test_id，mlog$_test_rowid，mlog$_test_objid，mlog$_test_sq</div><div>&nbsp;1.新增</div><div>&nbsp; insert into test_id &nbsp; &nbsp;values (1, 'a', 5);</div><div>&nbsp; insert into test_rowid values (1, 'a', 5);</div><div>&nbsp; insert into test_objid values (1, 'a', 5);</div><div>&nbsp; insert into test_sq &nbsp; &nbsp;values (1, 'a', 5);</div><div>&nbsp; commit;</div><div>&nbsp;2.修改</div><div>&nbsp; update test_id &nbsp; &nbsp;set name = 'c' where id = 1;</div><div>&nbsp; update test_rowid set name = 'c' where id = 1;</div><div>&nbsp; update test_objid set name = 'c' where id = 1;</div><div>&nbsp; update test_sq &nbsp; &nbsp;set name = 'c' where id = 1;</div><div>&nbsp; commit;</div><div>&nbsp;3.删除</div><div>&nbsp; delete test_id &nbsp; ;</div><div>&nbsp; delete test_rowid;</div><div>&nbsp; delete test_objid;</div><div>&nbsp; delete test_sq &nbsp; ;</div><div>&nbsp; commit;</div><div>&nbsp;在每一步commit后查看日志表记录。</div><div></div><div>四、oracle物化视图日志表字段取值解析</div><div>&nbsp;1.snaptime$$</div><div>&nbsp; 当基本表发生dml操作时，会记录到物化视图日志中，这时指定的时间4000年1月1日0时0分0秒（物化视图未被刷新）。</div><div>&nbsp; 如果物化视图日志供多个物化视图使用，则一个物化视图刷新后会将它刷新的记录的时间更新为它刷新的时间。</div><div>&nbsp; 只有建立快速刷新的物化视图才能使用物化视图日志，如果只建立一个物化视图，则物化视图刷新完会将物化视图日志清除掉</div><div>&nbsp;2.dmltype$$</div><div>&nbsp; 操作类型比较简单：只包括i(insert)、d(delete)和u(update)三种。</div><div>&nbsp;3.old_new$$</div><div>&nbsp; 新旧值也包括三种：o表示旧值（一般对应的操作时delete）、n表示新值（一般对应的操作是insert），还有一种u（对应update操作）。&nbsp;</div><div>&nbsp; 需要注意，对于基于主键的物化视图日志，如果更新了主键，则update操作转化为一条delete操作，一条insert操作。最后是delete操作。&nbsp;</div><div>&nbsp; 唯一的区别是每条update操作都对应物化视图日志中的两条记录。</div><div>&nbsp; 一条对应update操作的原记录dmltype$$和old_new$$都为u，一条对应update操作后的新记录，dmltype$$为u，old_new$$为n。</div><div>&nbsp; 当建立物化视图日志时指出了including new values语句时，就会出现这种情况。&nbsp;</div><div>&nbsp;4.change_vector$$</div><div>&nbsp; 最后简单讨论一下change_vector$$列。</div><div>&nbsp; insert和delete操作都是记录集的，即insert和delete会影响整条记录。</div><div>&nbsp; 而update操作是字段集的，update操作可能会更新整条记录的所有字段，也可能只更新个别字段。</div><div>&nbsp; 无论从性能上考虑还是从数据的一致性上考虑，物化视图刷新时都应该是基于字段集。</div><div>&nbsp; oracle就是通过change_vector$$列来记录每条记录发生变化的字段包括哪些。</div><div>&nbsp; 基于主键、rowid和object id的物化视图日志在change_vector$$上略有不同，但是总体设计的思路是一致的。</div><div>&nbsp; change_vector$$列是raw类型，其实oracle采用的方式就是用每个bit位去映射一个列。</div><div>&nbsp; 比如：第一列被更新设置为02，即00000010。</div><div>&nbsp; 第二列设置为04，即00000100，</div><div>&nbsp; 第三列设置为08，即00001000。</div><div>&nbsp; 当第一列和第二列同时被更新，则设置为06，00000110。</div><div>&nbsp; 如果三列都被更新，设置为0e，00001110。</div><div>&nbsp; 依此类推，第4列被更新时为0x10，第5列0x20，第6列0x40，第7列0x80，第8列0x100。</div><div>&nbsp; 当第1000列被更新时，change_vector$$的长度为1000/4+2为252。</div><div></div><div>&nbsp; 除了可以表示update的字段，还可以表示insert和delete。delete操作change_vector$$列为全0，具体个数由基表的列数决定。</div><div>&nbsp; insert操作的最低位为fe，如果基表列数较多，而存在高位的话，所有的高位都为ff。</div><div>&nbsp; 如果insert操作是前面讨论过的由update操作更新了主键造成的，则这个insert操作对应的change_vector$$列为全ff。</div><div></div><div>&nbsp; 可以看到，正如上面分析的，insert为fe，delete为00，对第一列的更新为02，第二列为04，第二列和第三列都更新为0c。需要注意，正常情况下，第一列会从02开始。</div><div>&nbsp; 但是如果对mlog$表执行了truncate操作，或者重建了物化视图日志，则可能造成第一列开始位置发生偏移。</div><div>&nbsp; 这个结果和rowid类型基本一致，不同的是，如果更新了主键，会将update操作在物化视图日志中记录为一条delete和一条insert，不过这时insert对应的change_vector$$的值是ff。</div><div>&nbsp; 这个结果也和rowid类型基本一致，需要注意的是，由于对象表包含两个隐含列，因此id不再是第一个字段，而是第三个，因此对应的值是08。</div><div>&nbsp; 最后看一个包含列数较多的例子，唯一需要注意的是，低位在左，高位在右。</div><div></div><div>五、oracle物化视图</div><div>&nbsp;1.物化视图mv_test_id</div><div>&nbsp; create materialized view mv_test_id refresh fast on commit as</div><div>&nbsp; &nbsp; select * from test_id; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --commit时物化视图被刷新</div><div>&nbsp;2.物化视图mv_test_rowid</div><div>&nbsp; create materialized view mv_test_rowid refresh fast as</div><div>&nbsp; &nbsp; select name, count(*) from test_rowid group by name;</div><div>&nbsp; &nbsp; --ORA-32401: "TT"."TEST_ROWID" 上的实体化视图日志没有新值</div><div>&nbsp; &nbsp; alter materialized view log on test_rowid add including new values;</div><div>&nbsp; create materialized view mv_test_rowid refresh fast as</div><div>&nbsp; &nbsp; select name, count(*) from test_rowid group by name;</div><div>&nbsp; &nbsp; --ORA-12033: 不能使用 "TT"."TEST_ROWID" 上实体化视图日志中的过滤器列</div><div>&nbsp; &nbsp; alter materialized view log on test_rowid add (name);&nbsp;</div><div>&nbsp; create materialized view mv_test_rowid refresh fast as</div><div>&nbsp; &nbsp; select name, count(*) from test_rowid group by name;</div><div>&nbsp;3.物化视图mv_test_objid</div><div>&nbsp; create materialized view mv_test_objid refresh fast as</div><div>&nbsp; &nbsp; select * from test_objid;</div><div>&nbsp; &nbsp; --ORA-12014: 表 'TEST_OBJID' 不包含主键约束条件</div><div>&nbsp; alter table test_objid add constraint pk_test_objid primary key (id); &nbsp;--增加主键&nbsp;</div><div>&nbsp; create materialized view mv_test_objid refresh fast as</div><div>&nbsp; &nbsp; select * from test_objid;</div><div>&nbsp; &nbsp; --ORA-23415: "TT"."TEST_OBJID" 的实体化视图日志不记录主键 &nbsp;&nbsp;</div><div>&nbsp; alter materialized view log on test_objid add (id);&nbsp;</div><div>&nbsp; alter materialized view log on test_objid add primary key (id);&nbsp;</div><div>&nbsp; drop materialized view &nbsp;log on test_objid;</div><div>&nbsp; create materialized view log on test_objid tablespace ttts with primary key including new values;</div><div>&nbsp; create materialized view mv_test_objid refresh fast as</div><div>&nbsp; &nbsp; select * from test_objid; &nbsp;&nbsp;</div><div>&nbsp;4.物化视图mv_test_sq</div><div>&nbsp; create materialized view mv_test_sq refresh fast as</div><div>&nbsp; &nbsp; select name, count(*) from test_sq group by name; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--需要用exec dbms_mview.refresh('mv_test_sq')来刷新</div><div></div><div>&nbsp;5.物化视图刷新</div><div>&nbsp; exec dbms_mview.refresh('mv_test_rowid');</div><div>&nbsp; exec dbms_mview.refresh('mv_test_objid');</div><div>&nbsp; exec dbms_mview.refresh('mv_test_sq');</div><div>&nbsp;物化视图刷新后日志表记录被清空。</div><div>&nbsp; refresh fast as &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 调用exec dbms_mview.refresh('mv_基本表')时物化视图刷新</div><div>&nbsp; refresh fast on commit as &nbsp; 在commit时物化视图刷新</div><div>&nbsp; refresh fast on demand &nbsp; &nbsp; &nbsp;定时物化视图刷新</div><div>&nbsp; &nbsp;create materialized view mv_test_sq2 refresh fast on demand</div><div>&nbsp; &nbsp;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)</div><div>&nbsp; &nbsp;as select id,count(*) from test_sq group by id;</div><div></div><div>六、错误提示：</div><div>&nbsp;--ORA-32401: "TT"."TEST_ROWID" 上的实体化视图日志没有新值</div><div>&nbsp;alter materialized view log on test_rowid add including new values;</div><div>&nbsp;--ORA-12033: 不能使用 "TT"."TEST_ROWID" 上实体化视图日志中的过滤器列</div><div>&nbsp;alter materialized view log on test_rowid add (name);&nbsp;</div><div>&nbsp;--ORA-12014: 表 'TEST_OBJID' 不包含主键约束条件</div><div>&nbsp;alter table test_objid add constraint pk_test_objid primary key (id); &nbsp;--增加主键 &nbsp;&nbsp;</div><div>&nbsp;--ORA-23415: "TT"."TEST_OBJID" 的实体化视图日志不记录主键</div><div>&nbsp;drop materialized view &nbsp;log on test_objid;</div><div>&nbsp;create materialized view log on test_objid tablespace ttts with primary key including new values;</div><div></div><div>七、相关语法：</div><div>&nbsp;create {materialized view | snapshot} log on &lt;tab&gt; [tablespace &lt;ts&gt;] [storage (&#8230;)] [pctfree &lt;10&gt;] [pctused &lt;40&gt;] [initrans &lt;1&gt;] [maxtrans &lt;n&gt;] [logging | nologging] [cache | nocache] [noparallel | parallel [&lt;n&gt;]] [partition&#8230;] [lob&#8230;] [using index&#8230;] [with [primary key] [, rowid] [(&lt;col&gt; [, &#8230;])] ] [{including | excluding} new values];</div><div>&nbsp;alter {materialized view | snapshot} log on &lt;tab&gt; [add [primary key] [, rowid] [(&lt;col&gt; [, &#8230;])] ] [&#8230;];</div><div>&nbsp;drop {materialized view | snapshot} log on &lt;tab&gt;;</div><div>&nbsp;create {materialized view | snapshot} &lt;mview&gt;[tablespace &lt;ts&gt;] [storage (&#8230;)] [pctfree &lt;10&gt;] [pctused &lt;40&gt;] [initrans &lt;1&gt;] [maxtrans &lt;n&gt;] [logging | nologging] [cache | nocache] [noparallel | parallel [&lt;n&gt;]] [cluster &lt;clust&gt; (&lt;col&gt; [, &#8230;])] [lob&#8230;] [partition&#8230;] [build {immediate | deferred}] [on prebuilt table [{with | without} reduced precision]] [using index&#8230;] [ refresh [fast | complete | force] [on commit | on demand] [start with &#8216;&lt;date&gt;&#8217;] [next &#8216;&lt;date&gt;&#8217;] [with {primary key | rowid}] [using [default] [master | local] rollback segment [&lt;rbs&gt;]] ] | never refresh ] [for update] [{enable | disable} query rewrite] as &lt;query&gt;;</div><div>&nbsp;alter {materialized view | snapshot} &lt;mview&gt; &#8230; [compile];</div><div>&nbsp;drop {materialized view | snapshot} &lt;mview&gt;;</div><div></div><div>八、举例</div><div>&nbsp;connect pubr/bit@datasource ;</div><div>&nbsp;drop materialized view log on pubr.allactive; &nbsp;--删除物化视图日志</div><div>&nbsp;create materialized view log</div><div>&nbsp; &nbsp; &nbsp;on pubr.allactive tablespace logts with primary key; --创建物化视图日志</div><div></div><div>&nbsp;connect ttowb/bit;</div><div>&nbsp;drop materialized view allactive_tt; &nbsp; &nbsp; &nbsp; &nbsp;--删除物化视图</div><div>&nbsp;create materialized view allactive_tt</div><div>&nbsp;refresh fast</div><div>&nbsp;as select ID,CATEGORY,FLOWID,MASTATUS,BASTATUS,APPLYDATETIME,CREATEDATETIME,COMMITDATETIME,BITSPNO,ARCHIVETIME,</div><div>&nbsp;DESCRIPTION,OPERTYPE,ISVALID,INVALIDREASON,INVALIDDATETIME,INVALIDPNO,ACTIVETABLENAME,PARENTID,STANID,REALTYPEID,</div><div>&nbsp;CORRECTID,to_date('1900-01-01') allactive_rtime from pubr.allactive@pubrowb; &nbsp;--创建物化视图</div><div></div><div>九、参考</div><div>&nbsp;http://tech.ddvip.com/2008-09/122180687967473.html &nbsp;Oracle物化视图：创建最简单物化视图</div><div>&nbsp;http://yangtingkun.itpub.net/post/468/20584 &nbsp; &nbsp; &nbsp; &nbsp; Oracle如何根据物化视图日志快速刷新物化视图</div><div>&nbsp;http://blog.itpub.net/post/468/20498 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;物化视图日志结构&nbsp;</div><img src ="http://www.blogjava.net/fyq891014/aggbug/377010.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/fyq891014/" target="_blank">执着那份情</a> 2012-04-29 08:38 <a href="http://www.blogjava.net/fyq891014/articles/377010.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>