Materialized
View中文翻譯成「實體化視圖」或「物化視圖」。其實就是以前比較熟悉的snapshot,將本機/遠端的資料庫上面一個/多個表格的查詢結果,快照儲存在本機/遠端的表格中。
如果要備份的表格是在遠端資料庫的時候,則必須先建立Database Link,可以參考我寫的另外一篇文章,以下只針對Materialized View的部分進行著墨。
CREATE MATERIALIZED VIEW/SNAPSHOT Material_View_Name
[TABLESPACE Tablespaces_Name]
REFRESH [complete/fast/force(default)] [WITH primary key(default)/rowid/object id]
[TABLESPACE Tablespaces_Name]
REFRESH [complete/fast/force(default)] [WITH primary key(default)/rowid/object id]
[START
WITH sysdate NEXT
(sysdate+1/24)]
AS Sql_Query;
AS Sql_Query;
Refresh的方式有三種
- COMPLETE:每次Refresh時,需要重新執行查詢,較費時。
- FAST:每次Refresh時,會根據事先建立的mlog去更新有異動的資料,速度較快。
- FORCE:每次Refresh時,會先以Fast的方式,如果無法完成,則採用COMPLETE的方式。
START WITH是開始執行快照的時間,NEXT是下次執行的時間,sysdate+1/24表示每小時更新一次,如果要表示每分鐘更新一次,只要寫成sysdate+1/(24*60)即可。
1.建立Materialized View (complete)
CREATE MATERIALIZED VIEW emp_mv
TABLESPACE TS1
REFRESH complete WITH rowid
START WITH sysdate NEXT sysdate+1/24
AS SELECT * FROM emp;
2.建立Materialized View (fast)
Step1:先建立mlog
CREATE MATERIALIZED VIEW LOG ON emp with rowid;
Step2:再建立Materialized View,與complete類似
CREATE MATERIALIZED VIEW emp_mv
TABLESPACE TS1
REFRESH fast WITH rowid
START WITH sysdate NEXT sysdate+1/24
AS SELECT * FROM emp;
3.更改Materialized View
ALTER MATERIALIZED VIEW emp_mv
REFRESH complete
START WITH sysdate+1/1440 NEXT sysdate+6/1440;
4.移除Materialized View
DROP MATERIALIZED VIEW emp_mv;
5.查看Materialized View情況
SELECT * FROM all_snapshots;