Automatic SQL Tuning in Oracle 11g

 

The Automatic Database Diagnostic Monitor in Oracle 11g
The ADDM in Oracle 11g

The automated tuning process in Oracle 11g is implemented by a number of functions in the Oracle database:

Automatic Database Diagnostic Monitor (ADDM)

Takes input from the AWR reports which it analyses in order to identify potential bottlenecks. If the statistics level parameter is set to TYPICAL or ALL then the ADDM is ran every time an AWR snapshot is taken.

Reports can be vierwed through Enterprise manager but can also be generated with the addmrpt.sql script located at $ORACLE_HOME/rdbms/admin/addmrpt.sql

SQL Tuning Advisor

The optimizer can run in 2 modes, Normal and Tuning. In Normal mode, the optimizer must produce a plan in a very short timeframe and so has to accept the information that it has is up to date and accurate. The alternative is Tuning mode where the optimizer is given more time so that it can perform analysis and gather more information in rder to produce a more accurate and efficient plan.

The ADDM identifies SQL that could be improved and passes it to the SQL Tuning Advisor. This invokes the Automatic Tuning Optimizer and evaluates possibilities for improvement. The automatic Tuning Advisor performs a range of different types of analysis including statistics analysis (gathering stats), SQL profiling (creating new SQL profiles), Access path analysis (adding aindexes to allow faster access to data) and SQL structure analysis (actually rewriting or rather suggesting options for rewriting SQl statements).

From Version 11g, the Automatic SQL Tuning Advisor is ran (by default) during the nightly maintanance window. While it does not automatically implement suggestoins, it can be set up to do so.

You can use the ‘ENABLE‘ or ‘DISABLE‘ procedures of the DBMS_AUTO_TASK_ADMIN package to let the automatic tuning job run or not. Setting the STATISTICS_LEVEL to BASIC will also disable it because that stops stats gathering be the AWR.

— Enable

BEGIN

DBMS_AUTO_TASK_ADMIN.enable(

client_name => ‘sql tuning advisor’,

operation => NULL,

window_name => NULL);

END;

/

When it does run, the procedure is as folows:

  1. Candidate SQL queries are identifed from the AWR. Only statements with execution plan that has a high potential for improvement and a high impact on the system are considered.
  2. Each statementnd is invidually tuned by calling the SQL Tuning Advisor. The only recommendation that can be automatically implemented are SQL profiles.
  3. SQL profiles are implemented when:
    • There is at least 3 fold improvement.
    • ACCEPT_SQL_PROFILES task parameter is TRUE

These profiles will have a type of AUTO in DBA_SQL_PROFILES

How to Setup and Configure Automatic SQL Tuning

Automatic SQL tuning task can be configured with the DBMS_SQLTUNE package. Only SYS can do this.

You can turn on or off the acceptance of automatically generated SQL profiles with the SET_TUNING_TASK_PARAMETER procedure.

BEGIN

DBMS_SQLTUNE.SET_TUNING_TASK_PARAMETER( task_name = > ‘SYS_AUTO_SQL_TUNING_TASK’,

parameter = > ‘ACCEPT_SQL_PROFILES’,

value = > ‘TRUE’);

END;

 Additional parameters:

  • ACCEPT_SQL_PROFILE – automatically accept profiles?
  • MAX_SQL_PROFILES_PER_EXEC – maximum number of profiles to evaluate per execution (default 20).
  • MAX_AUTO_SQL_PROFILES – total auto profiles allowed on the system (default 1000).
  • EXECUTION_DAYS_TO_EXPIRE – default 30 days – how long to save task history

Viewing Automatic SQL Tuning Reports

You can view the Automatic SQL Tuning Reports through Enterprise Manager as well as via the DBMS_SQLTUNE package – DBMS_SQLTUNE.REPORT_AUTO_TUNING_TASK

VARIABLE l_report CLOB;

BEGIN

:l_report := DBMS_SQLTUNE.report_auto_tuning_task(

begin_exec => NULL,

end_exec => NULL,

type => DBMS_SQLTUNE.type_text, — ‘TEXT’

level => DBMS_SQLTUNE.level_typical, — ‘TYPICAL’

section => DBMS_SQLTUNE.section_all, — ‘ALL’

object_id => NULL,

result_limit => NULL);

END;

/

 

SET LONG 1000000

PRINT :l_report

There are several useful views that can be referred to and are worth exploring to gain more information on Automatic SQL Tuning.

  • DBA_ADVISOR_ACTIONS – Actions associated with recommendations
  • DBA_ADVISOR_TASKS
  • DBA_ADVISOR_EXECUTIONS – Metadata for task executions
  • DBA_ADVISOR_FINDINGS
  • DBA_ADVISOR_RECOMMENDATIONS
  • DBA_ADVISOR_RATIONALE
  • DBA_ADVISOR_SQL_STATS
  • DBA_ADVISOR_SQLPLANS