使用sql优化顾问优化sql(单一sql)

oracle database 10g中引入了sql tuning advisor,帮助dba和开发人员提高sql语句的性能。

要想访问自己的优化集必须拥有sys授予的以下权限:

grant administer sql tuning set to scott;---或者授权下面语句
grant administer any sql tuning set to scott;

---and
grant advisor to scott;
grant create any sql profile to scott;
grant alter any sql profile to scott;
grant drop any sql profile to scott;

优化单一sql的步骤:

1.使用dbms_sqltune包创建优化任务;

declare
  tuning_task_name varchar2(30);
  tuning_sqltext   clob;
begin
  tuning_sqltext   := 'select count(*) from t1 where upper(owner)=:owner';
  tuning_task_name := dbms_sqltune.create_tuning_task(
                   sql_text  => tuning_sqltext,
                   bind_list => sql_binds(anydata.ConvertVarchar2(100)),
                   user_name => 'SCOTT',
                   scope => 'COMPREHENSIVE',
                   time_limit => 60,
                   task_name => 'first_tuning_task',
                   description => 'Tune t1 count'
                   );
end;

2.确定从顾问日志中可以查看到任务

select task_name from user_advisor_log;

TASK_NAME
------------------------------
first_tuning_task

可以查看到新建的任务的名称;

3.执行sql优化任务

begin
  dbms_sqltune.execute_tuning_task(task_name => 'first_tuning_task');
end;
/

4.查看优化任务的状态

select task_name, status
  from user_advisor_tasks
 where task_name = 'first_tuning_task';


TASK_NAME                      STATUS
------------------------------ -----------
first_tuning_task              COMPLETED

5.显示sql优化顾问生成的报告,显示输出的报告较长,但是主要的中心意思就是建议在t1表的owner列上建立一个基于函数的索引。sql优化顾问已经建议使用sql配置文件,使用dbms_sqltune.accept_sql_profile程序包来接受该建议。

SQL> set long 8000
SQL> set longchunksize 8000
SQL> set linesize 100
SQL> set pagesize 100

select dbms_sqltune.report_tuning_task('first_tuning_task') from dual;

DBMS_SQLTUNE.REPORT_TUNING_TASK('FIRST_TUNING_TASK')
----------------------------------------------------------------------------------------------------
GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name   : first_tuning_task
Tuning Task Owner  : SCOTT
Workload Type      : Single SQL Statement
Scope              : COMPREHENSIVE
Time Limit(seconds): 60
Completion Status  : COMPLETED
Started at         : 04/03/2016 05:54:23
Completed at       : 04/03/2016 05:54:30

-------------------------------------------------------------------------------
Schema Name: SCOTT
SQL ID     : 5jhrhxpzn6yh3
SQL Text   : select count(*) from t1 where upper(owner)=:owner

-------------------------------------------------------------------------------
FINDINGS SECTION (2 findings)
-------------------------------------------------------------------------------

1- Statistics Finding
---------------------
  Table "SCOTT"."T1" was not analyzed.

  Recommendation
  --------------
  - Consider collecting optimizer statistics for this table.
    execute dbms_stats.gather_table_stats(ownname => 'SCOTT', tabname =>
            'T1', 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.

2- Index Finding (see explain plans section below)
--------------------------------------------------
  The execution plan of this statement can be improved by creating one or more
  indices.

  Recommendation (estimated benefit: 98.96%)
  ------------------------------------------
  - Consider running the Access Advisor to improve the physical schema design
    or creating the recommended index.
    create index SCOTT.IDX$$_02170001 on SCOTT.T1(UPPER("OWNER"));

  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.

-------------------------------------------------------------------------------
EXPLAIN PLANS SECTION
-------------------------------------------------------------------------------

1- Original
-----------
Plan hash value: 3724264953

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    17 |   292   (1)| 00:00:04 |
|   1 |  SORT AGGREGATE    |      |     1 |    17 |            |          |
|*  2 |   TABLE ACCESS FULL| T1   |    12 |   204 |   292   (1)| 00:00:04 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter(UPPER("OWNER")=:OWNER)

2- Using New Indices
--------------------
Plan hash value: 3643728678

------------------------------------------------------------------------------------
| Id  | Operation         | Name           | Rows  | Bytes | Cost (%CPU)| Time    |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |                |     1 |    17 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE   |                |     1 |    17 |            |         |
|*  2 |   INDEX RANGE SCAN| IDX$$_02170001 |    12 |   204 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("T1".)

-------------------------------------------------------------------------------




你可能感兴趣的:(学习笔记)