Tuesday, March 21, 2017

Create an Oracle SQL TUNING TASK manually with the SQL_ID

Summary 
You can create an SQL TUNING TASK manually ad hoc with the following simple steps. 

ALTER SESSION SET NLS_LANGUAGE='AMERICAN'; 

0. Find the sql_id of the oracle session you would like to analyze. Usually the AWR has the top sql_ids. 
In case this is a current sql running use the v$session. 

select sql_id from v$session where sid = :x 

1. Login as SYSTEM (or any other user) at sqlplus and create the tuning task:
SET SERVEROUTPUT ON
declare
stmt_task VARCHAR2(40);
begin
stmt_task := DBMS_SQLTUNE.CREATE_TUNING_TASK(sql_id => '5tru8vxmktswq');
DBMS_OUTPUT.put_line('task_id: ' || stmt_task );
end;
/

OR

With increased TIME to avoid "Error: ORA-13639: The current operation was interrupted because it timed out."

SET SERVEROUTPUT ON 
DECLARE 
l_sql_tune_task_id VARCHAR2(100); 
BEGIN 
l_sql_tune_task_id := DBMS_SQLTUNE.create_tuning_task ( 
sql_id => '7pf7g0b97743g', 
scope => DBMS_SQLTUNE.scope_comprehensive, 
time_limit => 7200, 
task_name => '7pf7g0b97743g_11_tuning_task', 
description => 'Tuning task for statement 7pf7g0b97743g'); 
DBMS_OUTPUT.put_line('l_sql_tune_task_id: ' || l_sql_tune_task_id); 
dbms_sqltune.execute_tuning_task(task_name => '0kvazh8xgrt5b_tuning_task'); 
END; 
/ 
task_id: TASK_69287
2. Run the SQL TUNING TASK
begin
DBMS_SQLTUNE.EXECUTE_TUNING_TASK(task_name => 'TASK_69287');
end;
/
3. You can monitor the processing of the tuning task with the statement 

SELECT TASK_NAME, STATUS FROM DBA_ADVISOR_LOG WHERE TASK_NAME = 'TASK_69287'; 

4. When the task has a status=COMPLETED, then run:
SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK('TASK_69287') AS recommendations FROM dual;
5. Examine the recommendations from Oracle, in case you agree, then accept the best SQL profile.
begin
DBMS_SQLTUNE.ACCEPT_SQL_PROFILE(task_name => 'TASK_69287', task_owner => 'SYSTEM', replace => TRUE);
end;
/

6. You can check the database sql profiles with the statement:

select * from dba_sql_profiles;

In case you want to disable an sql profile use the statement:

begin
DBMS_SQLTUNE.ALTER_SQL_PROFILE('SQL_PROFILE','STATUS','DISABLED');
end;
/

No comments:

Post a Comment