20200909]為什麼執行計劃不是出現hash join semi.txt
[20200909]為什麼執行計劃不是出現hash join semi.txt
--//別人問的問題,為什麼執行計劃hash join semi,透過例子說明問題:
1.環境:
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0 12.2.0.1.0 Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production 0
2.測試:
SCOTT@test01p> alter session set statistics_level = all;
Session altered.
SCOTT@test01p> select * from emp where exists (select 1 from dept where dname='SALES' and dept.deptno=emp.deptno);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 500 30
7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30
7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30
7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 30
6 rows selected.
SCOTT@test01p> @ dpc '' advanced
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 8053b6kh802ky, child number 0
-------------------------------------
select * from emp where exists (select 1 from dept where dname='SALES'
and dept.deptno=emp.deptno)
Plan hash value: 367693176
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 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 | | | 5 (100)| | 6 |00:00:00.01 | 10 | | | |
|* 1 | HASH JOIN | | 1 | 5 | 255 | 5 (0)| 00:00:01 | 6 |00:00:00.01 | 10 | 2545K| 2545K| 785K (0)|
| 2 | TABLE ACCESS BY INDEX ROWID BATCHED| DEPT | 1 | 1 | 13 | 2 (0)| 00:00:01 | 1 |00:00:00.01 | 2 | | | |
|* 3 | INDEX RANGE SCAN | I_DEPT_DNAME | 1 | 1 | | 1 (0)| 00:00:01 | 1 |00:00:00.01 | 1 | | | |
| 4 | TABLE ACCESS FULL | EMP | 1 | 14 | 532 | 3 (0)| 00:00:01 | 14 |00:00:00.01 | 8 | | | |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$5DA710D3
2 - SEL$5DA710D3 / DEPT@SEL$2
3 - SEL$5DA710D3 / DEPT@SEL$2
4 - SEL$5DA710D3 / EMP@SEL$1
--//別人問為什麼不出現HASH JOIN semi.而是hash join,是連線順序的問題嗎?因為這樣dept是驅動表.
select /*+ leading(emp) */ * from emp where exists (select 1 from dept where dname='SALES' and dept.deptno=emp.deptno);
Plan hash value: 4254668763
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 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 | | | 5 (100)| | 6 |00:00:00.01 | 9 | | | |
|* 1 | HASH JOIN | | 1 | 5 | 255 | 5 (0)| 00:00:01 | 6 |00:00:00.01 | 9 | 1098K| 1098K| 664K (0)|
| 2 | TABLE ACCESS FULL | EMP | 1 | 14 | 532 | 3 (0)| 00:00:01 | 14 |00:00:00.01 | 7 | | | |
| 3 | TABLE ACCESS BY INDEX ROWID BATCHED| DEPT | 1 | 1 | 13 | 2 (0)| 00:00:01 | 1 |00:00:00.01 | 2 | | | |
|* 4 | INDEX RANGE SCAN | I_DEPT_DNAME | 1 | 1 | | 1 (0)| 00:00:01 | 1 |00:00:00.01 | 1 | | | |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--//一樣沒有出現HASH JOIN SEMI連線.因為dept.deptno是主鍵嗎?
3.繼續:
SCOTT@test01p> create table deptx as select * from dept ;
Table created.
select * from emp where exists (select 1 from deptx dept where dname='SALES' and dept.deptno=emp.deptno);
SCOTT@test01p> @ dpc '' advanced
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 5scp0hawkubrw, child number 0
-------------------------------------
select * from emp where exists (select 1 from deptx dept where
dname='SALES' and dept.deptno=emp.deptno)
Plan hash value: 1460795715
-------------------------------------------------------------------------------------------------------------------------------------------------
| 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 | | | 6 (100)| | 6 |00:00:00.01 | 17 | | | |
|* 1 | HASH JOIN SEMI | | 1 | 5 | 255 | 6 (0)| 00:00:01 | 6 |00:00:00.01 | 17 | 1098K| 1098K| 669K (0)|
| 2 | TABLE ACCESS FULL| EMP | 1 | 14 | 532 | 3 (0)| 00:00:01 | 14 |00:00:00.01 | 7 | | | |
|* 3 | TABLE ACCESS FULL| DEPTX | 1 | 1 | 13 | 3 (0)| 00:00:01 | 1 |00:00:00.01 | 10 | | | |
-------------------------------------------------------------------------------------------------------------------------------------------------
--//這次出現HASH JOIN SEMI.
SCOTT@test01p> create index i_deptx_dname on deptx(dname);
Index created.
select * from emp where exists (select 1 from deptx dept where dname='SALES' and dept.deptno=emp.deptno);
Plan hash value: 2867647663
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 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 | | | 5 (100)| | 6 |00:00:00.01 | 9 | | | |
|* 1 | HASH JOIN SEMI | | 1 | 5 | 255 | 5 (0)| 00:00:01 | 6 |00:00:00.01 | 9 | 1098K| 1098K| 691K (0)|
| 2 | TABLE ACCESS FULL | EMP | 1 | 14 | 532 | 3 (0)| 00:00:01 | 14 |00:00:00.01 | 7 | | | |
| 3 | TABLE ACCESS BY INDEX ROWID BATCHED| DEPTX | 1 | 1 | 13 | 2 (0)| 00:00:01 | 1 |00:00:00.01 | 2 | | | |
|* 4 | INDEX RANGE SCAN | I_DEPTX_DNAME | 1 | 1 | | 1 (0)| 00:00:01 | 1 |00:00:00.01 | 1 | | | |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--//這次也出現HASH JOIN SEMI.如果將dept作為驅動表呢?
select /*+ leading(dept@sub ) */ * from emp where exists (select /*+ qb_name(sub) */ 1 from deptx dept where dname='SALES' and dept.deptno=emp.deptno);
Plan hash value: 743985058
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 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 | | | 6 (100)| | 6 |00:00:00.01 | 46 | | | |
|* 1 | HASH JOIN | | 1 | 5 | 255 | 6 (17)| 00:00:01 | 6 |00:00:00.01 | 46 | 2545K| 2545K| 675K (0)|
| 2 | SORT UNIQUE | | 1 | 1 | 13 | 2 (0)| 00:00:01 | 1 |00:00:00.01 | 2 | 2048 | 2048 | 2048 (0)|
| 3 | TABLE ACCESS BY INDEX ROWID BATCHED| DEPTX | 1 | 1 | 13 | 2 (0)| 00:00:01 | 1 |00:00:00.01 | 2 | | | |
|* 4 | INDEX RANGE SCAN | I_DEPTX_DNAME | 1 | 1 | | 1 (0)| 00:00:01 | 1 |00:00:00.01 | 1 | | | |
| 5 | TABLE ACCESS FULL | EMP | 1 | 14 | 532 | 3 (0)| 00:00:01 | 14 |00:00:00.01 | 8 | | | |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--//可以發現這種情況,只要子查詢展開後,dept作為驅動表,Operation就不會出現HASH JOIN SEMI.
--//前面沒有出現hash join semi主要原因是dept.deptno是主鍵或者唯一索引.
SCOTT@test01p> create unique index i_deptx_deptno on deptx(deptno);
Index created.
select * from emp where exists (select 1 from deptx dept where dname='SALES' and dept.deptno=emp.deptno);
Plan hash value: 2939079003
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 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 | | | 5 (100)| | 6 |00:00:00.01 | 10 | | | |
|* 1 | HASH JOIN | | 1 | 5 | 255 | 5 (0)| 00:00:01 | 6 |00:00:00.01 | 10 | 2545K| 2545K| 710K (0)|
| 2 | TABLE ACCESS BY INDEX ROWID BATCHED| DEPTX | 1 | 1 | 13 | 2 (0)| 00:00:01 | 1 |00:00:00.01 | 2 | | | |
|* 3 | INDEX RANGE SCAN | I_DEPTX_DNAME | 1 | 1 | | 1 (0)| 00:00:01 | 1 |00:00:00.01 | 1 | | | |
| 4 | TABLE ACCESS FULL | EMP | 1 | 14 | 532 | 3 (0)| 00:00:01 | 14 |00:00:00.01 | 8 | | | |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--//這樣就不出現HASH JOIN SEMI.
--//平時自己也很少注意這些細節問題...
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2718394/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【sql調優之執行計劃】hash joinSQL
- 從Hash Join的執行計劃的細節中能看到點啥
- ORACLE:什麼是執行計劃Oracle
- 為什麼該SQL的執行計劃不走索引???SQL索引
- 執行緒join為什麼在解構函式中執行緒函式
- 查詢執行hash join出現ora-040300 (QERHJ hash-joi,kllcqas:kllsltba)的診斷案例
- 【sql調優之執行計劃】merge semi join and merge anti joinSQL
- 【sql調優之執行計劃】nested loops join and nested loop join outerSQLOOP
- 【sql調優之執行計劃】merge sort joinSQL
- 【sql調優之執行計劃】使用hint(四)Hints for JoinSQL
- [20230921]為什麼執行計劃不再awr中.txt
- 【專案管理經驗分享】為什麼專案計劃難以完美執行?專案管理
- 為什麼執行緒安全的List推薦使用CopyOnWriteArrayList,而不是Vector執行緒
- 執行計劃-1:獲取執行計劃
- ORACLE Hash JoinOracle
- 【sql調優之執行計劃】merge join cartesian and buffer sortSQL
- Redis作為單執行緒 為什麼我用它還是出現了超賣呢?Redis執行緒
- 執行計劃出現BITMAP CONVERSION TO ROWIDS的解釋!
- 執行計劃
- 為什麼進行統計分析執行效率反而更差呢?
- 怎麼穩固執行計劃<轉>
- HASH JOIN ,MERGE JOIN ,NESTED LOOPOOP
- 執行計劃中Row 數量為0
- HASH join詳解
- win10出現ls不是內部或外部命令也不是可執行怎麼恢復Win10
- 檢視執行計劃出現ORA-22992錯誤
- 決定一個SQL執行效率的是執行計劃, 而不是SQL的寫法SQL
- 資料庫實現原理#4(Hash Join)資料庫
- 分析執行計劃優化SQLORACLE的執行計劃(轉)優化SQLOracle
- oracle sqlprofile 固定執行計劃,並遷移執行計劃OracleSQL
- 【sql調優之執行計劃】獲取執行計劃SQL
- NEST LOOP改為HASH JOIN 效能提高6倍OOP
- SYBASE執行計劃
- MySQL 執行計劃MySql
- MySQL執行計劃MySql
- sql 執行計劃SQL
- ORACLE執行計劃Oracle
- nested loop,sort merge join,hash joinOOP