如何使用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