sql 自动调优

来源:互联网 发布:linux tweak yool 编辑:程序博客网 时间:2024/06/05 07:47

转自http://blog.csdn.net/wwlhz/article/details/70171288

Oracle SQL 自动调优

Oracle 11版本之后,Oracle支持自动调优,默认开启自动调优任务,每天执行一次,可以查看生成的调优建议。

本文的SQL语句基于 11.2.0.1.0版本,不保证在其他版本上的可用性。

查询是否启用自动SQL调优作业

select client_name,status,consumer_group,window_group from dba_autotask_client order by client_name;

这里写图片描述

其中一个是 sql tuning advisor SQL调优顾问。

查看SQL调优顾问最近几次的运行情况

select task_name,status,to_char(execution_end,'DD-MON-YY HH24:MI') from dba_advisor_executions where task_name='SYS_AUTO_SQL_TUNING_TASK' order by execution_end;

查看SQL自动调优建议

set  linesize 3000 PAGESIZE 0 LONG 100000select DBMS_SQLTUNE.REPORT_AUTO_TUNING_TASK FROM DUAL;一般会有大量输出,在sqlplus上不方便查看,最好能导出到文本中。

这里写图片描述

创建两个sql文件,auto_tuning_report_init.sql 是环境初始化设置和执行语句, 
auto_tuning_report.sql 是查询SQL自动调优建议的SQL语句,会被前一个调用, 
最终会在当前目录下生成 auto_tuning_report.txt

这里写图片描述

里面包含详细信息和建议。

生成SQL调优脚本

根据SQL建议,可以生成相应的调优的SQL语句脚本。

select DBMS_SQLTUNE.SCRIPT_TUNING_TASK('SYS_AUTO_SQL_TUNING_TASK') from dual;

这里写图片描述

创建两个sql文件,generate_tuning_sql_init.sql 是环境初始化设置和执行语句, 
generate_tuning_sql.sql 是生成调优SQL的SQL语句,会被前一个调用, 
最终会在当前目录下生成 auto_tuning_report_sql.txt

关闭sql tuning advisor自动调优

 BEGIN
   dbms_auto_task_admin.disable(
    client_name => 'sql tuning advisor',
    operation => NULL,
    window_name => NULL);
  END;

开启sql tuning advisor自动调优

 BEGIN
   dbms_auto_task_admin.enable(
    client_name => 'sql tuning advisor',
    operation => NULL,
    window_name => NULL);
  END;
  /