参见 Oracle性能优化求生指南。
如果某语句当前的执行计划并不令人满意,而且这条语句又不能加hint(因为在应用中不能编辑)。那么可以用这种方法,使执行计划稳定在一个与当前执行计划不同的执行计划上。
SYS@ prod> conn hr/hr
Connected.
为所要调整的语句用当前的执行计划(并不是想要的)创建一个outline。
HR@ prod> create outline pub_otln for category c1 on
2 select last_name from employees1 where employee_id = 100 ;
Outline created.
创建一个与上面相同的私有OUTLINE。
HR@ prod> create private outline otln1 from pub_otln ;
Outline created.
通过向原来的语句中加入hints得到想要的执行计划,如下,使执行计划使用复合索引test_idx2。
HR@ prod> set autotrace on
HR@ prod> select /*+ index(employees1 test_idx2) */ last_name from employees1 where employee_id = 100 ;
LAST_NAME
-------------------------
King
Execution Plan
----------------------------------------------------------
Plan hash value: 2093088777
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 11 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES1 | 1 | 11 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TEST_IDX2 | 1 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("EMPLOYEE_ID"=100)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
529 bytes sent via S