查詢轉換
一個查詢語句在進行完語句和許可權檢查後,會有一步查詢轉換的步驟,再到優化器為了決定最終的執行計劃計算成本評估。
查詢轉換的主要目的是確定如果改變查詢的寫法會不會提供更好的查詢計劃
如:
select *
from hr.employees
where department_id in (select department_id from hr.departments); --1
可能會被轉化為
select t1.*
from hr.employees t1, hr.departments t2
where t1.department_id = t2.department_id; --2
結果集沒有改變,但是在優化器看來,轉化後的版本執行計劃的選擇將會更好
幾種基本的查詢轉換如下:
1.檢視合併
2.子查詢巢狀
3.謂語前推
4.使用物化檢視進行查詢重寫
檢視合併
檢視合併,顧名思義,就是將原有的內嵌式檢視展開為獨立分析或者與剩餘查詢部分合併成總體執行計劃的獨立查詢塊的轉換。
這句話有點繞,在我理解來,就是將檢視還原為表查詢,再尋找最合適的執行計劃
look:
--語句1
select *
from scott.emp e, (select empno from scott.emp) e_view
where e.empno = e_view.empno;
--我們在檢視加入一個提示(/*+ merge*/強制檢視合併,/*+ no_merge*/強制檢視不合並)來防止語句被重寫
--語句2
select *
from scott.emp e,
(select /*+ no_merge*/
empno
from scott.emp) e_view
where e.empno = e_view.empno;
檢視執行計劃
-- 語句1檢視被合併
執行計劃
----------------------------------------------------------
Plan hash value: 4089764794
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 1400 | 3 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 14 | 1400 | 3 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL | EMP | 14 | 1218 | 3 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN| PK_EMP | 1 | 13 | 0 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("E"."EMPNO"="EMPNO")
-- 語句2檢視未合併
執行計劃
----------------------------------------------------------
Plan hash value: 1614380175
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time|
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 1400 | 6 (17)| 00:00:01|
|* 1 | HASH JOIN | | 14 | 1400 | 6 (17)| 00:00:01|
| 2 | TABLE ACCESS FUL | EMP | 14 | 1218 | 3 (0)| 00:00:01|
| 3 | VIEW | | 14 | 182 | 2 (0)| 00:00:01|
| 4 | INDEX FAST FULL SCAN| PK_EMP | 14 | 182 | 2 (0)| 00:00:01|
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."EMPNO"="E_VIEW"."EMPNO")
查詢轉換的主要目的是確定如果改變查詢的寫法會不會提供更好的查詢計劃
如:
select *
from hr.employees
where department_id in (select department_id from hr.departments); --1
可能會被轉化為
select t1.*
from hr.employees t1, hr.departments t2
where t1.department_id = t2.department_id; --2
結果集沒有改變,但是在優化器看來,轉化後的版本執行計劃的選擇將會更好
幾種基本的查詢轉換如下:
1.檢視合併
2.子查詢巢狀
3.謂語前推
4.使用物化檢視進行查詢重寫
檢視合併
檢視合併,顧名思義,就是將原有的內嵌式檢視展開為獨立分析或者與剩餘查詢部分合併成總體執行計劃的獨立查詢塊的轉換。
這句話有點繞,在我理解來,就是將檢視還原為表查詢,再尋找最合適的執行計劃
look:
--語句1
select *
from scott.emp e, (select empno from scott.emp) e_view
where e.empno = e_view.empno;
--我們在檢視加入一個提示(/*+ merge*/強制檢視合併,/*+ no_merge*/強制檢視不合並)來防止語句被重寫
--語句2
select *
from scott.emp e,
(select /*+ no_merge*/
empno
from scott.emp) e_view
where e.empno = e_view.empno;
檢視執行計劃
-- 語句1檢視被合併
執行計劃
----------------------------------------------------------
Plan hash value: 4089764794
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 1400 | 3 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 14 | 1400 | 3 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL | EMP | 14 | 1218 | 3 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN| PK_EMP | 1 | 13 | 0 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("E"."EMPNO"="EMPNO")
-- 語句2檢視未合併
執行計劃
----------------------------------------------------------
Plan hash value: 1614380175
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time|
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 1400 | 6 (17)| 00:00:01|
|* 1 | HASH JOIN | | 14 | 1400 | 6 (17)| 00:00:01|
| 2 | TABLE ACCESS FUL | EMP | 14 | 1218 | 3 (0)| 00:00:01|
| 3 | VIEW | | 14 | 182 | 2 (0)| 00:00:01|
| 4 | INDEX FAST FULL SCAN| PK_EMP | 14 | 182 | 2 (0)| 00:00:01|
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."EMPNO"="E_VIEW"."EMPNO")
注意:還有一些情況會阻止檢視合併的發生,如果一個查詢塊包含分析函式,或者聚合函式,集合運算(union/intersect/minus..),order by子句或者用了rownum,檢視合併將會被禁止或限制
Oracle Database Performance Tuning Guide>11.1.2.1
The view merging optimization applies to views that contain only selections, projections, and joins. That is, mergeable views do not contain set operators, aggregate functions, DISTINCT, GROUP BY, CONNECT BY, and so on.
Oracle Database Performance Tuning Guide>11.1.2.1
The view merging optimization applies to views that contain only selections, projections, and joins. That is, mergeable views do not contain set operators, aggregate functions, DISTINCT, GROUP BY, CONNECT BY, and so on.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26477854/viewspace-1072251/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle 查詢轉換Oracle
- Oracle 查詢轉換初探Oracle
- Oracle查詢轉換(五)子查詢展開Oracle
- Oracle 查詢轉換-01 or expansionOracle
- Mysql 查詢時間轉換MySql
- 【SQL】Oracle查詢轉換之 OR用法SQLOracle
- GP詭異的查詢轉換
- Oracle 12CR2查詢轉換之星型轉換Oracle
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- 一條SQL語句查詢塊分解及查詢轉換SQL
- Oracle 查詢轉換-04 Subquery UnnestingOracle
- Oracle XQuery查詢、構建和轉換XML(1)(轉)OracleXML
- Oracle 12CR2查詢轉換之臨時錶轉換Oracle
- (轉)SQL查詢案例:多行轉換為一行SQL
- Oracle 查詢轉換-02 View MergingOracleView
- Oracle 查詢轉換-03 Predicate PushingOracle
- 【SQL】Oracle查詢轉換之謂詞推送SQLOracle
- D4.玩轉查詢與替換
- 使用 Oracle XQuery 查詢、構建和轉換 XMLOracleXML
- 【八】查詢變換
- vim查詢替換
- 【SQL】Oracle查詢轉換之檢視合併SQLOracle
- Oracle查詢轉換(四)連線謂詞推入Oracle
- 怎樣把Oracle查詢轉換為SQL ServerOracleSQLServer
- CBO的查詢轉換(謂詞推入與子查詢展開(Subquery Unnesting))
- 記一次詭異的Oracle查詢轉換Oracle
- Oracle查詢轉換(一)簡單檢視合併Oracle
- Oracle查詢轉換(二)複雜檢視合併Oracle
- 關於查詢轉換的一些總結
- 字串查詢和替換字串
- Oracle 12CR2查詢轉換之謂詞推送Oracle
- Oracle查詢轉換(三)外連線檢視合併Oracle
- SQL解析過程中的查詢轉換 - Transforming QueriesSQLORM
- 隱式轉換影響物化檢視查詢重寫
- long查詢結果轉換為varchar2型別型別
- Word中巧用查詢/替換功能製作試卷(轉)
- PostgreSQL 查詢替換函式SQL函式
- redo切換頻率查詢