【MV】物化檢視查詢重寫
所謂物化檢視查詢重寫就是,如果初始化引數query_rewrite_enabled設定為TRUE,並且資料庫執行在CBO最佳化模式下,當對基表進行查詢時,Oracle會自動判斷是否能利用這個基表的所有包含ENABLE QUERY REWRITE關鍵字的物化檢視,如果可以且根據統計資訊判斷透過查詢物化檢視代價更小,則Oracle自動重寫查詢語句,透過查詢物化檢視得到正確的結果。
1.初始化環境
1)準備物化檢視基表
create table t (x int, y int,z int);
insert into t values (1,1,1);
insert into t values (2,2,2);
insert into t values (3,3,3);
insert into t values (4,4,4);
insert into t values (5,5,5);
insert into t values (6,6,6);
insert into t values (6,6,6);
commit;
sec@ora10g> select * from t;
X Y Z
---------- ---------- ----------
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
6 6 6
7 rows selected.
2)建立物化檢視日誌
注:包含所有欄位
sec@ora10g> create materialized view log on t with sequence, rowid (x,y,z) including new values;
Materialized view log created.
3)建立物化檢視
sec@ora10g> create materialized view mv_t build immediate refresh fast on commit enable query rewrite as select x,y,z,count(*) from t group by x,y,z;
Materialized view created.
sec@ora10g> select * from mv_t;
X Y Z COUNT(*)
---------- ---------- ---------- ----------
1 1 1 1
2 2 2 1
3 3 3 1
4 4 4 1
5 5 5 1
6 6 6 2
6 rows selected.
2.物化檢視查詢重寫測試
1)啟用查詢重寫功能(預設)
sec@ora10g> show parameter query_rewrite_enabled
NAME TYPE VALUE
--------------------- ------ -----------------
query_rewrite_enabled string TRUE
sec@ora10g> alter system set query_rewrite_enabled=TRUE;
System altered.
sec@ora10g> set autotrace on
sec@ora10g> select x,y,z,count(*) from t group by x,y,z;
X Y Z COUNT(*)
---------- ---------- ---------- ----------
1 1 1 1
2 2 2 1
3 3 3 1
4 4 4 1
5 5 5 1
6 6 6 2
6 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1712400360
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 6 | 312 | 3 (0)| 00:00:01 |
| 1 | MAT_VIEW REWRITE ACCESS FULL| MV_T | 6 | 312 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
677 bytes sent via SQL*Net to client
381 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed
從執行計劃上可以看很清晰的看到這個查詢重新的過程,我們對T表進行查詢,Oracle此時發現可以透過物化檢視MV_T直接返回我們需要的結果,因此最終智慧的決定透過檢索物化檢視來返回最後結果。
2)停用查詢重寫功能
sec@ora10g> alter system set query_rewrite_enabled=FALSE;
System altered.
sec@ora10g> show parameter query_rewrite_enabled
NAME TYPE VALUE
--------------------- ------ -----------------
query_rewrite_enabled string FALSE
sec@ora10g> set autotrace on
sec@ora10g> select x,y,z,count(*) from t group by x,y,z;
X Y Z COUNT(*)
---------- ---------- ---------- ----------
4 4 4 1
3 3 3 1
6 6 6 2
1 1 1 1
5 5 5 1
2 2 2 1
6 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 47235625
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 7 | 273 | 4 (25)| 00:00:01 |
| 1 | HASH GROUP BY | | 7 | 273 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| T | 7 | 273 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
15 consistent gets
0 physical reads
0 redo size
680 bytes sent via SQL*Net to client
381 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed
當停用查詢重寫功能後,查詢結果將只能透過T表進行返回。
3.小結
當滿足物化檢視查詢重寫的條條框框後,我們便實現了高效、靈活地檢索資料的目的。Oracle在CBO最佳化模式下帶給我們很多新奇的體驗。
Good luck.
secooler
11.06.12
-- The End --
1.初始化環境
1)準備物化檢視基表
create table t (x int, y int,z int);
insert into t values (1,1,1);
insert into t values (2,2,2);
insert into t values (3,3,3);
insert into t values (4,4,4);
insert into t values (5,5,5);
insert into t values (6,6,6);
insert into t values (6,6,6);
commit;
sec@ora10g> select * from t;
X Y Z
---------- ---------- ----------
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
6 6 6
7 rows selected.
2)建立物化檢視日誌
注:包含所有欄位
sec@ora10g> create materialized view log on t with sequence, rowid (x,y,z) including new values;
Materialized view log created.
3)建立物化檢視
sec@ora10g> create materialized view mv_t build immediate refresh fast on commit enable query rewrite as select x,y,z,count(*) from t group by x,y,z;
Materialized view created.
sec@ora10g> select * from mv_t;
X Y Z COUNT(*)
---------- ---------- ---------- ----------
1 1 1 1
2 2 2 1
3 3 3 1
4 4 4 1
5 5 5 1
6 6 6 2
6 rows selected.
2.物化檢視查詢重寫測試
1)啟用查詢重寫功能(預設)
sec@ora10g> show parameter query_rewrite_enabled
NAME TYPE VALUE
--------------------- ------ -----------------
query_rewrite_enabled string TRUE
sec@ora10g> alter system set query_rewrite_enabled=TRUE;
System altered.
sec@ora10g> set autotrace on
sec@ora10g> select x,y,z,count(*) from t group by x,y,z;
X Y Z COUNT(*)
---------- ---------- ---------- ----------
1 1 1 1
2 2 2 1
3 3 3 1
4 4 4 1
5 5 5 1
6 6 6 2
6 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1712400360
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 6 | 312 | 3 (0)| 00:00:01 |
| 1 | MAT_VIEW REWRITE ACCESS FULL| MV_T | 6 | 312 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
677 bytes sent via SQL*Net to client
381 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed
從執行計劃上可以看很清晰的看到這個查詢重新的過程,我們對T表進行查詢,Oracle此時發現可以透過物化檢視MV_T直接返回我們需要的結果,因此最終智慧的決定透過檢索物化檢視來返回最後結果。
2)停用查詢重寫功能
sec@ora10g> alter system set query_rewrite_enabled=FALSE;
System altered.
sec@ora10g> show parameter query_rewrite_enabled
NAME TYPE VALUE
--------------------- ------ -----------------
query_rewrite_enabled string FALSE
sec@ora10g> set autotrace on
sec@ora10g> select x,y,z,count(*) from t group by x,y,z;
X Y Z COUNT(*)
---------- ---------- ---------- ----------
4 4 4 1
3 3 3 1
6 6 6 2
1 1 1 1
5 5 5 1
2 2 2 1
6 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 47235625
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 7 | 273 | 4 (25)| 00:00:01 |
| 1 | HASH GROUP BY | | 7 | 273 | 4 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL| T | 7 | 273 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
15 consistent gets
0 physical reads
0 redo size
680 bytes sent via SQL*Net to client
381 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
6 rows processed
當停用查詢重寫功能後,查詢結果將只能透過T表進行返回。
3.小結
當滿足物化檢視查詢重寫的條條框框後,我們便實現了高效、靈活地檢索資料的目的。Oracle在CBO最佳化模式下帶給我們很多新奇的體驗。
Good luck.
secooler
11.06.12
-- The End --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1069052/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- 淺析物化檢視與查詢重寫(Enable query rewrite)
- 隱式轉換影響物化檢視查詢重寫
- 使用物化檢視查詢重寫 優化對於 UNION ALL檢視的CONNECT BY查詢優化
- Oracle最佳化技術---物化檢視查詢重寫query rewriteOracle
- Oracle物化檢視3 - Prebuilt MVOracleUI
- 建立物化檢視MV ( Materialized View )ZedView
- 【MV】group by查詢子句是否包含count(*)對物化檢視重新整理的影響
- 12c 查詢重寫物化檢視 - DBMS_ADVISOR.TUNE_MVIEW的使用View
- 【MV】實現跨庫可更新物化檢視
- MV (Materialed View) 物化檢視的重新整理組View
- DB2資料庫物化檢視:MQT物化查詢表的使用DB2資料庫MQQT
- 查詢重寫
- 包含複雜查詢的快速重新整理的物化檢視
- 物化檢視prebuilt和線上重定義UI
- 【ORACLE】常用物化檢視相關後設資料查詢語句Oracle
- 物化檢視妙用__表同步使用物化檢視方法
- 【物化檢視】幾種物化檢視日誌分析
- 【物化檢視】根據物化檢視日誌快速重新整理物化檢視的過程
- oracle物化檢視Oracle
- 物化檢視和query_rewrite_enabled引數配合提高select查詢效能
- 物化檢視詳解
- oracle 建立物化檢視Oracle
- Oracle 物化檢視建立Oracle
- materialized view (物化檢視)ZedView
- 物化檢視 on commitMIT
- 檢視查詢報錯
- 【MySQL】檢視&子查詢MySql
- oracle 常用查詢檢視Oracle
- 物化檢視日誌表被DROP後建立物化檢視報錯
- 物化檢視中的統計資訊導致的查詢問題分析和修復
- Oracle如何根據物化檢視日誌快速重新整理物化檢視Oracle
- 物化檢視的快速重新整理測試與物化檢視日誌
- 普通檢視和物化檢視的區別
- calcite物化檢視詳解
- Oracle物化檢視詳解Oracle
- ORACLE物化檢視測試Oracle
- Oracle 物化檢視案例分享Oracle