SQL Access Advisor、SQL Tuning Advisor 測試

Davis_itpub發表於2018-06-27
<div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 一.<span style="color:#565656;font-family:宋體, Arial;font-size:12px;line-height:12px;background-color:#FFFFFF;">&nbsp;</span>SQL Access Advisor </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 前期準備建立測試表 </div> <span style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;">SYS@jun1&gt;drop table enmo.t5;</span> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 建立ENMO這張表 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;create table enmo.t5 tablespace users as select rownum id from dual connect by rownum&lt;=100000;<br /> <br /> Table created.<br /> 建立索引<br /> SYS@jun1&gt;create index enmo.idx_t5_id on enmo.t5(id) tablespace users;<br /> <br /> Index created.<br /> 對錶進行分析<br /> SYS@jun1&gt;exec dbms_stats.gather_table_stats(ownname=&gt;'ENMO',tabname=&gt;'T5');<br /> <br /> PL/SQL procedure successfully completed. </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 做測試,使用hint強制走全表掃描 看是否給出建議 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;DECLARE<br /> &nbsp; 2&nbsp; v_task_name VARCHAR2(30);<br /> &nbsp; 3&nbsp; v_sql varchar2(200);<br /> &nbsp; 4&nbsp; BEGIN<br /> &nbsp; 5&nbsp; v_task_name :='MY_TASK_ACCESS_ADVISOR';<br /> &nbsp; 6&nbsp; v_sql:='select /*+ full(t) */ * from enmo.t5 where id=:1';<br /> &nbsp; 7&nbsp;&nbsp; DBMS_ADVISOR.QUICK_TUNE(DBMS_ADVISOR.SQLACCESS_ADVISOR,v_task_name,v_sql);<br /> &nbsp; 8&nbsp; END;<br /> &nbsp; 9&nbsp; /<br /> <br /> PL/SQL procedure successfully completed. </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 查詢,結果為1 。。。。剛才的全表掃Oracle已給出對應的建議 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;select count(*) from user_advisor_actions where task_name='MY_TASK_ACCESS_ADVISOR';<br /> <br /> &nbsp; COUNT(*)<br /> ----------<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1<br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 檢視給出的建議 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;set long 100000<br /> SYS@jun1&gt;set pagesize 50000 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;SELECT DBMS_ADVISOR.get_task_script('MY_TASK_ACCESS_ADVISOR') AS script FROM dual;<br /> <br /> SCRIPT<br /> --------------------------------------------------------------------------------<br /> Rem&nbsp; SQL Access Advisor: Version 11.2.0.4.0 - Production<br /> Rem<br /> Rem&nbsp; Username:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SYS<br /> Rem&nbsp; Task:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MY_TASK_ACCESS_ADVISOR<br /> Rem&nbsp; Execution date:<br /> Rem<br /> <br /> <span style="color:#AD0000;">/* RETAIN INDEX "ENMO"."IDX_T5_ID" */</span> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 也可輸出到到目錄下檢視 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;create or replace directory dir_aa as '/home/oracle'; </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> Directory created. </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;!cat /home/oracle/s.sql<br /> Rem&nbsp; SQL Access Advisor: Version 11.2.0.4.0 - Production<br /> Rem&nbsp;<br /> Rem&nbsp; Username:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SYS<br /> Rem&nbsp; Task:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MY_TASK_ACCESS_ADVISOR<br /> Rem&nbsp; Execution date:&nbsp;&nbsp;<br /> Rem&nbsp;<br /> <br /> /* RETAIN INDEX "ENMO"."IDX_T5_ID" */<br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 二.SQL Tuning Advisor() </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 建立使用者授權,並授予advisor給調優使用者 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;create user bamboo identified by bamboo;<br /> <br /> User created.<br /> <br /> SYS@jun1&gt;grant connect,resource to bamboo;<br /> <br /> Grant succeeded.<br /> <br /> SYS@jun1&gt;grant advisor to bamboo;<br /> <br /> Grant succeeded.<br /> <br /> SYS@jun1&gt;create table bigtable (id number(10),name varchar2(100));<br /> <br /> Table created. </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 建立兩張測試表 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;begin<br /> &nbsp; 2&nbsp; for i in 1..5000000 loop<br /> &nbsp; 3&nbsp; insert into bigtable values(i,'test'||i);<br /> &nbsp; 4&nbsp; end loop;<br /> &nbsp; 5&nbsp; end;<br /> &nbsp; 6&nbsp; /<br /> <br /> PL/SQL procedure successfully completed.<br /> <br /> SYS@jun1&gt;commit;<br /> <br /> Commit complete.<br /> <br /> SYS@jun1&gt; create table smalltable (id number(10),name varchar2(100));<br /> <br /> Table created.<br /> <br /> SYS@jun1&gt;begin<br /> &nbsp; 2&nbsp; for i in 1..100000 loop<br /> &nbsp; 3&nbsp; insert into smalltable values(i,'test'||i);<br /> &nbsp; 4&nbsp; end loop;<br /> &nbsp; 5&nbsp; end;<br /> &nbsp; 6&nbsp; /<br /> <br /> PL/SQL procedure successfully completed.<br /> <br /> SYS@jun1&gt;commit; </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 對於兩張表做一個等連線查詢 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 首先開啟跟蹤計劃 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;set autotrace on; </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;select a.id,a.name,b.id,b.name from bigtable a,smalltable b where a.id=b.id and a.id=40000;<br /> <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ID<br /> ----------<br /> NAME<br /> --------------------------------------------------------------------------------<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ID<br /> ----------<br /> NAME<br /> --------------------------------------------------------------------------------<br /> &nbsp;&nbsp;&nbsp;&nbsp; 40000<br /> test40000<br /> &nbsp;&nbsp;&nbsp;&nbsp; 40000<br /> test40000<br /> <br /> <br /> <br /> Execution Plan<br /> ----------------------------------------------------------<br /> Plan hash value: 1703851322<br /> <br /> --------------------------------------------------------------------------------<br /> -<br /> <br /> | Id&nbsp; | Operation&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | Rows&nbsp; | Bytes | Cost (%CPU)| Time<br /> |<br /> <br /> --------------------------------------------------------------------------------<br /> -<br /> <br /> |&nbsp;&nbsp; 0 | SELECT STATEMENT&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; 517 | 67210 |&nbsp; 4334&nbsp;&nbsp; (2)| 00:00:53<br /> |<br /> <br /> |*&nbsp; 1 |&nbsp; HASH JOIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp; 517 | 67210 |&nbsp; 4334&nbsp;&nbsp; (2)| 00:00:53<br /> |<br /> <br /> |*&nbsp; 2 |&nbsp;&nbsp; TABLE ACCESS FULL| SMALLTABLE |&nbsp;&nbsp;&nbsp;&nbsp; 3 |&nbsp;&nbsp; 195 |&nbsp;&nbsp;&nbsp; 77&nbsp;&nbsp; (2)| 00:00:01<br /> |<br /> <br /> |*&nbsp; 3 |&nbsp;&nbsp; TABLE ACCESS FULL| BIGTABLE&nbsp;&nbsp; |&nbsp;&nbsp; 171 | 11115 |&nbsp; 4257&nbsp;&nbsp; (2)| 00:00:52<br /> |<br /> <br /> --------------------------------------------------------------------------------<br /> -<br /> <br /> <br /> Predicate Information (identified by operation id):<br /> ---------------------------------------------------<br /> <br /> &nbsp;&nbsp; 1 - access("A"."ID"="B"."ID")<br /> &nbsp;&nbsp; 2 - filter("B"."ID"=40000)<br /> &nbsp;&nbsp; 3 - filter("A"."ID"=40000)<br /> <br /> Note<br /> -----<br /> &nbsp;&nbsp; - dynamic sampling used for this statement (level=2)<br /> <br /> <br /> Statistics<br /> ----------------------------------------------------------<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp; recursive calls<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp; db block gets<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 15825&nbsp; consistent gets<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9520&nbsp; physical reads<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp; redo size<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 739&nbsp; bytes sent via SQL*Net to client<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 523&nbsp; bytes received via SQL*Net from client<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp; SQL*Net roundtrips to/from client<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp; sorts (memory)<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp; sorts (disk)<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp; rows processed<br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 兩張都走了全表掃描,物理讀9520&nbsp; physical reads。設立最佳化器,看給出意見 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> 透過<span style="background-color:#FFFFFF;color:#333333;font-size:14px;line-height:25px;">DBMS_SQLTUNE包的CREATE_TUNING_TASK來建立任務,透過</span>DBMS_SQLTUNE.CREATE_TUNING_TASK 執行 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;DECLARE&nbsp;<br /> &nbsp; 2&nbsp; my_task_name VARCHAR2(30);&nbsp;<br /> &nbsp; 3&nbsp; my_sqltext CLOB;&nbsp;<br /> &nbsp; 4&nbsp; BEGIN&nbsp;<br /> &nbsp; 5&nbsp; my_sqltext := 'select a.id,a.name,b.id,b.name from bigtable a,smalltable b where a.id=b.id and a.id=40000';&nbsp;&nbsp;&nbsp;<br /> &nbsp; 6&nbsp; my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK(&nbsp;<br /> &nbsp; 7&nbsp; sql_text =&gt; my_sqltext,&nbsp;<br /> &nbsp; 8&nbsp; user_name =&gt; 'BAMBOO',&nbsp;<br /> &nbsp; 9&nbsp; scope =&gt; 'COMPREHENSIVE',&nbsp;<br /> 10&nbsp; time_limit =&gt; 60,&nbsp;<br /> 11&nbsp; task_name =&gt; 'test_sql_tuning_task1',&nbsp;<br /> 12&nbsp; description =&gt; 'Task to tune a query');&nbsp;<br /> 13&nbsp; DBMS_SQLTUNE.EXECUTE_TUNING_TASK(task_name =&gt; 'test_sql_tuning_task1');<br /> 14&nbsp; END;&nbsp;<br /> 15&nbsp; /<br /> <br /> PL/SQL procedure successfully completed.<br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt; select task_name,ADVISOR_NAME,STATUS from user_advisor_tasks;<br /> <br /> 檢視test_sql_tuning_task1 &nbsp;狀態 </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> SYS@jun1&gt;set autotrace off;<br /> SYS@jun1&gt;set long 999999<br /> SYS@jun1&gt;set LONGCHUNKSIZE 999999&nbsp;<br /> SYS@jun1&gt;set serveroutput on size 999999<br /> SYS@jun1&gt;set linesize 200<br /> SYS@jun1&gt;select<br /> &nbsp; 2&nbsp; dbms_sqltune.report_tuning_task('test_sql_tuning_task1') from dual; </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <div style="font-family:微軟雅黑;line-height:normal;orphans:2;text-align:-webkit-auto;white-space:normal;widows:2;font-size:medium;"> <br /> </div> <br />

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31520497/viewspace-2156810/,如需轉載,請註明出處,否則將追究法律責任。

相關文章