oracle hints

今天是2013-10-08,对于oracle hint有很多,具体可以参考联机手册:

http://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements006.htm#BABIJGJF

http://docs.oracle.com/cd/E11882_01/server.112/e41573/hintsref.htm#PFGRF501

刚刚开始,我进行hash join连接发现如下:

SQL> select /*+use_hash(emp)*/ empno from emp,dept where dept.deptno=emp.deptno;

Execution Plan
----------------------------------------------------------
Plan hash value: 716400937 -----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 140 | 1 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 14 | 140 | 1 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN | IND_EMP | 14 | 98 | 1 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN| REVERSE_INDEX | 1 | 3 | 0 (0)| 00:00:01 |
----------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 3 - access("DEPT"."DEPTNO"="EMP"."DEPTNO") SQL>

我明明指定的是emp做为驱动表然后进行hash join,但是不行,需要指定另个表,但是use_hash不能规定优化器来选择驱动表。

eg:

SQL> select /*+use_hash(emp,dept)*/ empno from emp,dept where dept.deptno=emp.deptno;

Execution Plan
----------------------------------------------------------
Plan hash value: 2255485930 ----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 140 | 2 (0)| 00:00:01 |
|* 1 | HASH JOIN | | 14 | 140 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN| REVERSE_INDEX | 4 | 12 | 1 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN| IND_EMP | 14 | 98 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("DEPT"."DEPTNO"="EMP"."DEPTNO") SQL> select /*+use_hash(dept,emp)*/ empno from dept,emp where dept.deptno=emp.deptno; Execution Plan
----------------------------------------------------------
Plan hash value: 2255485930 ----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 140 | 2 (0)| 00:00:01 |
|* 1 | HASH JOIN | | 14 | 140 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN| REVERSE_INDEX | 4 | 12 | 1 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN| IND_EMP | 14 | 98 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("DEPT"."DEPTNO"="EMP"."DEPTNO") SQL>

我们可以选择使用ordered或是leading来指定optimizer选择哪个表为驱动表。

note:

The LEADING hint instructs the optimizer to use the specified set of tables as the prefix in the execution plan. This hint is more versatile than the ORDERED hint.

The LEADING hint is ignored if the tables specified cannot be joined first in the order specified because of dependencies in the join graph. If you specify two or more conflicting LEADING hints, then all of them are ignored. If you specify the ORDERED hint, it overrides all LEADING hints.

The ORDERED hint instructs Oracle to join tables in the order in which they appear in the FROM clause. Oracle recommends that you use the LEADING hint, which is more versatile than the ORDERED hint.

When you omit the ORDERED hint from a SQL statement requiring a join, the optimizer chooses the order in which to join the tables. You might want to use the ORDERED hint to specify a join order if you know something that the optimizer does not know about the number of rows selected from each table. Such information lets you choose an inner and outer table better than the optimizer could.

eg:

SQL> select /*+leading(emp) use_hash(dept,emp)*/ empno from dept ,emp where dept.deptno=emp.deptno;

Execution Plan
----------------------------------------------------------
Plan hash value: 929644576 ----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 140 | 2 (0)| 00:00:01 |
|* 1 | HASH JOIN | | 14 | 140 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN| IND_EMP | 14 | 98 | 1 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN| REVERSE_INDEX | 4 | 12 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("DEPT"."DEPTNO"="EMP"."DEPTNO") SQL> select /*+leading(dept) use_hash(dept,emp)*/ empno from dept ,emp where dept.deptno=emp.deptno; Execution Plan
----------------------------------------------------------
Plan hash value: 2255485930 ----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 140 | 2 (0)| 00:00:01 |
|* 1 | HASH JOIN | | 14 | 140 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN| REVERSE_INDEX | 4 | 12 | 1 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN| IND_EMP | 14 | 98 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("DEPT"."DEPTNO"="EMP"."DEPTNO") SQL>
SQL> select /*+ordered use_hash(emp,dept)*/ empno from emp,dept where emp.deptno=dept.deptno;

Execution Plan
----------------------------------------------------------
Plan hash value: 929644576 ----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 140 | 2 (0)| 00:00:01 |
|* 1 | HASH JOIN | | 14 | 140 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN| IND_EMP | 14 | 98 | 1 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN| REVERSE_INDEX | 4 | 12 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("EMP"."DEPTNO"="DEPT"."DEPTNO") SQL> select /*+ordered use_hash(emp,dept)*/ empno from dept,emp where emp.deptno=dept.deptno; Execution Plan
----------------------------------------------------------
Plan hash value: 2255485930 ----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 140 | 2 (0)| 00:00:01 |
|* 1 | HASH JOIN | | 14 | 140 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN| REVERSE_INDEX | 4 | 12 | 1 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN| IND_EMP | 14 | 98 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("EMP"."DEPTNO"="DEPT"."DEPTNO") SQL>

