1、执行SQL语句,同时使用如下命令查找SQL ID
select a.SQL_ID,b.SQL_TEXT,b.LAST_LOAD_TIME,b.LAST_ACTIVE_TIME
from v$session a, v$sql b
where a.SQL_ID = b.SQL_ID
and a.USERNAME is not null
and a.STATUS = 'ACTIVE' ;
如:8nr2s7h0dd3tp
2、根据SQL ID创建性能分析任务
var tuning_task varchar2(100);
DECLARE
l_sql_id v$session.prev_sql_id%TYPE;
l_tuning_task VARCHAR2(30);
BEGIN
l_sql_id:='8nr2s7h0dd3tp';
l_tuning_task := dbms_sqltune.create_tuning_task(sql_id => l_sql_id);
:tuning_task:=l_tuning_task;
dbms_sqltune.execute_tuning_task(l_tuning_task);
dbms_output.put_line(l_tuning_task);
END;
/
如:TASK_94701
3、查看任务分析结果
SET LONG 999999
set serveroutput on size 999999
SET LINESIZE 100
SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK( 'TASK_94701') from DUAL;
4、使用SQL PROFILE绑定执行计划
execute dbms_sqltune.accept_sql_profile(task_name => 'TASK_94701',task_owner => 'SYS', replace => TRUE,force_match => TRUE);
5、再次执行SQL语句查看是否已经达到优化目的。