Utilizando o SQL Tuning Advisor (DBMS_SQLTUNE)
Autor: Mario Barduchi | Publicado: 2016-12-06T14:55:00-02:00


Fala pessoal...



Hoje fazendo uns acertos aqui no meu ambiente, precisei utilizar o SQL Tuning Advisor.



Ele é bem simples, mas é sempre bom lembrar que precisamos ter o "Diagnostic+Tunig" devidamente licenciado e habilitado.



Sendo assim, abaixo segue um exemplo da utilização.





-- Executar o ADDM.&nbsp;


 @?/rdbms/admin/addmrpt.sql



Na verdade baseado no ADDM identifiquei as necessidades iniciais tendo em vista que é um ambiente que não administro a muito tempo. Sendo assim, não é obrigatório executar esse relatório.





-- Create task

DECLARE

&nbsp; l_sql_tune_task_id &nbsp;VARCHAR2(100);

BEGIN

&nbsp; l_sql_tune_task_id := DBMS_SQLTUNE.create_tuning_task (

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin_snap &nbsp;=&gt; 34932,    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;-- Snap inicial

 &nbsp; &nbsp; &nbsp; &nbsp; end_snap &nbsp; &nbsp;=&gt; 35220,    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Snap final

 &nbsp; &nbsp; &nbsp; &nbsp; sql_id &nbsp; &nbsp; &nbsp;=&gt; 'cyb0csh3g3vxa',  &nbsp; -- SQL Id

 &nbsp; &nbsp; &nbsp; &nbsp; scope &nbsp; &nbsp; &nbsp; =&gt; DBMS_SQLTUNE.scope_comprehensive,

 &nbsp; &nbsp; &nbsp; &nbsp; time_limit &nbsp;=&gt; 3600,    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- Execução em seg

 &nbsp; &nbsp; &nbsp; &nbsp; task_name &nbsp; =&gt; 'cyb0csh3g3vxa_AWR2_tuning_task',&nbsp;

 &nbsp; &nbsp; &nbsp; &nbsp; description =&gt; 'Task: cyb0csh3g3vxa');

&nbsp; &nbsp;DBMS_OUTPUT.put_line('l_sql_tune_task_id: ' || l_sql_tune_task_id);

END;

/




No exemplo acima utilizei uma período do meu AWR para limitar a minha task. Se não quiser utilizar os snaps, basta excluir essas duas linhas.




-- Execute task 

&nbsp; &nbsp;EXEC DBMS_SQLTUNE.execute_tuning_task(

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;task_name =&gt; 'cyb0csh3g3vxa_AWR2_tuning_task');





-- Verify status

&nbsp; &nbsp;SELECT&nbsp;

     task_id,&nbsp;

     task_name,&nbsp;

     status

&nbsp; &nbsp;FROM&nbsp;

     dba_advisor_log

&nbsp; &nbsp;WHERE&nbsp;

     task_name like '%cyb0csh3g3vxa%'

&nbsp; &nbsp;order by&nbsp;

     task_id;



&nbsp; TASK_ID &nbsp;TASK_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;STATUS

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

&nbsp; &nbsp; 154921 cyb0csh3g3vxa_AWR2_tuning_task COMPLETED



  

-- Verify recomendations&nbsp;

 SET LONG 10000;

 SET PAGESIZE 1000

 SET LINESIZE 200

&nbsp; &nbsp;SELECT&nbsp;

&nbsp; &nbsp; &nbsp; DBMS_SQLTUNE.report_tuning_task(

&nbsp; &nbsp; &nbsp; 'cyb0csh3g3vxa_AWR2_tuning_task'

&nbsp; &nbsp; &nbsp; ) AS recommendations&nbsp;

&nbsp; &nbsp;FROM&nbsp;

&nbsp; &nbsp; &nbsp; dual;



Nesse momento será apresentado uma relatório com as recomendações de melhorias.



Example:



RECOMMENDATIONS

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

GENERAL INFORMATION SECTION

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

Tuning Task Name &nbsp; :&nbsp;cyb0csh3g3vxa_AWR2_tuning_task

Tuning Task Owner &nbsp;: SYS

Workload Type &nbsp; &nbsp; &nbsp;: Single SQL Statement

Scope &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: COMPREHENSIVE

Time Limit(seconds): 1200

Completion Status &nbsp;: COMPLETED

Started at &nbsp; &nbsp; &nbsp; &nbsp; : 12/06/2016 06:24:20

Completed at &nbsp; &nbsp; &nbsp; : 12/06/2016 06:24:22



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

Schema Name: PRD1_GC_APP

SQL ID &nbsp; &nbsp; :&nbsp;cyb0csh3g3vxa


