[转载]Oracle物化视图创建及管理
一、前言:
在Oracle文档中,物化视图作为数据仓库的一个技术,但是实际上在OLTP系统中,用来进行数据同步,使用查询重写优化SQL语句都是相当方便的。
二、物化视图创建
1)创建者需要的权限:
2)刷新者是所有者
三、物化视图创建
SQL> create table big2 as select * from dba_objects;
Table created.
SQL> create
materialized view log on big2 ;
create materialized view log on
big2
*
ERROR at line 1:
ORA-12014: table 'BIG2' does not
contain a primary key constraint
所以如果主表没有主键的话,选择rowid比较合适
SQL> create materialized view log on big2 with rowid;
Materialized view log created.
SQL> create
materialized view big2_mv
Materialized view created.
创建方式:build immediatedeferred 默认是build immediate
刷新方法:refresh
forcecompletefastnever on demandcommit
日志方式:with primary keyrowid
四、物化视图刷新
因为创建的时候指定了on demand,刷新就需要人工控制,无非是命令行方式调用刷新或者通过job实现。
1)手工刷新
SQL> insert into big2 select * from dba_objects where rownum<101;
100 rows created.
SQL> commit;
Commit complete.
SQL> select count(*) from mlog$_big2;
----------
SQL>
begin
PL/SQL procedure successfully completed.
SQL> select count(*) from big2_mv;
----------
SQL> select count(*) from mlog$_big2;
----------
刷新完以后会自动清除物化视图日志
2)使用刷新组管理刷新
SQL>
begin
加入刷新组,每10秒进行一次刷新
PL/SQL procedure successfully
completed.
SQL> select name,rname from user_refresh_children;
NAME
------------------------------
------------------------------
REF_MAIN
BIG_MV2
BIG2_MV
SQL> show parameter job
NAME
------------------------------------
----------- ------------------------------
job_queue_processes
SQL> select count(*)
from big2;
----------
SQL> delete from big2;
57891 rows deleted.
SQL> commit;
Commit complete.
SQL> select
mview_name,refresh_method,last_refresh_type,last_refresh_date
MVIEW_NAME
------------------------------
BIG2_MV
SQL> select count(*) from big2_mv;
----------
五、同一个主表的多物化视图刷新
SQL> create
materialized view big2_mv1
Materialized view created.
SQL> create materialized view big2_mv2
Materialized view created.
SQL> select count(*) from big2;
----------
SQL> select count(*) from big2_mv1;
----------
SQL> select count(*) from big2_mv2;
----------
SQL> insert into big2 select * from dba_objects;
57798 rows created.
SQL> commit;
Commit complete.
SQL> select count(*) from big2;
----------
SQL> select count(*) from mlog$_big2;
----------
SQL> begin
PL/SQL procedure successfully completed.
SQL> select count(*) from big2_mv1;
----------
SQL> select count(*) from mlog$_big2;
----------
SQL> begin
PL/SQL procedure successfully completed.
SQL> select count(*) from big2_mv2;
----------
SQL> select count(*) from mlog$_big2;
----------
SQL> insert into big2 select * from dba_objects where rownum<101;
100 rows created.
SQL> commit;
Commit complete.
SQL> select count(*) from mlog$_big2;
----------
SQL> begin
PL/SQL procedure successfully completed.
SQL> select count(*) from mlog$_big2;
----------
SQL> begin
PL/SQL procedure successfully completed.
SQL> select count(*) from mlog$_big2;
----------
附1 刷新组管理
a、添加某物化视图到物化视图组
BEGIN
END;
/
b、移除某物化视图
BEGIN
END;
/
c、删除物化视图组
BEGIN
END;
/
附2 物化视图相关数据字典
user_mviews all_mviews dba_mviews
(物化视图的基本信息及最后刷新信息)
user_mview_logs all_mview_logs
dba_mview_logs(物化视图日志的信息)
user_refresh all_refresh
dba_refresh(刷新组信息)
user_refresh_children
all_refresh_children dba_refresh_children