SQL hint作用

1、写HINT目的

手工指定SQL语句的执行计划

  hints是oracle提供的一种机制,用来告诉优化器按照我们的告诉它的方式生成执行计划。我们可以用hints来实现: 

  1) 使用的优化器的类型 

  2) 基于代价的优化器的优化目标,是all_rows还是first_rows。 

  3) 表的访问路径,是全表扫描,还是索引扫描,还是直接利用rowid。 

  4) 表之间的连接类型 

  5) 表之间的连接顺序 

  6) 语句的并行程度 



2、HINT可以基于以下规则产生作用

表连接的顺序、表连接的方法、访问路径、并行度



3、HINT应用范围

dml语句

查询语句



4、语法

{DELETE|INSERT|SELECT|UPDATE} /*+ hint [text] [hint[text]]... */

or

{DELETE|INSERT|SELECT|UPDATE} --+ hint [text] [hint[text]]...

如果语(句)法不对,则ORACLE会自动忽略所写的HINT,不报错



5、指定优化器模式的HINT

RULE:不管是否有统计信息,都将采用基于规则进行优化;

CHOOSE:只要被访问的数据中有一个表有统计信息,就将采用基于代价的方式进行优化;

FIRST_ROWS:不管是否有统计信息,都将采用基于代价的方式进行优化,其优化目标是最快响应时间;

ALL_ROWS:不管是否有统计信息,都将采用基于代价的方式进行优化,其优化目标是最大吞吐量;

例子:

尽快地显示前10行记录

select /*+ first_rows(10) */ * from emp where deptno=10;



6、指定访问路径的HINT

FULL: 执行全表扫描

/*+ FULL ( table ) */

ROID: 根据ROWID进行扫描

/*+ ROWID ( table ) */

INDEX: 根据某个索引进行扫描

/*+ INDEX ( table [index [index]...] ) */

select /*+ index(emp ind_emp_sal)*/ * from emp where deptno=200 and sal>300;

如果写了多个,则ORACLE自动选择最优的哪个

select /*+ index(emp ind_emp_sal ind_emp_deptno)*/ * from emp where deptno=200 and sal>300;

INDEX_JOIN: 如果所选的字段都是索引字段(是几个索引的),那么可以通过索引连接就可访问到数据,而不需要访问表的数据。

/*+ INDEX_JOIN ( table [index [index ...]] ) */

select /*+ index_join(emp ind_emp_sal ind_emp_deptno)*/ deptno,sal from emp where deptno=20;

INDEX_FFS: 执行快速全索引扫描

/*+ INDEX_FFS ( table [index [index]...] ) */

select /*+ index_ffs(emp pk_emp)*/ count(*) from emp;

NO_INDEX: 指定不使用哪些索引

/*+ NO_INDEX ( table [index [index]...] ) */

select /*+ no_index(emp ind_emp_sal ind_emp_deptno)*/ * from emp where deptno=200 and sal>300;

AND_EQUAL: 指定合并两个或以上索引检索的结果(交集),最多不能超过5个

/*+ AND_EQUAL ( table index index [index] [index] [index] ) */



7、指定表的连接顺序

ORDERED: 按表出现的顺序进行连接

/*+ ORDERED */

select /*+ordered*/ emp.ename,dept.dname from dept,emp where emp.deptno=dept.deptno;

select /*+ordered*/ emp.ename,dept.dname from emp,dept where emp.deptno=dept.deptno;



8、指定表的连接操作

USE_NL: 按nested loops方式连接

--默认hash join,获取所有数据的最快返回时间

select emp.ename,dept.dname from dept,emp where emp.deptno=dept.deptno;

--指定emp作为inner table ,以获取最快的响应时间

select /*+ordered use_nl(emp) to get first row faster */ emp.ename,dept.dname from dept,emp where emp.deptno=dept.deptno;

select /*+ordered use_nl(emp dept)*/ emp.ename,dept.dname from dept,emp where emp.deptno=dept.deptno;

posted @ 2018-03-19 17:31  hello|world  阅读(2328)  评论(0编辑  收藏  举报