SQL Text &nbsp; : select tbatchjobs0_.SUB_ID as SUB1_34_, tbatchjobs0_.RUN_ID as



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RUN2_34_, tbatchjobs0_.PROCESS_TYPE as PROCESS3_34_,



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tbatchjobs0_.PIECE_ID as PIECE4_34_, tbatchjobs0_.CREATE_TIME as



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CREATE5_34_, tbatchjobs0_.START_TIME as START6_34_,



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tbatchjobs0_.FINISH_TIME as FINISH7_34_, tbatchjobs0_.AGENT_ID



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;as AGENT8_34_, tbatchjobs0_.THREAD_ID as THREAD9_34_,



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tbatchjobs0_.STATUS as STATUS34_, tbatchjobs0_.RESULT_INFO as



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RESULT11_34_ from T_BATCH_JOB_SUB tbatchjobs0_ where



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tbatchjobs0_.AGENT_ID=1 and tbatchjobs0_.STATUS&lt;&gt;107 and



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tbatchjobs0_.STATUS&lt;&gt;105 and tbatchjobs0_.STATUS&lt;&gt;223 and



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tbatchjobs0_.STATUS&lt;&gt;106 order by tbatchjobs0_.SUB_ID






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

FINDINGS SECTION (3 findings)

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



1- Restructure SQL finding (see plan 1 in explain plans section)

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

&nbsp; Predicate "TBATCHJOBS0_"."STATUS"&lt;&gt;107 used at line ID 2 of the execution

&nbsp; plan is an inequality condition on indexed column "STATUS". This inequality

&nbsp; condition prevents the optimizer from selecting indices &nbsp;on table

&nbsp; "PRD1_GC"."T_BATCH_JOB_SUB".



&nbsp; Recommendation

&nbsp; --------------

&nbsp; - Rewrite the predicate into an equivalent form to take advantage of

&nbsp; &nbsp; indices.



2- Statistics Finding

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

&nbsp; Optimizer statistics for table "PRD1_GS"."T_CTS_BCP_GL_FEE" and its indices

&nbsp; are stale.



&nbsp; Recommendation

&nbsp; --------------

&nbsp; - Consider collecting optimizer statistics for this table.

&nbsp; &nbsp; execute dbms_stats.gather_table_stats(

&nbsp; &nbsp; &nbsp; &nbsp; ownname =&gt; 'PRD1_GS',&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; tabname =&gt; 'T_CTS_BCP_GL_FEE',&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; estimate_percent =&gt; DBMS_STATS.AUTO_SAMPLE_SIZE,&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; method_opt =&gt; 'FOR ALL COLUMNS SIZE AUTO');



&nbsp; Rationale

&nbsp; ---------

&nbsp; &nbsp; The optimizer requires up-to-date statistics for the table in order to

&nbsp; &nbsp; select a good execution plan.



3- SQL Profile Finding (see explain plans section below)

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

&nbsp; A potentially better execution plan was found for this statement.



&nbsp; Recommendation (estimated benefit: 48.56%)

&nbsp; ------------------------------------------

&nbsp; - Consider accepting the recommended SQL profile.

&nbsp; &nbsp; execute dbms_sqltune.accept_sql_profile(

   task_name =&gt; 'cyb0csh3g3vxa_AWR2_tuning_task',&nbsp;

   task_owner =&gt; 'SYS',&nbsp;

   replace =&gt; TRUE);



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

EXPLAIN PLANS SECTION

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



1- Original

-----------

Plan hash value: 4019928224



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

| Id &nbsp;| Operation &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| Rows &nbsp;| Bytes | Cost (%CPU)| Time &nbsp; &nbsp; |

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

| &nbsp; 0 | SELECT STATEMENT &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp;7983 | &nbsp; 350K| &nbsp;2063 &nbsp; (3)| 00:00:21 |

| &nbsp; 1 | &nbsp;SORT ORDER BY &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp;7983 | &nbsp; 350K| &nbsp;2063 &nbsp; (3)| 00:00:21 |

|* &nbsp;2 | &nbsp; TABLE ACCESS FULL| T_BATCH_JOB_SUB | &nbsp;7983 | &nbsp; 350K| &nbsp;2061 &nbsp; (3)| 00:00:21 |

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



Predicate Information (identified by operation id):

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



&nbsp; &nbsp;2 - filter("TBATCHJOBS0_"."STATUS"&lt;&gt;107 AND "TBATCHJOBS0_"."STATUS"&lt;&gt;105

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AND "TBATCHJOBS0_"."STATUS"&lt;&gt;106 AND "TBATCHJOBS0_"."AGENT_ID"=1 AND

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "TBATCHJOBS0_"."STATUS"&lt;&gt;223)

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



É isso ai pessoal, bem simples e objetivo.&nbsp;

Agora é só analisar essas sugestões e lembrar-se que o advisor fornece sugestões de melhorias e não uma receita de bolo pronta e infalível.&nbsp;

Portanto, o seu resultado e sua utilização devem ser analisados e testados com muito cuidado.

Abraço

Mario