swap_join_inputs是針對(duì)哈希連接的hint,它的含義是讓優(yōu)化器交換原哈希連接的驅(qū)動(dòng)表和被驅(qū)動(dòng)表的順序,即在依然走哈希連接的情況下讓原哈希連接的驅(qū)動(dòng)表變被驅(qū)動(dòng)表,讓原哈希連接的被驅(qū)動(dòng)表變?yōu)轵?qū)動(dòng)表。
成都創(chuàng)新互聯(lián)公司是專業(yè)的郊區(qū)網(wǎng)站建設(shè)公司,郊區(qū)接單;提供成都網(wǎng)站建設(shè)、做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行郊區(qū)網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
注意,在swap_join_inputs hint中指定的目標(biāo)表應(yīng)該是原哈希連接中的被驅(qū)動(dòng)表,否則oracle會(huì)忽略該hint。
/*+ swap_join_inputs(原哈希連接的被驅(qū)動(dòng)表) */
其使用范例如下:
select /*+ leading(dept) use_hash(emp) swap_join_intputs(emp) */ * from emp,dept where emp.deptno=dept.deptno
測(cè)試案例:
SCOTT@ORA12C> create table t1 as select * from dba_objects where rownum<2; Table created. SCOTT@ORA12C> create table t2 as select * from dba_objects where rownum<12; Table created. SCOTT@ORA12C> create table t3 as select * from dba_objects where rownum<22; Table created.
收集統(tǒng)計(jì)信息:
SCOTT@ORA12C> exec dbms_stats.gather_table_stats(ownname => 'SCOTT',tabname => 'T1',estimate_percent => 100,cascade => true,method_opt => 'for all columns size 1',no_invalidate => false); PL/SQL procedure successfully completed. SCOTT@ORA12C> exec dbms_stats.gather_table_stats(ownname => 'SCOTT',tabname => 'T2',estimate_percent => 100,cascade => true,method_opt => 'for all columns size 1',no_invalidate => false); PL/SQL procedure successfully completed. SCOTT@ORA12C> exec dbms_stats.gather_table_stats(ownname => 'SCOTT',tabname => 'T3',estimate_percent => 100,cascade => true,method_opt => 'for all columns size 1',no_invalidate => false); PL/SQL procedure successfully completed.
3個(gè)表的記錄如下:
SCOTT@ORA12C> select count(*) from t1; COUNT(*) ----------------- 1 1 row selected. SCOTT@ORA12C> select count(*) from t2; COUNT(*) ----------------- 11 1 row selected. SCOTT@ORA12C> select count(*) from t3; COUNT(*) ----------------- 21 1 row selected.
現(xiàn)在我們來(lái)讓表T2和T3做哈希連接,由于T3表的記錄數(shù)比T2表的記錄數(shù)多,所以這里指定T3為哈希連接的被驅(qū)動(dòng)表:
select /*+ ordered use_hash(t3) */ t2.object_name,t3.object_type 2 from t2,t3 where t2.object_id=t3.object_id; Execution Plan ---------------------------------------------------------- Plan hash value: 1730954469 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 11 | 220 |6 (0)| 00:00:01 | |* 1 | HASH JOIN | | 11 | 220 |6 (0)| 00:00:01 | | 2 | TABLE ACCESS FULL| T2 | 11 | 110 |3 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| T3 | 21 | 210 |3 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T2"."OBJECT_ID"="T3"."OBJECT_ID")
可以看到,上述SQL的執(zhí)行計(jì)劃現(xiàn)在走的是哈希連接,并且被驅(qū)動(dòng)表示表T3.
如果我們想讓哈希連接的被驅(qū)動(dòng)表由T3變成T2,可以在上述sql加入swap_join_inputs hint:
select /*+ ordered use_hash(t3) swap_join_inputs(t3) */ t2.object_name,t3.object_type 2 from t2,t3 where t2.object_id=t3.object_id; Execution Plan ---------------------------------------------------------- Plan hash value: 1723280936 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 11 | 220 |6 (0)| 00:00:01 | |* 1 | HASH JOIN | | 11 | 220 |6 (0)| 00:00:01 | | 2 | TABLE ACCESS FULL| T3 | 21 | 210 |3 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| T2 | 11 | 110 |3 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T2"."OBJECT_ID"="T3"."OBJECT_ID")
用leading(t3) use_hash(t2)也可以同樣達(dá)到目的:
select /*+ leading(t3) use_hash(t2) */ t2.object_name,t3.object_type 2 from t2,t3 where t2.object_id=t3.object_id; Execution Plan ---------------------------------------------------------- Plan hash value: 1723280936 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 11 | 220 |6 (0)| 00:00:01 | |* 1 | HASH JOIN | | 11 | 220 |6 (0)| 00:00:01 | | 2 | TABLE ACCESS FULL| T3 | 21 | 210 |3 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| T2 | 11 | 110 |3 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T2"."OBJECT_ID"="T3"."OBJECT_ID")
由此可見(jiàn)在兩個(gè)表關(guān)聯(lián)的時(shí)候,可以用其他hint代替swap_join_inputs來(lái)達(dá)到相同的目的:
那么多表關(guān)聯(lián)呢:
select /*+ ordered use_hash(t3) */ t1.owner,t2.object_name,t3.object_type 2 from t2,t3,t1 where t2.object_id=t3.object_id and t1.object_type=t3.object_type; Execution Plan ---------------------------------------------------------- Plan hash value: 98820498 ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 4 | 120 | 9 (0)| 00:00:01 | |* 1 | HASH JOIN | | 4 | 120 | 9 (0)| 00:00:01 | |* 2 | HASH JOIN | |11 | 220 | 6 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| T2 |11 | 110 | 3 (0)| 00:00:01 | | 4 | TABLE ACCESS FULL| T3 |21 | 210 | 3 (0)| 00:00:01 | | 5 | TABLE ACCESS FULL | T1 | 1 |10 | 3 (0)| 00:00:01 | ---------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T1"."OBJECT_TYPE"="T3"."OBJECT_TYPE") 2 - access("T2"."OBJECT_ID"="T3"."OBJECT_ID"
可以看到,現(xiàn)在上述sql的執(zhí)行計(jì)劃是先由表T2和表T3做哈希連接,然后將他們做哈希連接的連接結(jié)果集再和表T1做一次哈希連接。
表T1的記錄數(shù)為1,表T2的記錄數(shù)為11,表T3的記錄數(shù)為21,所以當(dāng)表的T2和T3做哈希連接時(shí),記錄數(shù)多的表T3應(yīng)該是被驅(qū)動(dòng)表,這是因?yàn)槲覀冊(cè)谏鲜鰏ql中使用了ordered hint和use_hash HINT指定表T3作為表T2和T3連接的時(shí)的被驅(qū)動(dòng)表,所以oracle這里選擇了表T2和T3做哈希連接,并且選擇了表T3作為該哈希連接的被驅(qū)動(dòng)表,這是沒(méi)有問(wèn)題的,現(xiàn)在問(wèn)題在于表T1的記錄數(shù)僅為1,所以當(dāng)表T2和T3做哈希連接的結(jié)果再和表T1做哈希連接時(shí),表T1應(yīng)該是驅(qū)動(dòng)表,而不是在上述執(zhí)行計(jì)劃里顯示的那樣作為第二個(gè)哈希連接的被驅(qū)動(dòng)表。
使用下面HINT:
select /*+ ordered use_hash(t3) */ t1.owner,t2.object_name,t3.object_type 2 from t1,t2,t3 where t2.object_id=t3.object_id and t1.object_type=t3.object_type; Execution Plan ---------------------------------------------------------- Plan hash value: 38266800 ------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 4 | 120 | 9 (0)| 00:00:01 | |* 1 | HASH JOIN | | 4 | 120 | 9 (0)| 00:00:01 | | 2 | MERGE JOIN CARTESIAN| | 11 | 220 | 6 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL | T1 | 1 | 10 | 3 (0)| 00:00:01 | | 4 | BUFFER SORT | | 11 | 110 | 3 (0)| 00:00:01 | | 5 | TABLE ACCESS FULL | T2 | 11 | 110 | 3 (0)| 00:00:01 | | 6 | TABLE ACCESS FULL | T3 | 21 | 210 | 3 (0)| 00:00:01 | ------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T2"."OBJECT_ID"="T3"."OBJECT_ID" AND "T1"."OBJECT_TYPE"="T3"."OBJECT_TYPE")
select /*+ leading(t1) use_hash(t3) */ t1.owner,t2.object_name,t3.object_type 2 from t1,t2,t3 where t2.object_id=t3.object_id and t1.object_type=t3.object_type; Execution Plan ---------------------------------------------------------- Plan hash value: 2308542799 ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 7 | 210 | 9 (0)| 00:00:01 | |* 1 | HASH JOIN | | 7 | 210 | 9 (0)| 00:00:01 | |* 2 | HASH JOIN | | 7 | 140 | 6 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| T1 | 1 |10 | 3 (0)| 00:00:01 | | 4 | TABLE ACCESS FULL| T3 |21 | 210 | 3 (0)| 00:00:01 | | 5 | TABLE ACCESS FULL | T2 |11 | 110 | 3 (0)| 00:00:01 | ---------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T2"."OBJECT_ID"="T3"."OBJECT_ID") 2 - access("T1"."OBJECT_TYPE"="T3"."OBJECT_TYPE")
加入以下hint,就解決:
SELECT /*+ ordered use_hash(t3) swap_join_inputs(t1) */ t1.owner, t2.object_name, t3.object_type FROM t2, t3, t1 WHERE t2.object_id = t3.object_id 5 AND t1.object_type = t3.object_type; Execution Plan ---------------------------------------------------------- Plan hash value: 3071514789 ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 4 | 120 | 9 (0)| 00:00:01 | |* 1 | HASH JOIN | | 4 | 120 | 9 (0)| 00:00:01 | | 2 | TABLE ACCESS FULL | T1 | 1 |10 | 3 (0)| 00:00:01 | |* 3 | HASH JOIN | |11 | 220 | 6 (0)| 00:00:01 | | 4 | TABLE ACCESS FULL| T2 |11 | 110 | 3 (0)| 00:00:01 | | 5 | TABLE ACCESS FULL| T3 |21 | 210 | 3 (0)| 00:00:01 | ---------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T1"."OBJECT_TYPE"="T3"."OBJECT_TYPE") 3 - access("T2"."OBJECT_ID"="T3"."OBJECT_ID")
文章題目:SWAP_JOIN_INPUTSOracleHint
標(biāo)題路徑:http://aaarwkj.com/article36/pegdpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、自適應(yīng)網(wǎng)站、定制網(wǎng)站、App設(shè)計(jì)、軟件開(kāi)發(fā)、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容