oracle實用sql(3)--sql tuning advisor

selectshen發表於2016-06-03

點選(此處)摺疊或開啟

  1. --以下是sql tuning advisor調優sql常用指令碼,透過coe_xfr_sql_profile調優參見:http://blog.itpub.net/28539951/viewspace-1603192/

  2. --os:centos 6.6
  3. --db:11.2.0.4

  4. --建測試表
  5. create table scott.t_test01 as select * from dba_objects;
  6. --插入資料
  7. insert into scott.t_test01 select * from scott.t_test01;
  8. commit;
  9. insert into scott.t_test01 select * from scott.t_test01;
  10. commit;
  11. insert into scott.t_test01 select * from scott.t_test01;
  12. commit;

  13. --建立生成sql tuning advisor
  14. DECLARE
  15.   ret_val VARCHAR2(4000);
  16.   sqltext CLOB;
  17. BEGIN
  18.   sqltext := 'select * from scott.t_test01 where owner=''SCOTT''';
  19.   --sql標識可以用sql_text也可以用sql_id
  20.   ret_val := DBMS_SQLTUNE.CREATE_TUNING_TASK(
  21.                 sql_text => sqltext,
  22.                 bind_list => NULL,
  23.                 user_name => 'SYS',
  24.                 scope => 'COMPREHENSIVE',
  25.                 time_limit => 1800,
  26.                 task_name => 'sql_tune_002',
  27.                 description => 'SQL Tuning Advisor Task');
  28.   Dbms_Sqltune.EXECUTE_TUNING_TASK(ret_val);
  29. End;

  30. --檢視sql tuning advisor report
  31. select Dbms_Sqltune.REPORT_TUNING_TASK('sql_tune_002', 'TEXT', 'ALL') report from dual;
  32. /*
  33. GENERAL INFORMATION SECTION
  34. -------------------------------------------------------------------------------
  35. Tuning Task Name : sql_tune_002
  36. Tuning Task Owner : SYSTEM
  37. Tuning Task ID : 62
  38. Workload Type : Single SQL Statement
  39. Execution Count : 1
  40. Current Execution : EXEC_52
  41. Execution Type : TUNE SQL
  42. Scope : COMPREHENSIVE
  43. Time Limit(seconds): 1800
  44. Completion Status : COMPLETED
  45. Started at : 04/26/2016 19:53:42
  46. Completed at : 04/26/2016 19:53:43

  47. -------------------------------------------------------------------------------
  48. Schema Name: SYS
  49. SQL ID : 7z30ga5js6pvn
  50. SQL Text : select * from scott.t_test01 where owner='SCOTT'

  51. -------------------------------------------------------------------------------
  52. FINDINGS SECTION (2 findings)
  53. -------------------------------------------------------------------------------

  54. 1- Statistics Finding
  55. ---------------------
  56.   Table "SCOTT"."T_TEST01" was not analyzed.

  57.   Recommendation
  58.   --------------
  59.   - Consider collecting optimizer statistics for this table.
  60.     execute dbms_stats.gather_table_stats(ownname => 'SCOTT', tabname =>
  61.             'T_TEST01', estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
  62.             method_opt => 'FOR ALL COLUMNS SIZE AUTO');

  63.   Rationale
  64.   ---------
  65.     The optimizer requires up-to-date statistics for the table in order to
  66.     select a good execution plan.

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

  71.   Recommendation (estimated benefit: 99.81%)
  72.   ------------------------------------------
  73.   - Consider running the Access Advisor to improve the physical schema design
  74.     or creating the recommended index.
  75.     create index SCOTT.IDX$$_003E0001 on SCOTT.T_TEST01("OWNER");

  76.   Rationale
  77.   ---------
  78.     Creating the recommended indices significantly improves the execution plan
  79.     of this statement. However, it might be preferable to run "Access Advisor"
  80.     using a representative SQL workload as opposed to a single statement. This
  81.     will allow to get comprehensive index recommendations which takes into
  82.     account index maintenance overhead and additional space consumption.

  83. -------------------------------------------------------------------------------
  84. EXPLAIN PLANS SECTION
  85. -------------------------------------------------------------------------------

  86. 1- Original
  87. -----------
  88. Plan hash value: 3092827266

  89.  
  90. ------------------------------------------------------------------------------
  91. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  92. ------------------------------------------------------------------------------
  93. | 0 | SELECT STATEMENT | | 111 | 22977 | 2749 (1)| 00:00:33 |
  94. |* 1 | TABLE ACCESS FULL| T_TEST01 | 111 | 22977 | 2749 (1)| 00:00:33 |
  95. ------------------------------------------------------------------------------
  96.  
  97. Query Block Name / Object Alias (identified by operation id):
  98. -------------------------------------------------------------
  99.  
  100.    1 - SEL$1 / T_TEST01@SEL$1
  101.  
  102. Predicate Information (identified by operation id):
  103. ---------------------------------------------------
  104.  
  105.    1 - filter("OWNER"='SCOTT')
  106.  
  107. Column Projection Information (identified by operation id):
  108. -----------------------------------------------------------
  109.  
  110.    1 - "OWNER"[VARCHAR2,30], "T_TEST01"."OBJECT_NAME"[VARCHAR2,128],
  111.        "T_TEST01"."SUBOBJECT_NAME"[VARCHAR2,30],
  112.        "T_TEST01"."OBJECT_ID"[NUMBER,22], "T_TEST01"."DATA_OBJECT_ID"[NUMBER,22
  113.        ], "T_TEST01"."OBJECT_TYPE"[VARCHAR2,19], "T_TEST01"."CREATED"[DATE,7],
  114.        "T_TEST01"."LAST_DDL_TIME"[DATE,7],
  115.        "T_TEST01"."TIMESTAMP"[VARCHAR2,19], "T_TEST01"."STATUS"[VARCHAR2,7],
  116.        "T_TEST01"."TEMPORARY"[VARCHAR2,1], "T_TEST01"."GENERATED"[VARCHAR2,1],
  117.        "T_TEST01"."SECONDARY"[VARCHAR2,1], "T_TEST01"."NAMESPACE"[NUMBER,22],
  118.        "T_TEST01"."EDITION_NAME"[VARCHAR2,30]
  119.  
  120. Note
  121. -----
  122.    - dynamic sampling used for this statement (level=2)

  123. 2- Using New Indices
  124. --------------------
  125. Plan hash value: 3193164626

  126.  
  127. ----------------------------------------------------------------------------------------------
  128. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  129. ----------------------------------------------------------------------------------------------
  130. | 0 | SELECT STATEMENT | | 56 | 11592 | 5 (0)| 00:00:01 |
  131. | 1 | TABLE ACCESS BY INDEX ROWID| T_TEST01 | 56 | 11592 | 5 (0)| 00:00:01 |
  132. |* 2 | INDEX RANGE SCAN | IDX$$_003E0001 | 56 | | 3 (0)| 00:00:01 |
  133. ----------------------------------------------------------------------------------------------
  134.  
  135. Query Block Name / Object Alias (identified by operation id):
  136. -------------------------------------------------------------
  137.  
  138.    1 - SEL$1 / T_TEST01@SEL$1
  139.    2 - SEL$1 / T_TEST01@SEL$1
  140.  
  141. Predicate Information (identified by operation id):
  142. ---------------------------------------------------
  143.  
  144.    2 - access("OWNER"='SCOTT')
  145.  
  146. Column Projection Information (identified by operation id):
  147. -----------------------------------------------------------
  148.  
  149.    1 - "OWNER"[VARCHAR2,30], "T_TEST01"."OBJECT_NAME"[VARCHAR2,128],
  150.        "T_TEST01"."SUBOBJECT_NAME"[VARCHAR2,30], "T_TEST01"."OBJECT_ID"[NUMBER,22],
  151.        "T_TEST01"."DATA_OBJECT_ID"[NUMBER,22], "T_TEST01"."OBJECT_TYPE"[VARCHAR2,19],
  152.        "T_TEST01"."CREATED"[DATE,7], "T_TEST01"."LAST_DDL_TIME"[DATE,7],
  153.        "T_TEST01"."TIMESTAMP"[VARCHAR2,19], "T_TEST01"."STATUS"[VARCHAR2,7],
  154.        "T_TEST01"."TEMPORARY"[VARCHAR2,1], "T_TEST01"."GENERATED"[VARCHAR2,1],
  155.        "T_TEST01"."SECONDARY"[VARCHAR2,1], "T_TEST01"."NAMESPACE"[NUMBER,22],
  156.        "T_TEST01"."EDITION_NAME"[VARCHAR2,30]
  157.    2 - "T_TEST01".ROWID[ROWID,10], "OWNER"[VARCHAR2,30]
  158.  
  159. Note
  160. -----
  161.    - dynamic sampling used for this statement (level=2)

  162. -------------------------------------------------------------------------------
  163. */


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

相關文章