当前位置: 移动技术网 > IT编程>数据库>Oracle > ORACLE中的的HINT详解

ORACLE中的的HINT详解

2017年12月08日  | 移动技术网IT编程  | 我要评论

三国之天机军师,一生一石,北京奥运会开幕式高清nbc

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,不报错  

1. /*+all_rows*/  

表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化.  

例如:  

select /*+all_rows*/ emp_no,emp_nam,dat_in from bsempms where emp_no='scott'; 

2. /*+first_rows*/  

表明对语句块选择基于开销的优化方法,并获得最佳响应时间,使资源消耗最小化.  

例如:  

select /*+first_rows*/ emp_no,emp_nam,dat_in from bsempms where emp_no='scott'; 

3. /*+choose*/  

表明如果数据字典中有访问表的统计信息,将基于开销的优化方法,并获得最佳的吞吐量;  

表明如果数据字典中没有访问表的统计信息,将基于规则开销的优化方法;  

例如:  

select /*+choose*/ emp_no,emp_nam,dat_in from bsempms where emp_no='scott'; 

4. /*+rule*/  

表明对语句块选择基于规则的优化方法.  

例如:  

select /*+ rule */ emp_no,emp_nam,dat_in from bsempms where emp_no='scott'; 

5. /*+full(table)*/  

表明对表选择全局扫描的方法.  

例如:  

select /*+full(a)*/ emp_no,emp_nam from bsempms a where emp_no='scott'; 

6. /*+rowid(table)*/  

提示明确表明对指定表根据rowid进行访问.  

例如:  

 select /*+rowid(bsempms)*/ * from bsempms where rowid>='aaaaaaaaaaaaaa' 
 and emp_no='scott'; 

7. /*+cluster(table)*/  

提示明确表明对指定表选择簇扫描的访问方法,它只对簇对象有效.  

例如:  

select /*+cluster */ bsempms.emp_no,dpt_no from bsempms,bsdptms 
 
 where dpt_no='tec304' and bsempms.dpt_no=bsdptms.dpt_no; 

8. /*+index(table index_name)*/  

表明对表选择索引的扫描方法.  

例如:    

 select /*+index(bsempms sex_index) use sex_index because there are fewmale bsempms */ from bsempms where sex='m'; 

9. /*+index_asc(table index_name)*/  

表明对表选择索引升序的扫描方法.  

例如:  

select /*+index_asc(bsempms pk_bsempms) */ from bsempms where dpt_no='scott'; 

10. /*+index_combine*/  

为指定表选择位图访问路经,如果index_combine中没有提供作为参数的索引,将选择出位图索引的布尔组合方式.  

例如:  

select /*+index_combine(bsempms sal_bmi hiredate_bmi)*/ * from bsempms 
where sal<5000000 and hiredate 

11. /*+index_join(table index_name)*/  

提示明确命令优化器使用索引作为访问路径.  

例如:  

select /*+index_join(bsempms sal_hmi hiredate_bmi)*/ sal,hiredate 
from bsempms where sal<60000; 

12. /*+index_desc(table index_name)*/  

表明对表选择索引降序的扫描方法.  

例如:  

select /*+index_desc(bsempms pk_bsempms) */ from bsempms where dpt_no='scott'; 

13. /*+index_ffs(table index_name)*/  

对指定的表执行快速全索引扫描,而不是全表扫描的办法.  

例如:  

select /*+index_ffs(bsempms in_empnam)*/ * from bsempms where dpt_no='tec305'; 

14. /*+add_equal table index_nam1,index_nam2,...*/  

提示明确进行执行规划的选择,将几个单列索引的扫描合起来.  

例如:  

 select /*+index_ffs(bsempms in_dptno,in_empno,in_sex)*/ * from bsempms where emp_no='scott' and dpt_no='tdc306'; 

15. /*+use_concat*/  

对查询中的where后面的or条件进行转换为union all的组合查询.  

例如:  

 select /*+use_concat*/ * from bsempms where dpt_no='tdc506' and sex='m'; 

16. /*+no_expand*/  

对于where后面的or 或者in-list的查询语句,no_expand将阻止其基于优化器对其进行扩展.  

例如:  

select /*+no_expand*/ * from bsempms where dpt_no='tdc506' and sex='m'; 

17. /*+nowrite*/  

禁止对查询块的查询重写操作.  

18. /*+rewrite*/  

可以将视图作为参数.  

19. /*+merge(table)*/  

能够对视图的各个查询进行相应的合并.  

例如:  

select /*+merge(v) */ a.emp_no,a.emp_nam,b.dpt_no from bsempms a (selet dpt_no 
 
 ,avg(sal) as avg_sal from bsempms b group by dpt_no) v where a.dpt_no=v.dpt_no 
 
 and a.sal>v.avg_sal; 

20. /*+no_merge(table)*/  

对于有可合并的视图不再合并.  

例如:  

