oracle物化视图

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 转自: http://www.cnblogs.com/BradMiller/archive/2011/04/24/2026321.html oracle物化视图 一、oracle物化视图基本概念 物化视图首先需要创建物化视图日志, oracle依据用户创建的物化视图日志来创建物化视图日志表, 物化视图日志表的名称为mlog$_后面跟基表的名称, 如果表名的长度超过20位,则只取前20位,当截短后出现名称重复时,oracle会自动在物化视图日志名称后面加上数字作为序号。

转自:

http://www.cnblogs.com/BradMiller/archive/2011/04/24/2026321.html

oracle物化视图

一、oracle物化视图基本概念
 物化视图首先需要创建物化视图日志,
 oracle依据用户创建的物化视图日志来创建物化视图日志表,
 物化视图日志表的名称为mlog2020oraclerowidprimarykeyobjectidsequencesnaptimedmltypedmliinsertddeleteuupdateoldnewnewolduupdatechangevectorwithprimarykeywithrowidmrowrowidwithobjectidsysncoid:用来记录每个变化对象的对象id。
 如果with后面跟了sequence,则物化视图日子中会包含:sequence$$:给每个操作一个sequence号,从而保证刷新时按照顺序进行刷新。
 如果with后面跟了一个或多个column名称,则物化视图日志中会包含这些列。

二、oracle物化视图日志
 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 mlogtestid;nametypenullabledefaultcommentsidnumberydmlsnaptimedateysnaptimedmltypevarchar2(1)ydmltypeiudinsertupdatedeleteoldnewvarchar2(1)yoldnewdmldmlonupdateuchangevectorraw(255)ychangevectordml使primarykeyoracleRUPD_基础表
  sql> desc rupdtestid;nametypenullabledefaultcommentsidnumberydmltypevarchar2(1)ysnapidintegerychangevector$ 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 mlogtestrowid;nametypenullabledefaultcommentsmrowvarchar2(255)ysnaptimedateydmltypevarchar2(1)yoldnewvarchar2(1)ychangevector$ 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 mlogtestobjid;nametypenullabledefaultcommentssysncoid     raw(16)     y                        
  snaptimedateydmltype

       varchar2(1) y                        
  old_newvarchar2(1)ychangevector
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 mlogtestsq;nametypenullabledefaultcommentsidnumberynamevarchar2(30)ymrowvarchar2(255)ysequencenumberysequencesnaptimedateydmltypevarchar2(1)yoldnewvarchar2(1)ychangevector$ raw(255)      y 

三、oracle物化视图日志表
 基础表:test_id,test_rowid,test_objid,test_sq
 日志表:mlogtestidmlog_test_rowid,mlogtestobjidmlog_test_sq
 1.新增
  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;
 2.修改
  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;
 3.删除
  delete test_id   ;
  delete test_rowid;
  delete test_objid;
  delete test_sq   ;
  commit;
 在每一步commit后查看日志表记录。

四、oracle物化视图日志表字段取值解析
 1.snaptimedml400011000使使2.dmltype


  操作类型比较简单:只包括i(insert)、d(delete)和u(update)三种。
 3.old_newodeleteninsertuupdateupdatedeleteinsertdeleteupdateupdatedmltype
和old_newuupdatedmltype
为u,old_newnincludingnewvalues4.changevector

  最后简单讨论一下change_vectorinsertdeleteinsertdeleteupdateupdateoraclechangevector
列来记录每条记录发生变化的字段包括哪些。
  基于主键、rowid和object id的物化视图日志在change_vectorchangevector
列是raw类型,其实oracle采用的方式就是用每个bit位去映射一个列。
  比如:第一列被更新设置为02,即00000010。
  第二列设置为04,即00000100,
  第三列设置为08,即00001000。
  当第一列和第二列同时被更新,则设置为06,00000110。
  如果三列都被更新,设置为0e,00001110。
  依此类推,第4列被更新时为0x10,第5列0x20,第6列0x40,第7列0x80,第8列0x100。
  当第1000列被更新时,change_vector$$的长度为1000/4+2为252。

  除了可以表示update的字段,还可以表示insert和delete。delete操作change_vector0insertfeffinsertupdateinsertchangevector

