在sql語句優化過程中,我們經常會用到hint,現總結一下在sql優化過程中常見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<sysdate;
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,[email protected] 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 ;