[20160211]閉包傳遞3.txt
[20160211]閉包傳遞3.txt
--所謂閉包傳遞是指sql語句的謂詞條件A=B and B=C 可以推出 A=C. oracle 的 最佳化器能夠利用這個特性最佳化sql語句。
--前一陣子看電子電子書<Apress.Expert.Oracle.SQL.Optimization.Deployment.and.Statistics.1430259779.pdf>測試連結:
http://blog.itpub.net/267265/viewspace-1981803/
http://blog.itpub.net/267265/viewspace-1987668/
--晚上以前學習oracle最佳化時,我練習過透過hint控制4個表的連線順序問題.自己重複測試看看,難道4個表就沒有問題嗎?
1.環境:
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
-------------------- ----------- ---------------------------------------------------------------------------- ------
IBMPC/WIN_NT64-9.1.0 12.1.0.1.0 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 0
create table t1 as select rownum id,rownum||'t1' name from dual connect by level<=5;
create table t2 as select rownum+1 id,rownum||'t2' name from dual connect by level<=5;
create table t3 as select rownum+2 id,rownum||'t3' name from dual connect by level<=5;
create table t4 as select rownum+3 id,rownum||'t3' name from dual connect by level<=5;
-- 分析表.Method_Opt => 'FOR ALL COLUMNS SIZE 1 '
2.以前我記得我學習最佳化時,聽過一些最佳化的課程,裡面提到如何能很好的控制4個表的連線順序,能很好地解決一些問題.
我當時覺得很簡單,實際上就是使用提示leading ,SWAP_JOIN_INPUTS 就很好的控制連線順序.舉一個例子:
假設要使用t4->(t2,t3)->t1 這樣的連線順序,使用的提示是:
leading (t2 t3 t4 t1) SWAP_JOIN_INPUTS(t4)
--測試看看:
select /*+ leading (t2 t3 t4 t1) SWAP_JOIN_INPUTS(t4) */ * from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t3.id and t3.id=t4.id;
Plan hash value: 1146301945
--------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
--------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 12 (100)| | 2 |00:00:00.01 | 13 | | | |
|* 1 | HASH JOIN | | 1 | 5 | 140 | 12 (0)| 00:00:01 | 2 |00:00:00.01 | 13 | 1245K| 1245K| 557K (0)|
|* 2 | HASH JOIN | | 1 | 5 | 105 | 9 (0)| 00:00:01 | 3 |00:00:00.01 | 9 | 1888K| 1888K| 1093K (0)|
| 3 | TABLE ACCESS FULL | T4 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
|* 4 | HASH JOIN | | 1 | 5 | 70 | 6 (0)| 00:00:01 | 4 |00:00:00.01 | 6 | 1888K| 1888K| 1064K (0)|
| 5 | TABLE ACCESS FULL| T2 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 6 | TABLE ACCESS FULL| T3 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 7 | TABLE ACCESS FULL | T1 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 4 | | | |
--------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
3 - SEL$1 / T4@SEL$1
5 - SEL$1 / T2@SEL$1
6 - SEL$1 / T3@SEL$1
7 - SEL$1 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID"="T2"."ID")
2 - access("T3"."ID"="T4"."ID")
4 - access("T2"."ID"="T3"."ID")
3.當然我的問題要測試閉包問題,修改連線條件如下:
select /*+ leading (t2 t3 t4 t1) SWAP_JOIN_INPUTS(t4) */ * from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t4.id and t3.id=t4.id;
--修改t2.id=t3.id為t2.id=t4.id,這樣實際上暗含有t2.id=t3.id條件的.看看執行計劃:
Plan hash value: 2268006538
----------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 16 (100)| | 2 |00:00:00.01 | 13 | | | |
|* 1 | HASH JOIN | | 1 | 5 | 140 | 16 (0)| 00:00:01 | 2 |00:00:00.01 | 13 | 1245K| 1245K| 525K (0)|
|* 2 | HASH JOIN | | 1 | 5 | 105 | 13 (0)| 00:00:01 | 3 |00:00:00.01 | 9 | 1645K| 1645K| 1211K (0)|
| 3 | TABLE ACCESS FULL | T4 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 4 | MERGE JOIN CARTESIAN| | 1 | 25 | 350 | 10 (0)| 00:00:01 | 25 |00:00:00.01 | 6 | | | |
| 5 | TABLE ACCESS FULL | T2 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 6 | BUFFER SORT | | 5 | 5 | 35 | 7 (0)| 00:00:01 | 25 |00:00:00.01 | 3 | 2048 | 2048 | 2048 (0)|
| 7 | TABLE ACCESS FULL | T3 | 1 | 5 | 35 | 1 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 8 | TABLE ACCESS FULL | T1 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 4 | | | |
----------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
3 - SEL$1 / T4@SEL$1
5 - SEL$1 / T2@SEL$1
7 - SEL$1 / T3@SEL$1
8 - SEL$1 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID"="T2"."ID")
2 - access("T2"."ID"="T4"."ID" AND "T3"."ID"="T4"."ID")
--注意看T2,T3的連線使用了CARTESIAN,看來是自己以前忽略了這個問題.
4.按照作者觀點,測試加入一個常量看看:
select /*+ leading (t2 t3 t4 t1) SWAP_JOIN_INPUTS(t4) */ * from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t4.id and t3.id=t4.id and t1.id=4;
Plan hash value: 2268006538
----------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 12 (100)| | 1 |00:00:00.02 | 13 | | | |
|* 1 | HASH JOIN | | 1 | 1 | 28 | 12 (0)| 00:00:01 | 1 |00:00:00.02 | 13 | 1245K| 1245K| 697K (0)|
|* 2 | HASH JOIN | | 1 | 1 | 21 | 9 (0)| 00:00:01 | 1 |00:00:00.01 | 9 | 1645K| 1645K| 731K (0)|
|* 3 | TABLE ACCESS FULL | T4 | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 3 | | | |
| 4 | MERGE JOIN CARTESIAN| | 1 | 1 | 14 | 6 (0)| 00:00:01 | 1 |00:00:00.01 | 6 | | | |
|* 5 | TABLE ACCESS FULL | T2 | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 3 | | | |
| 6 | BUFFER SORT | | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 3 | 2048 | 2048 | 2048 (0)|
|* 7 | TABLE ACCESS FULL | T3 | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 3 | | | |
|* 8 | TABLE ACCESS FULL | T1 | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 4 | | | |
----------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
3 - SEL$1 / T4@SEL$1
5 - SEL$1 / T2@SEL$1
7 - SEL$1 / T3@SEL$1
8 - SEL$1 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID"="T2"."ID")
2 - access("T2"."ID"="T4"."ID" AND "T3"."ID"="T4"."ID")
3 - filter("T4"."ID"=4)
5 - filter("T2"."ID"=4)
7 - filter("T3"."ID"=4)
8 - filter("T1"."ID"=4)
-- 注意看裡面的filter條件.看來以後最佳化時要注意這個問題.
--所謂閉包傳遞是指sql語句的謂詞條件A=B and B=C 可以推出 A=C. oracle 的 最佳化器能夠利用這個特性最佳化sql語句。
--前一陣子看電子電子書<Apress.Expert.Oracle.SQL.Optimization.Deployment.and.Statistics.1430259779.pdf>測試連結:
http://blog.itpub.net/267265/viewspace-1981803/
http://blog.itpub.net/267265/viewspace-1987668/
--晚上以前學習oracle最佳化時,我練習過透過hint控制4個表的連線順序問題.自己重複測試看看,難道4個表就沒有問題嗎?
1.環境:
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
-------------------- ----------- ---------------------------------------------------------------------------- ------
IBMPC/WIN_NT64-9.1.0 12.1.0.1.0 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 0
create table t1 as select rownum id,rownum||'t1' name from dual connect by level<=5;
create table t2 as select rownum+1 id,rownum||'t2' name from dual connect by level<=5;
create table t3 as select rownum+2 id,rownum||'t3' name from dual connect by level<=5;
create table t4 as select rownum+3 id,rownum||'t3' name from dual connect by level<=5;
-- 分析表.Method_Opt => 'FOR ALL COLUMNS SIZE 1 '
2.以前我記得我學習最佳化時,聽過一些最佳化的課程,裡面提到如何能很好的控制4個表的連線順序,能很好地解決一些問題.
我當時覺得很簡單,實際上就是使用提示leading ,SWAP_JOIN_INPUTS 就很好的控制連線順序.舉一個例子:
假設要使用t4->(t2,t3)->t1 這樣的連線順序,使用的提示是:
leading (t2 t3 t4 t1) SWAP_JOIN_INPUTS(t4)
--測試看看:
select /*+ leading (t2 t3 t4 t1) SWAP_JOIN_INPUTS(t4) */ * from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t3.id and t3.id=t4.id;
Plan hash value: 1146301945
--------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
--------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 12 (100)| | 2 |00:00:00.01 | 13 | | | |
|* 1 | HASH JOIN | | 1 | 5 | 140 | 12 (0)| 00:00:01 | 2 |00:00:00.01 | 13 | 1245K| 1245K| 557K (0)|
|* 2 | HASH JOIN | | 1 | 5 | 105 | 9 (0)| 00:00:01 | 3 |00:00:00.01 | 9 | 1888K| 1888K| 1093K (0)|
| 3 | TABLE ACCESS FULL | T4 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
|* 4 | HASH JOIN | | 1 | 5 | 70 | 6 (0)| 00:00:01 | 4 |00:00:00.01 | 6 | 1888K| 1888K| 1064K (0)|
| 5 | TABLE ACCESS FULL| T2 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 6 | TABLE ACCESS FULL| T3 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 7 | TABLE ACCESS FULL | T1 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 4 | | | |
--------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
3 - SEL$1 / T4@SEL$1
5 - SEL$1 / T2@SEL$1
6 - SEL$1 / T3@SEL$1
7 - SEL$1 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID"="T2"."ID")
2 - access("T3"."ID"="T4"."ID")
4 - access("T2"."ID"="T3"."ID")
3.當然我的問題要測試閉包問題,修改連線條件如下:
select /*+ leading (t2 t3 t4 t1) SWAP_JOIN_INPUTS(t4) */ * from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t4.id and t3.id=t4.id;
--修改t2.id=t3.id為t2.id=t4.id,這樣實際上暗含有t2.id=t3.id條件的.看看執行計劃:
Plan hash value: 2268006538
----------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 16 (100)| | 2 |00:00:00.01 | 13 | | | |
|* 1 | HASH JOIN | | 1 | 5 | 140 | 16 (0)| 00:00:01 | 2 |00:00:00.01 | 13 | 1245K| 1245K| 525K (0)|
|* 2 | HASH JOIN | | 1 | 5 | 105 | 13 (0)| 00:00:01 | 3 |00:00:00.01 | 9 | 1645K| 1645K| 1211K (0)|
| 3 | TABLE ACCESS FULL | T4 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 4 | MERGE JOIN CARTESIAN| | 1 | 25 | 350 | 10 (0)| 00:00:01 | 25 |00:00:00.01 | 6 | | | |
| 5 | TABLE ACCESS FULL | T2 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 6 | BUFFER SORT | | 5 | 5 | 35 | 7 (0)| 00:00:01 | 25 |00:00:00.01 | 3 | 2048 | 2048 | 2048 (0)|
| 7 | TABLE ACCESS FULL | T3 | 1 | 5 | 35 | 1 (0)| 00:00:01 | 5 |00:00:00.01 | 3 | | | |
| 8 | TABLE ACCESS FULL | T1 | 1 | 5 | 35 | 3 (0)| 00:00:01 | 5 |00:00:00.01 | 4 | | | |
----------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
3 - SEL$1 / T4@SEL$1
5 - SEL$1 / T2@SEL$1
7 - SEL$1 / T3@SEL$1
8 - SEL$1 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID"="T2"."ID")
2 - access("T2"."ID"="T4"."ID" AND "T3"."ID"="T4"."ID")
--注意看T2,T3的連線使用了CARTESIAN,看來是自己以前忽略了這個問題.
4.按照作者觀點,測試加入一個常量看看:
select /*+ leading (t2 t3 t4 t1) SWAP_JOIN_INPUTS(t4) */ * from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t4.id and t3.id=t4.id and t1.id=4;
Plan hash value: 2268006538
----------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 12 (100)| | 1 |00:00:00.02 | 13 | | | |
|* 1 | HASH JOIN | | 1 | 1 | 28 | 12 (0)| 00:00:01 | 1 |00:00:00.02 | 13 | 1245K| 1245K| 697K (0)|
|* 2 | HASH JOIN | | 1 | 1 | 21 | 9 (0)| 00:00:01 | 1 |00:00:00.01 | 9 | 1645K| 1645K| 731K (0)|
|* 3 | TABLE ACCESS FULL | T4 | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 3 | | | |
| 4 | MERGE JOIN CARTESIAN| | 1 | 1 | 14 | 6 (0)| 00:00:01 | 1 |00:00:00.01 | 6 | | | |
|* 5 | TABLE ACCESS FULL | T2 | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 3 | | | |
| 6 | BUFFER SORT | | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 3 | 2048 | 2048 | 2048 (0)|
|* 7 | TABLE ACCESS FULL | T3 | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 3 | | | |
|* 8 | TABLE ACCESS FULL | T1 | 1 | 1 | 7 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 4 | | | |
----------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
3 - SEL$1 / T4@SEL$1
5 - SEL$1 / T2@SEL$1
7 - SEL$1 / T3@SEL$1
8 - SEL$1 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID"="T2"."ID")
2 - access("T2"."ID"="T4"."ID" AND "T3"."ID"="T4"."ID")
3 - filter("T4"."ID"=4)
5 - filter("T2"."ID"=4)
7 - filter("T3"."ID"=4)
8 - filter("T1"."ID"=4)
-- 注意看裡面的filter條件.看來以後最佳化時要注意這個問題.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-1987848/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 利用閉包傳遞引數
- POJ 3360-Cow Contest(傳遞閉包)
- [20160213]閉包傳遞5.txt
- [20160212]閉包傳遞4.txt
- [20160210]閉包傳遞2.txt
- POJ3660 Cow Contest【Floyd演算法 傳遞閉包】演算法
- [20160125]閉包傳遞問題.txt
- swift 閉包傳值Swift
- poj2594Treasure Exploration【最小路徑覆蓋+floyd傳遞閉包】
- JS函式表示式——函式遞迴、閉包JS函式遞迴
- 值傳遞與引用傳遞
- 值傳遞和引用傳遞
- 閉包
- BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比賽:floyd傳遞閉包
- 用閉包替換遞迴實現斐波拉契數列遞迴
- 閉包 | 淺談JavaScript閉包問題JavaScript
- Swift-逃逸閉包、自動閉包Swift
- Windows 10傳遞最佳化功能有什麼用 Windows 10傳遞最佳化功能怎麼關閉Windows
- JavaScript的值傳遞和引用傳遞JavaScript
- 快速搞懂值傳遞與引用傳遞
- Java的值傳遞和引用傳遞Java
- Python3之從遞迴到閉包再到裝飾器Python遞迴
- 【集合論】關係閉包 ( 關係閉包求法 | 關係圖求閉包 | 關係矩陣求閉包 | 閉包運算與關係性質 | 閉包複合運算 )矩陣
- PHP 閉包PHP
- JavaScript閉包JavaScript
- Golang閉包Golang
- golang 閉包Golang
- JavaScript 閉包JavaScript
- 理解“閉包”
- Swift 閉包Swift
- Swift,閉包Swift
- 理解閉包
- Swift - 閉包Swift
- lisp 閉包Lisp
- JavaScript - 閉包JavaScript
- go 值傳遞和地址傳遞的例子Go
- Java - 是值傳遞還是引用傳遞Java
- Java中的值傳遞和引用傳遞Java