【oracle】leading vs ordered hint

简介: leading hint 可以指示oracle使用leading 中指定的表作为驱动表,比如 正常的访问计划如下SCOTT@yangdb> select e.
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")

我们在leading 提示中指定 emp 表为驱动表
SCOTT@yangdb> select /*+ leading(e b) */ 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")
如结果执行计划中将emp 作为驱动表!
1 在leading 提示同时使用ordered hint,则leading hint无效
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 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 使用两个冲突的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 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")

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")

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> 
目录
相关文章
|
11月前
|
数据库
解决which is not functionally dependent on columns in GROUP BY clause;...sql_mode=only_full_group_by
解决which is not functionally dependent on columns in GROUP BY clause;...sql_mode=only_full_group_by
234 0
1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause
1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause
198 0
1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause
|
SQL 数据库
Database specific hint in One order search
Database specific hint in One order search
Database specific hint in One order search
|
SQL Oracle 算法
Cost-based query transformation in Oracle
这篇paper主要介绍了Oracle从10g开始引入的CBQT(Cost Based Query Transformation)框架。虽然以Oracle历来的风格,无法期待它在paper中讨论很多细节,不过这篇还是可以让我们一窥Oracle对于query rewrite的处理思路和很多非常实用的query rewrite模式,对于开发优化器的同学很有参考意义。 值得一提的是,PolarDB目前也在做这方面的工作,而主要的参考正是这篇paper。此外这篇paper的思路和MemSQL optimizer中对query rewrite的处理思路非常接近,关于MemSQL optimizer的介绍可
312 0
Cost-based query transformation in Oracle
|
SQL Oracle 关系型数据库
聊聊Oracle 11g中的Reference Partition(上)
  Data Partition是Oracle早期提出的一项针对大数据对象的解决方案。经过若干版本的演变,Partition依然是目前比较流行、应用广泛并且接受程度较高的技术策略。
856 1
|
SQL Oracle 关系型数据库
ORACLE不可见索引(Invisible Indexes)
不可见索引概念   不可见索引(Invisible Index)是ORACLE 11g引入的新特性。不可见索引是会被优化器忽略的不可见索引,除非在会话或系统级别上将OPTIMIZER_USE_INVISIBLE_INDEXES初始化参数显式设置为TRUE。
1536 0
|
SQL Oracle 关系型数据库
|
SQL 关系型数据库 测试技术