SQL调优顾问需要一个或多个SQL语句作为输入,并调用自动优化器执行SQL调优。SQL调优顾问输出是以一种意见或者建议的形式,以及对每一项建议和期望效益的理由。该建议涉及对象的统计收集,新索引的创建,SQL语句的重组,或SQL概要的创建。你可以选择该建议来完成SQL语句的调优。
Oracle数据库可以自动优化有问题的SQL语句,并使用SQL调优顾问实现调优建议。也可以手动运行SQL调优顾问选择单个SQL语句或者一个SQL语句集来完成SQL语句调优。
一般使用的比较多的是手动SQL调优顾问,因此也重点介绍手动SQL调优顾问。
begin
dbms_auto_task_admin.enable(client_name => 'sql tuning advisor',
operation => null,
window_name => null);
end;
begin
dbms_auto_task_admin.disable(client_name => 'sql tuning advisor',
operation => null,
window_name => null);
end;
create table a (a1 clob);
declare
my_rept clob;
begin
my_rept := dbms_sqltune.report_auto_tuning_task(begin_exec => null,
end_exec => null,
type => 'TEXT',
level => 'TYPICAL',
section => 'ALL',
object_id => null,
result_limit => null);
insert into a values (my_rept);
end;
select * from a;
截取报告片段:
FINDINGS SECTION (3 findings)
-------------------------------------------------------------------------------
1- Statistics Finding
---------------------
表 "SYS"."OBJAUTH$" 及其索引的优化程序统计信息已失效。
Recommendation
--------------
- 考虑收集此表的优化程序统计信息。
execute dbms_stats.gather_table_stats(ownname => 'SYS', tabname =>
'OBJAUTH$', estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO');
Rationale
---------
为了选择好的执行计划, 优化程序需要此表的最新统计信息。
SQL调优顾问可以按照需求手动调用一个或者多个SQL语句。要调用多条SQL语句,需要创建一个SQL调优集(STS)。SQL调优集是一个存储SQL语句以及执行上下文的数据库对象。
使用DBMS_SQLTUNE包运行SQL调优顾问,需要一下几个步骤:
一个SQL调优任务可以为单个SQL语句创建。对于多个SQL语句,需要先创建SQL调优集(STS)。具体步骤如下:
为了找出有问题的SQL语句,需要创建创建一张没有任何所有的表。如下:
create table my_objects as select * from dba_objects;
查询EMP对象的SQL语句
select * from my_objects o where o.object_name='EMP';
my_objects表上没有任何创建索引,因此该查询语句肯定是有问题的,接下来使用SQL调优顾问来优化该SQL语句:
declare
v_my_task_name varchar2(50);
v_my_sql_text clob;
begin
v_my_sql_text := 'select * from my_objects o where o.object_name=:bin';
v_my_task_name := dbms_sqltune.create_tuning_task(sql_text => v_my_sql_text,
bind_list => sql_binds(anydata.convertvarchar2('EMP')),
user_name => 'GDSHEC',
scope => 'COMPREHENSIVE',
time_limit => 60,
task_name => 'my_sql_tuning_task',
description => 'Task to tune a query on a specified ');
end;
begin
dbms_sqltune.set_tuning_task_parameter(task_name => 'my_sql_tuning_task',
parameter => 'TIME_LIMIT',
value => 300);
end;
begin
dbms_sqltune.execute_tuning_task(task_name => 'my_sql_tuning_task');
end;
select * from dba_advisor_tasks t where t.task_name = 'my_sql_tuning_task';
select dbms_sqltune.report_tuning_task(task_name => 'my_sql_tuning_task')
from dual;
优化结果报告:
GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name : my_sql_tuning_task
Tuning Task Owner : GDSHEC
Workload Type : Single SQL Statement
Scope : COMPREHENSIVE
Time Limit(seconds): 300
Completion Status : COMPLETED
Started at : 12/05/2013 16:10:32
Completed at : 12/05/2013 16:10:34
-------------------------------------------------------------------------------
Schema Name: GDSHEC
SQL ID : 14suc19101mgf
SQL Text : select * from my_objects o where o.object_name=:bin
-------------------------------------------------------------------------------
FINDINGS SECTION (1 finding)
-------------------------------------------------------------------------------
1- Index Finding (see explain plans section below)
--------------------------------------------------
通过创建一个或多个索引可以改进此语句的执行计划。
Recommendation (estimated benefit: 98.73%)
------------------------------------------
- 考虑运行可以改进物理方案设计的访问指导或者创建推荐的索引。
create index GDSHEC.IDX$$_02290001 on GDSHEC.MY_OBJECTS("OBJECT_NAME");
Rationale
---------
创建推荐的索引可以显著地改进此语句的执行计划。但是, 使用典型的 SQL 工作量运行 "访问指导"
可能比单个语句更可取。通过这种方法可以获得全面的索引建议案, 包括计算索引维护的开销和附加的空间消耗。
-------------------------------------------------------------------------------
EXPLAIN PLANS SECTION
-------------------------------------------------------------------------------
1- Original
-----------
Plan hash value: 880823944
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 194 | 317 (1)| 00:00:04 |
|* 1 | TABLE ACCESS FULL| MY_OBJECTS | 2 | 194 | 317 (1)| 00:00:04 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("O"."OBJECT_NAME"=:BIN)
2- Using New Indices
--------------------
Plan hash value: 394593291
----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 194 | 4 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| MY_OBJECTS | 2 | 194 | 4 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX$$_02290001 | 2 | | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("O"."OBJECT_NAME"=:BIN)
-------------------------------------------------------------------------------
总结:SQL调优顾问发现一个问题,建议在OBJECT_NAME表的MY_OBJECTS字段上创建索引。并对照创建索引前后的执行计划进行对比。
begin
dbms_sqltune.drop_tuning_task(task_name => 'my_sql_tuning_task');
end;