2014-06-08 Created By BaoXinjian
1. 用法
物化视图是包括一个查询结果的数据库对象,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表。
物化视图存储基于远程表的数据,也可以称为快照。对于复制,物化视图允许你在本地维护远程数据的副本,这些副本是只读的。
如果你想修改本地副本,必须用高级复制的功能。当你想从一个表或视图中抽取数据时,你可以用从物化视图中抽取。
对于数据仓库,创建的物化视图通常情况下是聚合视图,单一表聚合视图和连接视图。
实现两个数据库之间的数据同步,可以存在时间差。
1. 刷新的方式
Fast
Complete
Fource
2. 刷新的方法
DBMS_REFRESH.Refresh
DBMS_MVIEW.Refresh
2. 具体应用
(1).在源数据库建立mview log日志文件
create materialized view log on w_1 ;
----注:(TEST为表名或者视图名,关于视图上建立物化视图,见基于视图的物化视图
----创建物化视图语句:
(2).在统计数据建立materializad view 语法
Create materialized view MV_TEST
----MVTEST为物化视图名
Build immediate
----创建时生成数据对应的是build deferred
Refresh fast
----增量刷新
On commit
----在基表有更新时提交,这里该句对视图无效
With rowid
----这里创建基于rowid的物化视图,对应的是 primary key
As
Select * from TEST;
----生成物化视图数据语句
(3).调用时进行刷新
dbms_refresh.refresh('W_1')
3. 语法
1. 基本语法
4. 案例
案例: 创建三种刷新方式的物化视图, 并创建日志,通过dbms_job定时refresh mview group
Step1. 创建三个实体表
CREATE TABLE bxj_objects_t1
(
owner,
object_name,
subobject_name,
object_id PRIMARY KEY, data_object_id, object_type, created, last_ddl_time, timestamp, status, temporary, generated, secondary, namespace, edition_name ) AS SELECT * FROM all_objects; CREATE TABLE bxj_objects_t2 ( owner, object_name, subobject_name, object_id PRIMARY KEY, data_object_id, object_type, created, last_ddl_time, timestamp, status, temporary, generated, secondary, namespace, edition_name ) AS SELECT * FROM all_objects; CREATE TABLE bxj_objects_t3 ( owner, object_name, subobject_name, object_id PRIMARY KEY, data_object_id, object_type, created, last_ddl_time, timestamp, status, temporary, generated, secondary, namespace, edition_name ) AS SELECT * FROM all_objects;
Step2. 创建物化视图日志
CREATE MATERIALIZED VIEW LOG ON bxj_objects_t1;
CREATE MATERIALIZED VIEW LOG ON bxj_objects_t2; CREATE MATERIALIZED VIEW LOG ON bxj_objects_t3;
select * from DBA_MVIEW_lOGS
select * from MLOG$_BXJ_OBJECTS_T1 select * from MLOG$_BXJ_OBJECTS_T2 select * from MLOG$_BXJ_OBJECTS_T3
Step3. 创建物化视图
CREATE MATERIALIZED VIEW bxj_objects_mv_t1 REFRESH FAST AS SELECT * FROM apps.bxj_objects_t1; CREATE MATERIALIZED VIEW bxj_objects_mv_t2 REFRESH FORCE AS SELECT * FROM bxj_objects_t2; CREATE MATERIALIZED VIEW bxj_objects_mv_t3 REFRESH COMPLETE AS SELECT * FROM bxj_objects_t3;
Step4. 创建物化视图Refresh Group
EXEC DBMS_REFRESH.MAKE('REP_MVIEWGROUP', 'BXJ_OBJECTS_MV_T1,BXJ_OBJECTS_MV_T2,BXJ_OBJECTS_MV_T3', SYSDATE, 'SYSDATE + 1')
Step5. 刷新物化视图
EXEC DBMS_MVIEW.refresh('BXJ_OBJECTS_MV_T1','C'); EXEC DBMS_REFRESH.refresh('REP_MVIEWGROUP');
Step6. 加入dbms_jobs,定时刷新物化视图
DECLARE
jobno NUMBER; --通过查看该变量可以得到返回的作业编号
BEGIN DBMS_JOB.SUBMIT ( jobno, 'DBMS_REFRESH.refresh(''' || 'REP_MVIEWGROUP' || ''');', --执行脚本程序 SYSDATE, --现在执行 'SYSDATE+1' ); END;
Thanks and Regards
ERP技术讨论群: 288307890
技术交流,技术讨论,欢迎加入
Technology Blog Created By Oracle ERP - 鲍新建