SQL访问顾问SAA(SQL Access Advisor)针对单条sql优化实践

create table testobj as select * from dba_objects;

exec dbms_stats.gather_table_stats(ownname => 'TEST',tabname => 'TESTOBJ',cascade => true);

必须存在统计信息,否则saa是不起作用的,刚开始实验迷糊了,就是没结果,查了好些例子才发现

exec   dbms_advisor.quick_tune(DBMS_ADVISOR.SQLACCESS_ADVISOR,'mytask1','select * from  testobj where OBJECT_ID=16');

 

SQL>   select * from dba_advisor_actions where task_name ='mytask1' ;
OWNER                                                                               TASK_ID TASK_NAME                                                                        EXECUTION_NAME                                                                       REC_ID  ACTION_ID  OBJECT_ID COMMAND                                                          COMMAND_ID      FLAGS ATTR1                                                                            ATTR2                                                                            ATTR3                                                                            ATTR4                                                                            ATTR5                                                                            ATTR6                                                                             NUM_ATTR1  NUM_ATTR2  NUM_ATTR3  NUM_ATTR4  NUM_ATTR5 MESSAGE                                                                          FILTERED
-------------------------------------------------------------------------------- ---------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------- ---------- ---------- ---------------------------------------------------------------- ---------- ---------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------- ---------- ---------- ---------- ---------- -------------------------------------------------------------------------------- --------
TEST                                                                                     41 mytask1                                                                                                                                                                    1          1          0 CREATE INDEX                                                              2          0 "TEST"."TESTOBJ_IDX$$_00290000"                                                                                                                                   "TEST"."TESTOBJ"                                                                 BTREE                                                                            ("OBJECT_ID")                                                                                                                                                        1332956          0          0          0          0                                                                                  N
SQL>   select * from user_advisor_sqla_wk_stmts where task_name ='mytask1'  ;
TASK_NAME                                                                           TASK_ID  SQLSET_ID SQLSET_NAME                                                                      WORKLOAD_NAME                                                                    SQL_ID           SQL_SEQ PLAN_HASH_VALUE PARSING_SCHEMA_NAME                                                              USERNAME                                                                         MODULE                                                           ACTION                                                             CPU_TIME BUFFER_GETS DISK_READS ELAPSED_TIME ROWS_PROCESSED EXECUTIONS FIRST_LOAD_TIME LAST_EXECUTION_DATE   PRIORITY COMMAND_TYPE STAT_PERIOD ACTIVE_STAT_PERIOD SQL_TEXT                                                                            PRECOST   POSTCOST IMPORTANCE     REC_ID  VALIDATED
-------------------------------------------------------------------------------- ---------- ---------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ------------- ---------- --------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------- ----------- ---------- ------------ -------------- ---------- --------------- ------------------- ---------- ------------ ----------- ------------------ -------------------------------------------------------------------------------- ---------- ---------- ---------- ---------- ----------
mytask1  
set LONGCHUNKSIZE 999999
set serveroutput on size 999999
set long 999999

SQL> select  dbms_advisor.get_task_script('mytask1') from dual;
DBMS_ADVISOR.GET_TASK_SCRIPT('
--------------------------------------------------------------------------------
Rem  SQL 访问指导: 版本 18.0.0.0.0 - 正式版
Rem
Rem  用户名:     TEST
Rem  任务:        mytask1
Rem  执行日期:
Rem
CREATE INDEX "TEST"."TESTOBJ_IDX$$_002B0000"
    ON "TEST"."TESTOBJ"
    ("OBJECT_ID")
    COMPUTE STATISTICS;

查看优化效果

select a.owner,
       a.task_id,
       a.task_name,
       execution_start,
       a.status_message,
       b.command from dba_advisor_log a,
       dba_advisor_actions          b where a.task_id = b.task_id and b.task_name = 'mytask1'

       OWNER    TASK_ID    TASK_NAME    EXECUTION_START    STATUS_MESSAGE    COMMAND
1    TEST    41    mytask1    2018/12/13 14:54:57    访问指导执行完毕    CREATE INDEX
 

SELECT sql_id,
       precost 优化前cost,
       postcost 优化后cost,
       (precost / postcost) cost提升倍数,decode(PRIORITY, 1, '高', 2, '中', 3, '低') 重要性
  FROM dba_advisor_sqla_wk_stmts
  WHERE
  task_name='mytask1';
 

       SQL_ID    优化前COST    优化后COST    COST提升倍数    重要性
1    3gc62uk6vc0pu    389    3    129.666666666666666666666666666666666667    中
 

感觉报告比sta牛逼很多

exec dbms_advisor.delete_task('mytask1');

 

 

 

你可能感兴趣的:(SQL访问顾问SAA(SQL Access Advisor)针对单条sql优化实践)