物化檢視和query_rewrite_enabled引數配合提高select查詢效能
最近有一個需求,是使用物化檢視來最佳化查詢語句效能。因為不想大規模修改查詢語句,所以想到使用query_rewrite_enabled引數,使查詢語句能使用建好的物化檢視。
在asktom()找到例子,測試過程如下:
oracle@oracle[/home/oracle]> echo $ORACLE_SID cnhtm oracle@oracle[/home/oracle]> sqlplus / as sysdba SQL*Plus: Release 10.2.0.1.0 - Production on Mon Dec 28 09:52:44 2009 Copyright (c) 1982, 2005, Oracle. All rights reserved. Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production With the Partitioning, OLAP and Data Mining options |
為了測試方便,將dba角色給了scott使用者
[@more@]sys@CNHTM> grant dba to scott; Grant succeeded. sys@CNHTM> conn scott/tiger Connected. |
建立測試表t,並執行表分析
scott@CNHTM> Create table t as 2 select substr(object_name,1,1) a , b, rpad('x',50,'x') data 3 from all_objects, (select 1 b from dual union all select 2 b from dual union all select 3 b 4 from dual ); Table created. scott@CNHTM> analyze table t compute statistics; Table analyzed. |
檢視sql語句的執行計劃
scott@CNHTM> EXPLAIN PLAN FOR 2 select distinct a 3 from t 4 where b = 3; Explained. scott@CNHTM> select * from table(dbms_xplan.display) dual; PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------- Plan hash value: 1793979440 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 49 | 147 | 297 (5)| 00:00:04 | | 1 | HASH UNIQUE | | 49 | 147 | 297 (5)| 00:00:04 | |* 2 | TABLE ACCESS FULL| T | 49785 | 145K| 291 (3)| 00:00:04 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("B"=3) 14 rows selected. |
建立物化檢視,並分析物化檢視
scott@CNHTM> create materialized view mv_t 2 enable query rewrite 3 as select distinct a,b from t; Materialized view created. scott@CNHTM> analyze table mv_t compute statistics; Table analyzed. |
設定query_rewrite_enabled相關引數
scott@CNHTM> alter session set query_rewrite_enabled=true; Session altered. scott@CNHTM> alter session set query_rewrite_integrity=trusted; Session altered. |
再次執行sql語句,並檢視sql語句的執行計劃,注意紅色部分,證明已經對sql語句執行了rewrite。並且Cost大量降低
scott@CNHTM> EXPLAIN PLAN FOR 2 select distinct a 3 from t 4 where b = 3; Explained. scott@CNHTM> select * from table(dbms_xplan.display) dual; PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------- Plan hash value: 3347475089 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 34 | 102 | 4 (25)| 00:00:01 | | 1 | HASH UNIQUE | | 34 | 102 | 4 (25)| 00:00:01 | |* 2 | MAT_VIEW REWRITE ACCESS FULL| MV_T | 49 | 147 | 3 (0)| 00:00:01 | -------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("MV_T"."B"=3) 14 rows selected. |
--end--
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22049049/viewspace-1030037/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【MV】物化檢視查詢重寫
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- Oracle物化檢視2 -- Query Rewrite及引數Oracle
- 檢視慢查詢中,表被update 或 select 次數
- 使用物化檢視查詢重寫 優化對於 UNION ALL檢視的CONNECT BY查詢優化
- DB2資料庫物化檢視:MQT物化查詢表的使用DB2資料庫MQQT
- 提高SQL查詢效能SQL
- v$動態效能檢視和隱含引數
- 淺析物化檢視與查詢重寫(Enable query rewrite)
- 隱式轉換影響物化檢視查詢重寫
- 包含複雜查詢的快速重新整理的物化檢視
- 普通檢視和物化檢視的區別
- ClickHouse效能優化?試試物化檢視優化
- ClickHouse 效能優化?試試物化檢視優化
- Oracle最佳化技術---物化檢視查詢重寫query rewriteOracle
- 【ORACLE】常用物化檢視相關後設資料查詢語句Oracle
- 物化檢視中的統計資訊導致的查詢問題分析和修復
- 物化檢視妙用__表同步使用物化檢視方法
- 【物化檢視】幾種物化檢視日誌分析
- 通過Oracle動態效能檢視採集查詢調優數Oracle
- Oracle普通檢視和物化檢視的區別Oracle
- 【物化檢視】根據物化檢視日誌快速重新整理物化檢視的過程
- [20170502]11G查詢隱含引數檢視.txt
- oracle物化檢視Oracle
- MySQL引數化查詢的IN 和 LIKEMySql
- 隱藏引數查詢和dictionary viewView
- linux和aix核心引數檢查LinuxAI
- 【原創】Oracle 初始化引數&效能檢視Oracle
- 物化檢視prebuilt和線上重定義UI
- trigger和物化檢視同步表
- Oracle檢視引數Oracle
- 物化檢視詳解
- oracle 建立物化檢視Oracle
- Oracle 物化檢視建立Oracle
- materialized view (物化檢視)ZedView
- 物化檢視 on commitMIT
- 用hash cluster表提高查詢效能 (一)
- 【MV】group by查詢子句是否包含count(*)對物化檢視重新整理的影響