oracle hints的更多相关文章

  1. Oracle Hints详解

    在向大家详细介绍Oracle Hints之前,首先让大家了解下Oracle Hints是什么,然后全面介绍Oracle Hints,希望对大家有用.基于代价的优化器是很聪明的,在绝大多数情况下它会选择 ...

  2. Oracle Hints具体解释

    在向大家具体介绍Oracle Hints之前,首先让大家了解下Oracle Hints是什么,然后全面介绍Oracle Hints,希望对大家实用.基于代价的优化器是非常聪明的,在绝大多数情况下它会选 ...

  3. 普及下Oracle hints语法

    普及下Oracle hints的语法:{DELETE|INSERT|SELECT|UPDATE} /*+ hint [text] [hint[text]]... */ 1.hint只能出现在诸如sel ...

  4. Oracle Hints详细解释

    特别介绍给大家Oracle Hints之前,让我们知道下Oracle Hints什么,然后好Oracle Hints,我们希望实际.基于成本的优化器是很聪明,在大多数情况下,将选择正确的优化,减少DB ...

  5. 常用oracle hints

    在SQL语句优化过程中,经常会用到hint, 以下是在SQL优化过程中常见Oracle中"HINT"的30个用法 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方 ...

  6. 详解Oracle hints PQ_DISTRIBUTE

    PQ_DISTRIBUTE是并行的hints中稍微复杂一点的一个 下面就这个hints做以下说明: 1.使用格式 /+ PQ_DISTRIBUTE(tablespec outer_distributi ...

  7. Oracle数据库基础知识

    oracle数据库plsql developer   目录(?)[-] 一     SQL基础知识 创建删除数据库 创建删除修改表 添加修改删除列 oracle cascade用法 添加删除约束主键外 ...

  8. Oracle提示大全

    Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明误,选择了很差的执行计划,使某个语句的执行变得奇慢无比. 此时就需要DBA进行 ...

  9. SQL调优 - Hints指定索引 解决慢查询案例

    背景 每当交易高峰时期,可能会暴露一些平时无法发现的问题,机遇和挑战并存.下面聊聊最近解决的一个案例,因为执行计划走错导致慢查询,进而引发应用线程阻塞.线程池爆满,最后应用功能瘫痪.如何标本兼治的解决 ...

随机推荐

  1. ios7--系统自带的向右滑动手势返回上一个界面

    当从控制器A push到控制器B,我们返回控制器A,除了使用按钮返回 [self.navigationController pushViewController:Vc animated:YES]; 还 ...

  2. power shell upload file to azure storage

    # Azure subscription-specific variables. $storageAccountName = "storage-account-name" $con ...

  3. 防止sql注入式攻击 SQL注入学习——三层架构

    解决方案是:1.首先在UI录入时,要控制数据的类型和长度.防止SQL注入式攻击,系统提供检测注入式攻击的函数,一旦检测出注入式攻击,该数据即不能提交:2.业务逻辑层控制,通过在方法内部将SQL关键字用 ...

  4. Köln-keith jarrett

    在火车上遇到一男生,带着他弟弟.他弟弟跑来跑去的,他就安稳地坐在下铺看书,不知道是哪种语言. 我们都是在北京下车. 第二天在王府井吃饭,姐姐带我吃西餐.我又看到他,跟一个阿姨一起吃饭. 吃饭的时候姐姐 ...

  5. 时区之痒 - 从手机GPS模块获取的时间,真的是北京时间么?

    去年互联网地图行业开始引入众包模式,国内比较大的地图商,比如四维图新.高德地图.百度地图纷纷开始推出UGC应用,众包给用户采集门址.公交站等信息,并按照工作量给与采集者一定的回报.我曾经玩过某德推出的 ...

  6. 平衡搜索树(三) B-Tree

    B树的简介 B 树是为了磁盘或其它存储设备而设计的一种多叉平衡查找树.与红黑树很相似,但在降低磁盘I/0操作方面要更好一些(树的深度较低).许多数据库系统都一般使用B树或者B树的各种变形结构.B树与红 ...

  7. boost 1.56.0 编译

    编译步骤及参数说明: http://www.cnblogs.com/zhcncn/p/3950477.html 编译64位版本: http://www.cnblogs.com/codingmylife ...

  8. 授权(Authorization)

    介绍 除了认证服务,laravel还提供了授权服务,laravel同样提供了一个简单的方式去组织授权的逻辑来控制资源的访问.我们提供了各种各样的方法协助你们组织授权的逻辑,这些都在下面的文档之中. 定 ...

  9. v880 debug

    zte v880手机,ubuntu中配置真机调试, 1.开启手机调试模式2.增加/etc/udev/rules.d/51-android.rules. 内容如下:SUBSYSTEM=="us ...

  10. xadmin的插件机制

    xadmin的视图方法中如果加了@filter_hook 标记的都可以作为插件的钩子函数. 例如在ListAdminView类中有许多加了上述标记的方法, @filter_hook def get_c ...