create table bys.t1 as select * from dba_objects;
create index bys.t1_idx on t1(object_id);
exec dbms_stats.gather_table_stats(‘bys‘,‘t1‘,cascade=>true,degree=>4);
set autotrace trace;
select a.*,b.owner from t1 a,t1 b where a.object_name like ‘%T1%‘ and a.object_id=b.object_id;
select /*+ use_nl(a b) index(t2) */ a.*,b.owner from t1 a,t1 b where a.object_name like ‘%T1%‘ and a.object_id=b.object_id;
set autotrace off;
explain plan for select /*+ use_nl(a b) index(t2) */ a.*,b.owner from t1 a,t1 b where a.object_name like ‘%T1%‘ and a.object_id=b.object_id;
col sql_text for a100
SELECT SQL_ID,SQL_TEXT FROM V$SQL WHERE SQL_TEXT LIKE ‘%a.*,b.owner from t1 a,t1 b%‘;
####2t5xqt4d1dsaw 10g开始,v$sql_plan中就包括了SQL语句OUTLINE数据,也就是稳定执行计划的Hints。如下:
set pagesize 1000
SQL> select * from table(dbms_xplan.display_cursor(‘2t5xqt4d1dsaw‘,null,‘outline‘));
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------
SQL_ID 2t5xqt4d1dsaw, child number 0
-------------------------------------
select /*+ use_nl(a b) index(t2) */ a.*,b.owner from t1 a,t1 b where
a.object_name like ‘%T1%‘ and a.object_id=b.object_id
Plan hash value: 190596302
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 979 (100)| |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 1 | 9 | 2 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 471 | 43803 | 979 (1)| 00:00:12 |
|* 3 | TABLE ACCESS FULL | T1 | 471 | 39564 | 36 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | T1_IDX | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
## execute dbms_sqltune.accept_sql_profile(task_name =>‘tuning_test‘ ,replace => TRUE,force_match=>true); 完成后验证语句:
set autotrace trace;
select a.*,b.owner from t1 a,t1 b where a.object_name like ‘%T1%‘ and a.object_id=b.object_id;
set autotrace off; 删除SQL语句:
exec dbms_sqltune.drop_tuning_task(‘tuning_test‘);
如果未找到理想的执行计划,则参考下一个方法。
##################################################################
exec dbms_sqltune.execute_tuning_task(‘sql_tuning_test‘); --执行调优的任务
SELECT STATUS FROM USER_ADVISOR_TASKS WHERE task_name = ‘sql_tuning_test‘; --查看调优作业的状态
SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK(‘sql_tuning_test‘) FROM DUAL;--查看调优的建议
exec dbms_sqltune.drop_tuning_task(‘sql_tuning_test‘);--删除任务