物化視圖是一種特殊的物理表
Oracle最早在OLAP系統中引入了物化視圖的概念
本文是Oracle物化視圖系列文章的第一篇
下面創建一個最簡單的物化視圖
本例中需要特別注意的是
像我們這樣
從下例中可以看出
● 創建物化視圖
SQL> select * from v$version;
BANNER
Oracle Database
PL/SQL Release
CORE
TNS for
NLSRTL Version
SQL> create materialized view mv_testcf
Materialized view created
SQL> analyze table xiaotg
Table analyzed
SQL> select tl
TABLE_NAME NUM_ROWS
MV_TESTCF
TESTCF
SQL> col segment_name for a
SQL> select sg
SEGMENT_NAME BYTES BLOCKS
MV_TESTCF
● 查看物化視圖關鍵定義
SQL> select mv
(為增加查詢結果的可讀性
OWNERXIAOTG
MVIEW_NAMEMV_TESTCF
CONTAINER_NAMEMV_TESTCF
QUERY
QUERY_LEN
UPDATABLEN
UPDATE_LOG
MASTER_ROLLBACK_SEG
MASTER_LINK
REWRITE_ENABLEDN
REWRITE_CAPABILITYGENERAL
REFRESH_MODEDEMAND
REFRESH_METHODFORCE
BUILD_MODEIMMEDIATE
FAST_REFRESHABLEDML
LAST_REFRESH_TYPECOMPLETE
LAST_REFRESH_DATE
STALENESSFRESH
AFTER_FAST_REFRESHFRESH
UNKNOWN_PREBUILTN
UNKNOWN_PLSQL_FUNCN
UNKNOWN_EXTERNAL_TABLEN
UNKNOWN_CONSIDER_FRESHN
UNKNOWN_IMPORTN
UNKNOWN_TRUSTED_FDN
COMPILE_STATEVALID
USE_NO_INDEXN
STALE_SINCE
NUM_PCT_TABLES
NUM_FRESH_PCT_REGIONS
NUM_STALE_PCT_REGIONS
物化視圖最重要的功能和特性之一
但怎麼更新?或者說物化視圖的數據怎麼隨著基表而更新?Oracle提供了兩種方式
這是最基本的刷新辦法了
下面將測試INSERT
需要注意的是
● 在基表插入測試數據
基表數據插入後
SQL> col id for a
SQL> col name for a
SQL> select * from xiaotg
ID NAME
SQL> select * from xiaotg
ID NAME
SQL> insert into xiaotg
SQL> commit;
Commit complete
從下面的實驗可以看出
關於如何使得ON DEMAND物化視圖數據被更新
SQL> select * from xiaotg
ID NAME
SQL> select * from xiaotg
ID NAME
SQL>
最簡單的ON COMMIT物化視圖的創建
● 創建物化視圖
需要注意的是
SQL> create materialized view mv_testcf
Materialized view created
SQL>
SQL> analyze table xiaotg
Table analyzed
SQL> analyze table xiaotg
Table analyzed
SQL> select tl
TABLE_NAME NUM_ROWS
MV_TESTCF
TESTCF
● 查看物化視圖關鍵定義
可以從DBA_MVIEWS中看出
SQL> select mv
(為增加查詢結果的可讀性
REFRESH_MODECOMMIT
REFRESH_METHODFORCE
BUILD_MODEIMMEDIATE
ON COMMIT物化視圖會在基表一旦提交時
這意味著
實驗中
● 在基表中插入數據
SQL> set timing on;
SQL> insert into xiaotg
Executed in
SQL> commit;
Commit complete
Executed in
SQL> select * from xiaotg
ID NAME
Executed in
SQL> col id for a
SQL> col name for a
SQL> select * from xiaotg
ID NAME
Executed in
SQL> select * from xiaotg
ID NAME
Executed in
● 測試基表正常情況下的COMMIT速度
SQL> drop materialized view mv_testcf
Materialized view dropped
Executed in
SQL>
SQL>
SQL> insert into xiaotg
Executed in
SQL> commit;
Commit complete
Executed in
From:http://tw.wingwit.com/Article/program/Oracle/201311/17144.html