最近在群里聊天,有朋友笑言STA是现在M的优化“利器”,很是好奇,我也来学学这个工具的用法:
一、模拟测试环境
SQL> create table t_tables as select OWNER,TABLE_NAME,
2 TABLESPACE_NAME,NUM_ROWS from dba_tables;
Table created.
SQL> create table t_objects as select OWNER,OBJECT_NAME,SUBOBJECT_NAME,OBJECT_ID,
2 DATA_OBJECT_ID,OBJECT_TYPE,CREATED from dba_objects;
Table created.
SQL> insert into t_tables select * from t_tables;
1529 rows created.
SQL> /
12232 rows created.
SQL> commit;
Commit complete.
SQL> insert into t_objects select * from t_objects;
49824 rows created.
SQL> /
199296 rows created.
SQL> commit;
Commit complete.
SQL> set timing on
SQL> set autotrace on
SQL> select count(*) from t_objects a, t_tables b where a.object_name=b.table_name;
COUNT(*)
----------
218368
Elapsed: 00:00:00.11
Execution Plan
-------------------------------------------------------------------------------------------------------
Plan hash value: 3023195286
---------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time|
---------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 83 | 798 (5)| 00:00:10|
| 1 | SORT AGGREGATE | | 1 | 83 | ||
|* 2 | HASH JOIN | 1308K| 103M| 798 (5)| 00:00:10|
| 3 | TABLE ACCESS FULL | T_TABLES | 24415 | 405K| 43 (3)| 00:00:01|
| 4 | TABLE ACCESS FULL | T_OBJECTS | 500K| 31M| 735 (3)| 00:00:09|
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("A"."OBJECT_NAME"="B"."TABLE_NAME")
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3432 consistent gets
0 physical reads
0 redo size
413 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> set autot off
SQL> set timing off
SQL>
二、使用工具进行优化
1.创建优化任务
通过调用函数CREATE_TUNING_TASK来创建优化任务,调用存储过程 EXECUTE_TUNING_TASK执行该任务:
SQL>
SQL>
SQL> DECLARE
2 my_task_name VARCHAR2(30);
3 my_sqltext CLOB;
4 BEGIN
5 my_sqltext := 'select count(*) from t_tables t1,t_objects t2 where t1.table_name=t2.object_name';
6 my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK(
7 sql_text => my_sqltext,
8 user_name => 'TEST',
9 scope => 'COMPREHENSIVE',
10 time_limit => 60,
11 task_name => 'my_sql_tuning_task',
12 description => 'Task to tune a query on a specified table');
13 END;
14 /
PL/SQL procedure successfully completed.
第二步: 执行优化任务
通过调用dbms_sqltune.execute_tuning_task过程来执行前面创建好的
SQL> BEGIN
2 DBMS_SQLTUNE.EXECUTE_TUNING_TASK( task_name => 'my_sql_tuning_task' );
3 END;
4 /
PL/SQL procedure successfully completed.
第三步:检查优化任务的状态
通过查看user_advisor_tasks/dba_advisor_tasks视图可以查看优化任务的当前状态
SQL> SELECT task_name,status FROM USER_ADVISOR_TASKS WHERE task_name ='my_sql_tuning_task';
TASK_NAME STATUS
------------------------------ -----------
my_sql_tuning_task COMPLETED
SQL>
SQL> select task_name, status from USER_ADVISOR_LOG where task_name='my_sql_tuning_task';
TASK_NAME STATUS
------------------------------ -----------
my_sql_tuning_task COMPLETED
第四步:查看优化器的建议
通过dbms_sqltune.report_tning_task函数可以获得优化任务的结果。
SQL>
SQL> SET LONG 999999
SQL> set serveroutput on size 999999
SET LONGCHUNKSIZE 1000
SET LINESIZE 100
SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK('my_sql_tuning_task')
FROM DUAL;SQL> SQL> SQL> 2
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name : my_sql_tuning_task
Tuning Task Owner : TEST
Scope : COMPREHENSIVE
Time Limit(seconds) : 60
Completion Status : COMPLETED
Started at : 12/01/2013 08:03:39
Completed at : 12/01/2013 08:03:41
Number of Statistic Findings : 2
Number of Index Findings : 1
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Schema Name: TEST
SQL ID : 76u1asv25u8wt
SQL Text : select count(*) from t_tables t1,t_objects t2 where
t1.table_name=t2.object_name
-------------------------------------------------------------------------------
FINDINGS SECTION (3 findings)
-------------------------------------------------------------------------------
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
1- Statistics Finding
---------------------
Table "TEST"."T_OBJECTS" was not analyzed.
Recommendation
--------------
- Consider collecting optimizer statistics for this table.
execute dbms_stats.gather_table_stats(ownname => 'TEST', tabname =>
'T_OBJECTS', estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO');
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
Rationale
---------
The optimizer requires up-to-date statistics for the table in order to
select a good execution plan.
2- Statistics Finding
---------------------
Table "TEST"."T_TABLES" was not analyzed.
Recommendation
--------------
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
- Consider collecting optimizer statistics for this table.
execute dbms_stats.gather_table_stats(ownname => 'TEST', tabname =>
'T_TABLES', estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO');
Rationale
---------
The optimizer requires up-to-date statistics for the table in order to
select a good execution plan.
3- Index Finding (see explain plans section below)
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
--------------------------------------------------
The execution plan of this statement can be improved by creating one or more
indices.
Recommendation (estimated benefit: 100%)
----------------------------------------
- Consider running the Access Advisor to improve the physical schema design
or creating the recommended index.
create index TEST.IDX$$_00E40001 on TEST.T_TABLES('TABLE_NAME');
- Consider running the Access Advisor to improve the physical schema design
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
or creating the recommended index.
create index TEST.IDX$$_00E40002 on TEST.T_OBJECTS('OBJECT_NAME');
Rationale
---------
Creating the recommended indices significantly improves the execution plan
of this statement. However, it might be preferable to run "Access Advisor"
using a representative SQL workload as opposed to a single statement. This
will allow to get comprehensive index recommendations which takes into
account index maintenance overhead and additional space consumption.
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------
EXPLAIN PLANS SECTION
-------------------------------------------------------------------------------
1- Original
-----------
Plan hash value: 3023195286
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 83 | 798 (5)| 00:00:10 |
| 1 | SORT AGGREGATE | | 1 | 83 | | |
|* 2 | HASH JOIN | | 1308K| 103M| 798 (5)| 00:00:10 |
| 3 | TABLE ACCESS FULL| T_TABLES | 24415 | 405K| 43 (3)| 00:00:01 |
| 4 | TABLE ACCESS FULL| T_OBJECTS | 500K| 31M| 735 (3)| 00:00:09 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("T1"."TABLE_NAME"="T2"."OBJECT_NAME")
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
2- Using New Indices
--------------------
Plan hash value: 3781359934
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 83 | 106 (20)| 00:00:02 |
| 1 | SORT AGGREGATE | | 1 | 83 | | |
|* 2 | HASH JOIN | | 1308K| 103M| 106 (20)| 00:00:02 |
DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK')
----------------------------------------------------------------------------------------------------
| 3 | INDEX FAST FULL SCAN| IDX$$_00E40001 | 24415 | 405K| 3 (0)| 00:00:01 |
| 4 | INDEX FAST FULL SCAN| IDX$$_00E40002 | 500K| 31M| 83 (2)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("T1"."TABLE_NAME"="T2"."OBJECT_NAME")
-------------------------------------------------------------------------------
第一部分是关于这次优化任务的基本信息:如任务名称、执行时间、范围、涉及到的语句等等。
第二部分是关于这次优化任务的所找到的问题以及给出的优化建议。前面先给出了问题描述:可以通过建立更多的索引来提高性能;然后是建议的具体内容建立相关索引,走INDEX FAST FULL SCAN;最后是相关注意事项:此次优化虽然给出了创建索引的建议,但是最好通过SQL访问建议器(SQL Access Advisor SAA)结合整个数据库的工作量来深入分析,那样就能给出考虑了索引维护和空间消耗等因素的更加合理的建议。
最后,报告还给出了原有的查询计划,以及采用优化建议以后的查询计划的对比。
5.删除优化任务
通过调用dbms_sqltuen.drop_tuning_task可以删除已经存在的优化任务
exec dbms_sqltune.drop_tuning_task('my_sql_tuning_task');