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. CSS 背景

    CSS 背景属性用于定义HTML元素的背景. CSS 属性定义背影效果: background-color background-image background-repeat background- ...

  2. Lucene初步搜索

    Lucene在创立索引后,要进行搜索查询 搜索大概需要5部, 1,读取索引. 2,查询索引. 3,匹配数据. 4,封装匹配结果. 5,获取需要的值. 语言表达能力不好,大概就是分着几部吧. /** * ...

  3. 校省选赛第一场A题Cinema题解

    今天是学校省选的第一场比赛,0战绩收工,死死啃着A题来做,偏偏一直WA在TES1. 赛后,才发现,原来要freopen("input.txt","r",stdi ...

  4. [转] 小tips: 使用 等空格实现最小成本中文对齐 ---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4562 一.重见天日第 ...

  5. Object之魔术函数__toString() 直接输出对象引用时自动调用

    __toString()是快速获取对象的字符串信息的便捷方式 在直接输出对象引用时自动调用的方法. __toString()的作用 当我们调试程序时,需要知道是否得出正确的数据.比如打印一个对象时,看 ...

  6. PDF转图片 C# with Adobe API

    PDF转图片大概有十几种方式,褒贬不一,我就详细给大家说一下我认为效率最高的方式,使用Adobe官方的SDK 安装acrobat reader 9.0以上即可,勾选如下组件.

  7. 从string.size()和string.length()聊到长度的问题和一个关于数据结构定义的技巧

    最近工作中要查看一下string的长度,然后忘了是哪个函数,所以去网上搜了一搜,决定把网上学的和其他的一些有关长度的东西在这里汇总一下, 然后就有了此帖. string 是从c语言的char数组的概念 ...

  8. 实现windows和linux的NFS交互

    说明:本文是Omni-NFS-X Windows与Linux间通讯的另一种方式和在windows中配置使用NFS客户端的杂交篇 概述 windows/winnt4.0/win2000与Linux/Fr ...

  9. Ogre内部渲染流程分析系列

    come from:http://blog.csdn.net/weiqubo/article/details/6956005 要理解OGRE引擎,就要理解其中占很重要位置的 Renderable接口, ...

  10. 对Gearman中client,worker,jobserver的理解

    在gearman的官网http://gearman.org/有以下的一段说明 A Gearman powered application consists of three parts: a clie ...