三大连接的使用限制
1 hash Join的连接限制
hash join 不支持连接条件是大于、小于、不等于和like的场景. 因为hash join是一种经典的等值算法。
merge sort join 支持连接条件是大于、小于的场景,但是不支持不等于和like的场景。
1 hash Join的连接限制
hash join 不支持连接条件是大于、小于、不等于和like的场景. 因为hash join是一种经典的等值算法。
语句1 :连接条件为大于,结果无法根据hint走hash join
set linesize 1000
set autotrace traceonly explain
select /*+ leading(t1) use_hash(t2)*/ * from t1,t2 where t1.id>t2.t1_id and t1.n=9;
ZBB@test>select /*+ leading(t1) use_hash(t2)*/ * from t1,t2 where t1.id>t2.t1_id and t1.n=9;
Execution Plan
----------------------------------------------------------
Plan hash value: 1967407726
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5591 | 21M| 276 (1)| 00:00:01 |
| 1 | NESTED LOOPS | | 5591 | 21M| 276 (1)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| T1 | 1 | 2028 | 3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| T2 | 5591 | 10M| 273 (1)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("T1"."N"=9)
3 - filter("T1"."ID">"T2"."T1_ID")
Note
-----
- dynamic sampling used for this statement (level=2)
语句2 ,当连接条件为小于时,结果无法根据hint走hash join
select /*+ leading(t1) use_hash(t2)*/ * from t1,t2 where t1.id<t2.t1_id and t1.n=9;
ZBB@test>select /*+ leading(t1) use_hash(t2)*/ * from t1,t2 where t1.id<t2.t1_id and t1.n=9;
Execution Plan
----------------------------------------------------------
Plan hash value: 1967407726
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5591 | 21M| 276 (1)| 00:00:01 |
| 1 | NESTED LOOPS | | 5591 | 21M| 276 (1)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| T1 | 1 | 2028 | 3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| T2 | 5591 | 10M| 273 (1)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("T1"."N"=9)
3 - filter("T1"."ID"<"T2"."T1_ID")
Note
-----
- dynamic sampling used for this statement (level=2)
语句3 ,当连接条件为不等于的时候,无法根据hint走hash join
select /*+ leading(t1) use_hash(t2)*/ * from t1,t2 where t1.id<>t2.t1_id and t1.n=9;
ZBB@test>select /*+ leading(t1) use_hash(t2)*/ * from t1,t2 where t1.id<>t2.t1_id and t1.n=9;
Execution Plan
----------------------------------------------------------
Plan hash value: 1967407726
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 111K| 433M| 276 (1)| 00:00:01 |
| 1 | NESTED LOOPS | | 111K| 433M| 276 (1)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| T1 | 1 | 2028 | 3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| T2 | 111K| 217M| 273 (1)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("T1"."N"=9)
3 - filter("T1"."ID"<>"T2"."T1_ID")
Note
-----
- dynamic sampling used for this statement (level=2)
ZBB@test>
语句4,当连接条件为like的时候,无法根据hint走hash join
select /*+ leading(t1) use_hash(t2)*/ * from t1,t2 where t1.id like t2.t1_id and t1.n=9; --
ZBB@test>select /*+ leading(t1) use_hash(t2)*/ * from t1,t2 where t1.id like t2. t1_id and t1.n=9;
Execution Plan
----------------------------------------------------------
Plan hash value: 1967407726
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5591 | 21M| 276 (1)| 00:00:01 |
| 1 | NESTED LOOPS | | 5591 | 21M| 276 (1)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| T1 | 1 | 2028 | 3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| T2 | 5591 | 10M| 273 (1)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("T1"."N"=9)
3 - filter(TO_CHAR("T1"."ID") LIKE TO_CHAR("T2"."T1_ID"))
Note
-----
- dynamic sampling used for this statement (level=2)
ZBB@test>
2 排序合并连接的限制
merge sort join 支持连接条件是大于、小于的场景,但是不支持不等于和like的场景。
语句1 当条件为大于的时候,merge sort join支持这个算法,可以根据hint走merge sort join
set autotrace traceonly explain
select /*+ leading(t1) use_merge(t2)*/ * from t1,t2 where t1.id>t2.t1_id and t1.n=9;
ZBB@test>select /*+ leading(t1) use_merge(t2)*/ * from t1,t2 where t1.id>t2.t1_id and t1.n=9;
Execution Plan
----------------------------------------------------------
Plan hash value: 412793182
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5591 | 21M| | 47920 (1)| 00:00:11 |
| 1 | MERGE JOIN | | 5591 | 21M| | 47920 (1)| 00:00:11 |
| 2 | SORT JOIN | | 1 | 2028 | | 4 (25)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| T1 | 1 | 2028 | | 3 (0)| 00:00:01 |
|* 4 | SORT JOIN | | 111K| 217M| 582M| 47916 (1)| 00:00:11 |
| 5 | TABLE ACCESS FULL| T2 | 111K| 217M| | 273 (1)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("T1"."N"=9)
4 - access(INTERNAL_FUNCTION("T1"."ID")>INTERNAL_FUNCTION("T2"."T1_ID"))
filter(INTERNAL_FUNCTION("T1"."ID")>INTERNAL_FUNCTION("T2"."T1_ID"))
Note
-----
- dynamic sampling used for this statement (level=2)
语句2 ,条件为小于
select /*+ leading(t1) use_merge(t2)*/ * from t1,t2 where t1.id<t2.t1_id and t1.n=9;
ZBB@test>select /*+ leading(t1) use_merge(t2)*/ * from t1,t2 where t1.id<t2.t1_id and t1.n=9;
Execution Plan
----------------------------------------------------------
Plan hash value: 412793182
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 99949 | 11M| | 1849 (1)| 00:00:01 |
| 1 | MERGE JOIN | | 99949 | 11M| | 1849 (1)| 00:00:01 |
| 2 | SORT JOIN | | 1 | 57 | | 4 (25)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| T1 | 1 | 57 | | 3 (0)| 00:00:01 |
|* 4 | SORT JOIN | | 99999 | 6445K| 15M| 1845 (1)| 00:00:01 |
| 5 | TABLE ACCESS FULL| T2 | 99999 | 6445K| | 273 (1)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("T1"."N"=9)
4 - access("T1"."ID"<"T2"."T1_ID")
filter("T1"."ID"<"T2"."T1_ID")
语句3 当条件为不等于
select /*+ leading(t1) use_merge(t2)*/ * from t1,t2 where t1.id<>t2.t1_id and t1.n=9;
ZBB@test>select /*+ leading(t1) use_merge(t2)*/ * from t1,t2 where t1.id<>t2.t1_id and t1.n=9;
Execution Plan
----------------------------------------------------------
Plan hash value: 1967407726
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 99998 | 11M| 276 (1)| 00:00:01 |
| 1 | NESTED LOOPS | | 99998 | 11M| 276 (1)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| T1 | 1 | 57 | 3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| T2 | 99998 | 6445K| 273 (1)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("T1"."N"=9)
3 - filter("T1"."ID"<>"T2"."T1_ID")
ZBB@test>
语句4 当条件为like
select /*+ leading(t1) use_merge(t2)*/ * from t1,t2 where t1.id like t2.t1_id and t1.n=9;
ZBB@test>select /*+ leading(t1) use_merge(t2)*/ * from t1,t2 where t1.id like t2.t1_id and t1.n=9;
Execution Plan
----------------------------------------------------------
Plan hash value: 1967407726
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5000 | 600K| 276 (1)| 00:00:01 |
| 1 | NESTED LOOPS | | 5000 | 600K| 276 (1)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| T1 | 1 | 57 | 3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| T2 | 5000 | 322K| 273 (1)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("T1"."N"=9)
3 - filter(TO_CHAR("T1"."ID") LIKE TO_CHAR("T2"."T1_ID"))
ZBB@test>
3 nested Loop 的连接限制
从上面的例子可以看到。当hint提示不起作用的时候,走的就是nested loop. 所以,nested loop join是没有任何限制的 。
END