淺析物化檢視與查詢重寫(Enable query rewrite)
查詢重寫是指當對物化檢視的基表進行查詢時,Oracle會自動判斷能否通過查詢物化檢視來得到結果,如果可以,則避免了聚集或連線操作,而直接從已經計算好的物化檢視中讀取資料
通過如下實驗說明,物化檢視給我們帶來的效能提高,以及查詢重寫的好處
1 執行一段普通的連線查詢,並檢視執行計劃以及統計資訊
SQL> alter system flush buffer_cache;
System altered.
SQL> set autotrace traceonly
SQL> select ename,job,dname
2 from emp,dept
3 where emp.deptno=dept.deptno;
14 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 844388907
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 420 | 6 (17)| 00:00:01 |
| 1 | MERGE JOIN | | 14 | 420 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 52 | 2 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)| 00:00:01 |
|* 4 | SORT JOIN | | 14 | 238 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 14 | 238 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("EMP"."DEPTNO"="DEPT"."DEPTNO")
filter("EMP"."DEPTNO"="DEPT"."DEPTNO")
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
11 consistent gets
9 physical reads
0 redo size
970 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
14 rows processed
System altered.
SQL> set autotrace traceonly
SQL> select ename,job,dname
2 from emp,dept
3 where emp.deptno=dept.deptno;
14 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 844388907
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 420 | 6 (17)| 00:00:01 |
| 1 | MERGE JOIN | | 14 | 420 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 52 | 2 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)| 00:00:01 |
|* 4 | SORT JOIN | | 14 | 238 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 14 | 238 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("EMP"."DEPTNO"="DEPT"."DEPTNO")
filter("EMP"."DEPTNO"="DEPT"."DEPTNO")
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
11 consistent gets
9 physical reads
0 redo size
970 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
14 rows processed
物理讀和一致性讀分別為11個 和9個
2 建立以上語句的物化檢視,並檢視通過物化檢視查詢所得到的執行計劃以及統計資訊
CREATE MATERIALIZED VIEW LOG ON emp
WITH ROWID
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON dept
WITH ROWID
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW emp_dept_mv_01
BUILD IMMEDIATE
REFRESH force ON COMMIT
as select ename,job,dname
from emp,dept
where emp.deptno=dept.deptno;
WITH ROWID
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON dept
WITH ROWID
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW emp_dept_mv_01
BUILD IMMEDIATE
REFRESH force ON COMMIT
as select ename,job,dname
from emp,dept
where emp.deptno=dept.deptno;
SQL> select * from emp_dept_mv_01;
14 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2905768236
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 308 | 3 (0)| 00:00:01 |
| 1 | MAT_VIEW ACCESS FULL| EMP_DEPT_MV_01 | 14 | 308 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
2 physical reads
0 redo size
1079 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
14 rows processed
14 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2905768236
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 308 | 3 (0)| 00:00:01 |
| 1 | MAT_VIEW ACCESS FULL| EMP_DEPT_MV_01 | 14 | 308 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
2 physical reads
0 redo size
1079 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
14 rows processed
通過物化檢視的查詢,發現只有4個一致性讀和2個物理讀
至此,說明物化檢視會為系統帶來一定的效能提升
3 將物化檢視設定為enable query rewrite(預設為disable),並執行原查詢語句,檢視執行計劃
SQL> alter MATERIALIZED VIEW EMP_DEPT_MV_01 enable query rewrite;
Materialized view altered.
Materialized view altered.
SQL> show parameter rewrite
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
query_rewrite_enabled string TRUE
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
query_rewrite_enabled string TRUE
SQL> alter system flush buffer_cache;
System altered.
SQL> select ename,job,dname
2 from emp,dept
3 where emp.deptno=dept.deptno;
14 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1320338361
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 308 | 3 (0)| 00:00:01 |
| 1 | MAT_VIEW REWRITE ACCESS FULL| EMP_DEPT_MV_01 | 14 | 308 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
2 physical reads
0 redo size
1079 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
14 rows processed
結果驗證,該語句實際上自動查詢的是與其對應的物化檢視
總結:
物化檢視提高查詢速度,但需要額外的儲存空間
查詢重寫: 無需改變程式碼,只需建立一個查詢重寫的物化檢視即可提高系統效能
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28719055/viewspace-1258720/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- 物化檢視
- 兼顧高效能與低成本,淺析 Apache Doris 非同步物化檢視原理及典型場景Apache非同步
- 淺析 Flutter 與 iOS 的檢視橋樑FlutteriOS
- 物化檢視(zt)
- 淺析前端框架如何更新檢視前端框架
- Rewrite %{QUERY_STRING}用法
- MySQL Rewriter Query Rewrite PluginMySqlPlugin
- calcite物化檢視詳解
- Nginx location匹配及Rewrite重寫Nginx
- 物化檢視快速重新整理與ORA-00001
- 檢視查詢報錯
- 【MySQL】檢視&子查詢MySql
- Oracle普通檢視和物化檢視的區別Oracle
- 資料庫的物化檢視資料庫
- 物化檢視分割槽實驗
- Laravel query when 的查詢Laravel
- MySQL 查詢語句執行過程淺析MySql
- 物化檢視幾個知識點
- ClickHouse 物化檢視學習總結
- Elasticsearch Query DSL查詢入門Elasticsearch
- OushuDB 檢視查詢執行情況
- 【PDB】Oracle跨PDB檢視查詢Oracle
- MySQL 查詢的成本的檢視MySql
- PostgreSQL 原始碼解讀(24)- 查詢語句#9(查詢重寫)SQL原始碼
- Oracle物化檢視的建立及使用(二)Oracle
- Oracle物化檢視的建立及使用(一)Oracle
- ClickHouse 效能優化?試試物化檢視優化
- ClickHouse效能優化?試試物化檢視優化
- 淺析Windows的訪問許可權檢查機制Windows訪問許可權
- Elasticsearch複合查詢—constant score queryElasticsearch
- 檢視 Laravel 查詢資料語句Laravel
- [20210418]查詢v$檢視問題.txt
- Laravel Query Builder 複雜查詢案例:子查詢實現分割槽查詢 partition byLaravelUI
- URL重寫(rewrite)的具體實現與異常問題解決
- 基於ROWID更新的物化檢視測試
- StarRocks 物化檢視重新整理流程及原理
- StarRocks 物化檢視重新整理流程和原理
- 物化檢視如何快速完成資料聚合操作?