SQL Tuning Advisor
SQL Tuning Advisor
o The SQL Tuning Advisor takes one or more SQL statements as an input and
invokes the Automatic Tuning Optimizer to perform SQL tuning on the
statements.
o The output of the SQL Tuning Advisor is in the form of an recommendations,
along with a rationale for each recommendation and its expected benefit.The
recommendation relates to collection of statistics on objects, creation of new
indexes, restructuring of the SQL statement, or creation of a SQL profile. You
can choose to accept the recommendation to complete the tuning of the SQL
statements.
o You can also run the SQL Tuning Advisor selectively on a single or a set of
SQL statements that have been identified as problematic.
o Find the problematic SQL_ID from v$session you would like to analyze.
Usually the AWR has the top SQL_IDs column.
In order to access the SQL tuning advisor API, a user must be granted the ADVISOR
privilege:
sqlplus / as sysdba
GRANT ADVISOR TO HARI;
CONN HARI/hari;
Steps to tune the problematic SQL_ID using SQL TUNING ADVISOR :-
Create Tuning Task :
DECLARE
my_task_name VARCHAR2(30);
BEGIN
my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK(
sql_id => '43x11xxhxy1j7',
scope => 'COMPREHENSIVE',
time_limit => 3600,
task_name => 'my_sql_tuning_task_1',
description => 'Tune query using sqlid');
end;
/
Execute Tuning task :
BEGIN
DBMS_SQLTUNE.EXECUTE_TUNING_TASK( task_name => 'my_sql_tuning_task_1');
end;
/
Monitor the task executing using below query:
SELECT TASK_NAME, STATUS FROM DBA_ADVISOR_LOG WHERE
TASK_NAME ='my_sql_tuning_task_1';
TASK_NAME STATUS
------------------------------ -----------
my_sql_tuning_task_1 COMPLETED
Check the status is completed for the task and we can get recommendations of the
advisor.
Report Tuning task :
SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK( 'my_sql_tuning_task_1')
from DUAL;
DBMS_SQLTUNE.REPORT_TUNING_TASK('my_sql_tuning_task_1')
-----------------------------------------------------------------------
GENERAL INFORMATION SECTION
-----------------------------------------------------------------------
Scope : COMPREHENSIVE
Time Limit(seconds) : 60
Completion Status : COMPLETED
--------------------------------------------------------------------
--------------------------------------------------------
------------------------------------------
DECLARE
l_sql_tune_task_id VARCHAR2(100);
BEGIN
l_sql_tune_task_id := DBMS_SQLTUNE.create_tuning_task (
begin_snap => 1868,
end_snap => 1894,
sql_id => '483wz173punyb',
scope => DBMS_SQLTUNE.scope_comprehensive,
time_limit => 300,
task_name => '483wz173punyb_tuning_task',
description => 'Tuning task for statement 483wz173punyb in AWR.');
DBMS_OUTPUT.put_line('l_sql_tune_task_id: ' || l_sql_tune_task_id);
END;
/
Execute Task,
Report task,