Oracle创建物化视图

1.物化视图语法

 create materialized view [view_name]
 refresh [fast|complete|force]
 [
 on [commit|demand] |
 start with (start_time) next (next_time)
 ]
 as
 {创建物化视图用的查询语句}

以上是Oracle创建物化视图(Materialized View,以下简称MV)时的常用语法,各参数的含义如下:

1.refresh [fast|complete|force] 视图刷新的方式:
fast: 增量刷新.假设前一次刷新的时间为t1,那么使用fast模式刷新物化视图时,只向视图中添加t1到当前时间段内,
主表变化过的数据.为了记录这种变化,建立增量刷新物化视图还需要一个物化视图日志表。create materialized view log on (主表名)。
(多张表时,此语句也生效,创建后,原来的表中会多出两类视图表:MLOG$_table_name和RUPD$_table_name)
  
complete:全部刷新。相当于重新执行一次创建视图的查询语句。
 
force: 这是默认的数据刷新方式。当可以使用fast模式时,数据刷新将采用fast方式;否则使用complete方式。
 
2. 建立基表的物化视图日志
-- tablename 为基表 with后面可以接主键,rowid  primary key是主键,rowid是表更新涉及的行号,sequence是序列对,自由添加。
--including new values必须包含
create materialized view log on tablename with primary key,rowid,sequence (AREA_NM_R, AREA_NM_N) including new values;

3. 赋予主表的权限给建立视图的用户

grant select on tabelname to A;

4.示例

--1. 建立基表的物化视图日志
create materialized view log on auth_role with rowid, sequence (role_id, role_ad, bpm_group, role_name, role_enable, role_type, order_num) including new values ;
--2. 授权 grant select on sys_role to auth;
--3. 创建物化视图 create materialized view viewname refresh force on demand start with SYSDATE next SYSDATE + NUMTODSINTERVAL(2,'MINUTE') as select role_id, role_ad, bpm_group, role_name, role_enable, role_type, order_num, 'auth' sys_code from auth_role union all select role_id, role_ad, bpm_group, role_name, role_enable, role_type, order_num, 'bpm' sys_code from cfcap.sys_role union all select role_id, role_id role_ad, role_id bpm_group, role_name, to_number(ROLE_STAT) role_enable,to_char(role_type)||'' role_type, 0 order_num, 'wbs' sys_code from forms.ts_role;

原文地址:https://www.cnblogs.com/zhouziyuan/p/10253673.html