select /*+no_merge(v) */ a.emp_no,a.emp_nam,b.dpt_no from bsempms a (select dpt_no,avg(sal) as avg_sal from bsempms b group by dpt_no) v where a.dpt_no=v.dpt_no and a.sal>v.avg_sal; 

21. /*+ordered*/  

根据表出现在from中的顺序,ordered使oracle依此顺序对其连接.  

例如:  

 select /*+ordered*/ a.col1,b.col2,c.col3 from table1 a,table2 b,table3 c where a.col1=b.col1 and b.col1=c.col1; 

22. /*+use_nl(table)*/  

将指定表与嵌套的连接的行源进行连接,并把指定表作为内部表.  

例如:  

select /*+ordered use_nl(bsempms)*/ bsdptms.dpt_no,bsempms.emp_no,bsempms.emp_nam from bsempms,bsdptms where bsempms.dpt_no=bsdptms.dpt_no; 

23. /*+use_merge(table)*/  

将指定的表与其他行源通过合并排序连接方式连接起来.  

例如:  

select /*+use_merge(bsempms,bsdptms)*/ * from bsempms,bsdptms where bsempms.dpt_no=bsdptms.dpt_no; 

24. /*+use_hash(table)*/  

将指定的表与其他行源通过哈希连接方式连接起来.  

例如:  

select /*+use_hash(bsempms,bsdptms)*/ * from bsempms,bsdptms where bsempms.dpt_no=bsdptms.dpt_no; 

 25. /*+driving_site(table)*/  

强制与oracle所选择的位置不同的表进行查询执行.  

例如:  

select /*+driving_site(dept)*/ * from bsempms,dept@bsdptms where bsempms.dpt_no=dept.dpt_no; 

26. /*+leading(table)*/  

将指定的表作为连接次序中的首表.  

27. /*+cache(table)*/  

当进行全表扫描时,cache提示能够将表的检索块放置在缓冲区缓存中最近最少列表lru的最近使用端  

例如:  

select /*+full(bsempms) cahe(bsempms) */ emp_nam from bsempms; 

28. /*+nocache(table)*/  

当进行全表扫描时,cache提示能够将表的检索块放置在缓冲区缓存中最近最少列表lru的最近使用端  

例如: 

select /*+full(bsempms) nocahe(bsempms) */ emp_nam from bsempms; 

29. /*+append*/  

直接插入到表的最后,可以提高速度.  

insert /*+append*/ into test1 select * from test4 ; 

30. /*+noappend*/  

通过在插入语句生存期内停止并行模式来启动常规插入.  

insert /*+noappend*/ into test1 select * from test4 ; 

31. 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; 

32. parallel  

select /*+ parallel(emp,4)*/ * from emp where deptno=200 and sal>300; 

另:每个select/insert/update/delete命令后只能有一个/*+ */,但提示内容可以有多个,可以用逗号分开,空格也可以。  

如:/*+ ordered index() use_nl() */  

--------- 

类似如下的一条语句:insert into xxxx select /*+parallel(a) */ * from xxx a;数据量大约在75g左右,这位兄弟从上午跑到下午还没跑完,过来问我咋回事,说平常2hrs能跑完的东西跑了好几个小时还撒动静。查看系统性能也比较 正常,cpu,io都不繁忙,平均read速度在80m/s左右(勉强凑合),但平均写速度只有10m不到。等待事件里面大量的‘ ‘px deq credit: send blkd',这里能看出并行出了问题,从而最后得知是并行用法有问题,修改之后20分钟完成了该操作。正确的做法应该是:  

alter session enable dml parallel; 
insert /*+parallel(xxxx,4) */ into xxxx select /*+parallel(a) */ * from xxx a;

因为oracle默认并不会打开pdml,对dml语句必须手工启用。 另外不得不说的是,并行不是一个可扩展的特性,只有在数据仓库或作为dba等少数人的工具在批量数据操作时利于充分利用资源,而在oltp环境下使用并行 需要非常谨慎。事实上pdml还是有比较多的限制的,例如不支持触发器,引用约束,高级复制和分布式事务等特性,同时也会带来额外的空间占用,pddl同 样是如此。

--------- 

select count(*) 
 from wid_serv_prod_mon_1100 a 
 where a.acct_month = 201010 
  and a.partition_id = 10 
  and serv_state not in ('2hb', '2hl', '2hj', '2hp', '2hf') 
  and online_flag in (0) 
  and incr_product_id in (2000020) 
  and product_id in (2020966, 2020972, 2100297, 2021116) 
  and billing_mode_id = 1 
  and exp_date > to_date('201010', 'yyyymm') 
  and not exists (select /*+no_index (b idx_w_cdr_mon_serv_id_1100)*/ 
     1 
     from wid_cdr_mon_1100 b 
     where b.acct_month = 201010 
      and b.ana_event_type_4 in 
        ('10201010201', '10202010201', '10203010201', '10203010202', '10203030201', '10203030202', '10204010201', '10204010202', '10204030201') 
      and a.serv_id = b.serv_id) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网