leading hint 可以指示oracle使用leading 中指定的表作为驱动表,
比如 正常的访问计划如下
SCOTT@yangdb> select e.ename, hiredate, b.comm
2 from emp e, bonus b
3 where e.ename = b.ename;
Execution Plan
----------------------------------------------------------
Plan hash value: 1125985041
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
2 from emp e, bonus b
3 where e.ename = b.ename;
Execution Plan
----------------------------------------------------------
Plan hash value: 1125985041
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
我们在leading 提示中指定 emp 表为驱动表
SCOTT@yangdb> select /*+ leading(e b) */ e.ename,hiredate,b.comm
----------------------------------------------------------
Plan hash value: 1842254584
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
SCOTT@yangdb> select /*+ leading(e b) */ e.ename,hiredate,b.comm
2 from emp e, bonus b
3 where e.ename = b.ename;
Execution Plan3 where e.ename = b.ename;
----------------------------------------------------------
Plan hash value: 1842254584
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
如结果执行计划中将emp 作为驱动表!
1 在leading 提示同时使用ordered hint,则leading hint无效
SCOTT@yangdb> select /*+ leading(b e) ordered */ e.ename,hiredate, b.comm
----------------------------------------------------------
Plan hash value: 1842254584
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
2 使用两个冲突的leading hint ,则oracle cbo会忽略所有的leading 提示!
SCOTT@yangdb> select /*+ leading(b e) leading(e b) */e.ename,hiredate, b.comm
----------------------------------------------------------
Plan hash value: 1125985041
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
SCOTT@yangdb> select /*+ leading(b e) ordered */ e.ename,hiredate, b.comm
2 from emp e, bonus b
3 where e.ename = b.ename;
Execution Plan3 where e.ename = b.ename;
----------------------------------------------------------
Plan hash value: 1842254584
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
2 使用两个冲突的leading hint ,则oracle cbo会忽略所有的leading 提示!
SCOTT@yangdb> select /*+ leading(b e) leading(e b) */e.ename,hiredate, b.comm
2 from emp e, bonus b
3 where e.ename = b.ename;
Execution Plan3 where e.ename = b.ename;
----------------------------------------------------------
Plan hash value: 1125985041
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
odered hint 可以指示oracle 使用from 关键字后面的表的顺序进行join连接!cbo会优先按照from 后面的表的顺序来进行join,当统计信息失效或者不准确的时候,效果会比较差,使用ordered 提示 使cbo优化器忽略解析过程中的表连接评估,从而避免Oracle产生错误的执行计划,或者强制Oracle按照我们指定的方式执行。
SCOTT@yangdb> select /*+ ordered */e.ename,hiredate,b.comm
2 from emp e,bonus b
3 where e.ename = b.ename;
Execution Plan
----------------------------------------------------------
Plan hash value: 1842254584
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
2 from emp e,bonus b
3 where e.ename = b.ename;
Execution Plan
----------------------------------------------------------
Plan hash value: 1842254584
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
SCOTT@yangdb> select /*+ ordered(b e)*/e.ename,hiredate,b.comm
2 from emp e,bonus b
3 where e.ename = b.ename;
Execution Plan
----------------------------------------------------------
Plan hash value: 1125985041
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 34 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 34 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS FULL| BONUS | 1 | 20 | 2 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 196 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."ENAME"="B"."ENAME")
SCOTT@yangdb>