列为全ff。

  可以看到,正如上面分析的,insert为fe,delete为00,对第一列的更新为02,第二列为04,第二列和第三列都更新为0c。需要注意,正常情况下,第一列会从02开始。
  但是如果对mlogtruncaterowidupdatedeleteinsertinsertchangevector$的值是ff。
  这个结果也和rowid类型基本一致,需要注意的是,由于对象表包含两个隐含列,因此id不再是第一个字段,而是第三个,因此对应的值是08。
  最后看一个包含列数较多的例子,唯一需要注意的是,低位在左,高位在右。

五、oracle物化视图
 1.物化视图mv_test_id
  create materialized view mv_test_id refresh fast on commit as
    select * from test_id;                                           --commit时物化视图被刷新
 2.物化视图mv_test_rowid
  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;   
 4.物化视图mv_test_sq
  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')来刷新

 5.物化视图刷新
  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;

六、错误提示:
 --ORA-32401: "TT"."TEST_ROWID" 上的实体化视图日志没有新值
 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); 
 --ORA-12014: 表 'TEST_OBJID' 不包含主键约束条件
 alter table test_objid add constraint pk_test_objid primary key (id);  --增加主键   
 --ORA-23415: "TT"."TEST_OBJID" 的实体化视图日志不记录主键
 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 [tablespace ] [storage (…)] [pctfree ] [pctused ] [initrans ] [maxtrans ] [logging | nologging] [cache | nocache] [noparallel | parallel []] [partition…] [lob…] [using index…] [with [primary key] [, rowid] [( [, …])] ] [{including | excluding} new values];
 alter {materialized view | snapshot} log on [add [primary key] [, rowid] [( [, …])] ] […];
 drop {materialized view | snapshot} log on ;
 create {materialized view | snapshot} [tablespace ] [storage (…)] [pctfree ] [pctused ] [initrans ] [maxtrans ] [logging | nologging] [cache | nocache] [noparallel | parallel []] [cluster ( [, …])] [lob…] [partition…] [build {immediate | deferred}] [on prebuilt table [{with | without} reduced precision]] [using index…] [ refresh [fast | complete | force] [on commit | on demand] [start with ‘’] [next ‘’] [with {primary key | rowid}] [using [default] [master | local] rollback segment []] ] | never refresh ] [for update] [{enable | disable} query rewrite] as ;
 alter {materialized view | snapshot} … [compile];
 drop {materialized view | snapshot} ;

八、举例
 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;  --创建物化视图

九、参考
 http://tech.ddvip.com/2008-09/122180687967473.html  Oracle物化视图:创建最简单物化视图
 http://yangtingkun.itpub.net/post/468/20584         Oracle如何根据物化视图日志快速刷新物化视图
 http://blog.itpub.net/post/468/20498                物化视图日志结构

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
打赏
0
0
0
0
91
分享
相关文章
Oracle物化视图:数据的“快照”艺术
【4月更文挑战第19天】Oracle物化视图是预计算的查询结果集,存储为实际表,提升查询性能,简化复杂查询。它们用于快速访问、封装复杂逻辑,加速数据汇总和报表生成。然而,需注意基础数据稳定性、刷新策略及存储成本。物化视图是高效数据管理的工具,但需适时优化使用。
10 PostgreSQL 表级复制-物化视图篇, 支持异地,异构如 Oracle 到 pg 的物化视图|学习笔记
快速学习10 PostgreSQL 表级复制-物化视图篇,支持异地,异构如 Oracle 到 pg 的物化视图
10 PostgreSQL 表级复制-物化视图篇, 支持异地,异构如 Oracle 到 pg 的物化视图|学习笔记
Oracle运维笔记之创建物化视图报错ORA-08102
Oracle运维笔记之创建物化视图报错ORA-08102
PostgreSQL 物化视图(Oracle同步到PG,PG同步到PG) - by pgsnapshot (plperlu trigger) (支持类似Oracle的mvlog fast complete force刷新)
标签 PostgreSQL , 物化视图 , 增量刷新 , mvlog , Oracle 同步到 PG , PG 同步到 PG 背景 PostgreSQL自身的物化视图没有MVLOG,也就是说,刷新的时候是VIEW定义产生的记录与MV已刷新的记录进行比对,进行增量更新的过程。
3394 0

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等