如何使用USE_CONCAT提示
2024-07-21 02:06:27
供稿:网友
使用use_concat提示
--use use_concat hints in oracle
last updated: thursday, 2004-11-18 21:48 eygle
use_concat提示强迫优化器扩展查询中的每一个or谓词为独立的查询块.
最后合并所有查询块的结果,返回结果集给用户。
当使用多个in-lists查询时,oracle可能选择把单个查询扩展为多个查询块。
使用use_concat提示示例:
1.使用scott用户及标准表进行测试
$ sqlplus scott/tigersql*plus: release 9.2.0.4.0 - production on wed nov 17 15:17:51 2004copyright (c) 1982, 2002, oracle corporation. all rights reserved.connected to:oracle9i enterprise edition release 9.2.0.4.0 - 64bit productionwith the partitioning, olap and oracle data mining optionsjserver release 9.2.0.4.0 - productionsql> set autotrace onsql> select * from emp where empno in (7788,7900); empno ename job mgr hiredate sal comm deptno---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7788 scott analyst 7566 19-apr-87 3000 20 7900 james clerk 7698 03-dec-81 950 30execution plan---------------------------------------------------------- 0 select statement optimizer=choose (cost=2 card=2 bytes=74) 1 0 table access (full) of 'emp' (cost=2 card=2 bytes=74)--注意,此处oracle选择了全表扫描,因为成本较低。statistics---------------------------------------------------------- 0 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 1032 bytes sent via sql*net to client 655 bytes received via sql*net from client 2 sql*net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 2 rows processed
2.添加提示
sql> select /*+ use_concat */ * from emp where empno in (7788,7900); empno ename job mgr hiredate sal comm deptno---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7900 james clerk 7698 03-dec-81 950 30 7788 scott analyst 7566 19-apr-87 3000 20execution plan---------------------------------------------------------- 0 select statement optimizer=choose (cost=4 card=2 bytes=74) 1 0 concatenation 2 1 table access (by index rowid) of 'emp' (cost=2 card=1 bytes=37) 3 2 index (unique scan) of 'pk_emp' (unique) (cost=1 card=14) 4 1 table access (by index rowid) of 'emp' (cost=2 card=1 bytes=37) 5 4 index (unique scan) of 'pk_emp' (unique) (cost=1 card=14)--使用use_concat提示以后,oracle将in-lists条件展开为两个查询块,分别使用索引,最后concatenation得到最后输出。--注意,这里强制使用索引导致成本上升为4。statistics---------------------------------------------------------- 0 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 1032 bytes sent via sql*net to client 655 bytes received via sql*net from client 2 sql*net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 2 rows processedsql>
3.oracle对于执行计划的改写
对于inlist查询,oracle通常会进行改写,将形如
select ..... from ....... where ....in (..........)
的sql语句,改写为union all的形式来执行,这个改写通常是潜在的。
然而这一改写可能存在问题,如果inlist中的值比较多的话,cbo花在分析执行路径上的时间和成本都会相当大,此时我们通常需要阻止oracle的这一展开操作.
我们可以通过no_expand提示来阻止oracle进行这样的改写。
那么实际上,在这里,use_concat和no_expand成了互为"反函数"。在使用了no_expand提示后,从oracle8之后,oracle会使用"inlist iterator"
方式来执行sql,这样可以用到index。
本文作者:
eygle,oracle技术关注者,来自中国最大的oracle技术论坛itpub.
www.eygle.com是作者的个人站点.你可通过[email protected]来联系作者.欢迎技术探讨交流以及链接交换.
原文出处:
http://www.eygle.com/sql/how.to.use.use_concat.hints.in.oracle.htm