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;