手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle SQL调优:使用outline稳固sql执行计划

[日期:2012-07-29] 来源:Linux社区  作者:aqszhuaihuai [字体:大 中 小]

outline for 绑定变量  
1.获取带绑定变量sql的  child_number,hash_value
select  child_number,hash_value,address,sql_text from v$sql
 where sql_text like 'select * from t where id%';

2.创建outline 
begin
    dbms_outln.create_outline (
       hash_value =>3573770389,
       child_number =>0,
       category =>'CATE_OUTLINE');
end;
/  


 

  1. SQL> var v_id number;  
  2. SQL> exec :v_id :=5;  
  3.   
  4. PL/SQL 过程已成功完成。  
  5.   
  6. 提交完成。  
  7. SQL> set autot traceonly  
  8. SQL> select * from t where id=:v_id;  
  9.   
  10.   
  11. 执行计划  
  12. ----------------------------------------------------------   
  13. Plan hash value: 1601196873  
  14.   
  15. --------------------------------------------------------------------------   
  16. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  17. --------------------------------------------------------------------------   
  18. |   0 | SELECT STATEMENT  |      |   100 |  1300 |     7  (15)| 00:00:01 |  
  19. |*  1 |  TABLE ACCESS FULL| T    |   100 |  1300 |     7  (15)| 00:00:01 |  
  20. --------------------------------------------------------------------------   
  21.   
  22. Predicate Information (identified by operation id):  
  23. ---------------------------------------------------   
  24.   
  25.    1 - filter("ID"=TO_NUMBER(:V_ID))  
  26.   
  27. Note  
  28. -----   
  29.    - dynamic sampling used for this statement  
  30.   
  31.   
  32. 统计信息  
  33. ----------------------------------------------------------   
  34.           0  recursive calls  
  35.           0  db block gets  
  36.           0  consistent gets  
  37.           0  physical reads  
  38.           0  redo size  
  39.           0  bytes sent via SQL*Net to client  
  40.           0  bytes received via SQL*Net from client  
  41.           0  SQL*Net roundtrips to/from client  
  42.           0  sorts (memory)  
  43.           0  sorts (disk)  
  44.           1  rows processed  
  45.   
  46. SQL> col child_number for 999999999999  
  47. SQL> col hash_values for 999999999999  
  48. SQL> col hash_value for 999999999999  
  49. SQL> col address for a20  
  50. SQL> col sql_text for a50  
  51. SQL> set linesize 200  
  52.   
  53. SQL> select  child_number,hash_value,address,sql_text from v$sql  
  54.   2   where sql_text like 'select * from t where id%';  
  55.   
  56.  CHILD_NUMBER    HASH_VALUE ADDRESS              SQL_TEXT  
  57. ------------- ------------- -------------------- --------------------------------------------------   
  58.             0    3573770389 22E58344             select * from t where id=:v_id  
  59.   
  60. SQL> begin  
  61.   2     dbms_outln.create_outline (  
  62.   3        hash_value =>3573770389,  
  63.   4        child_number =>0,  
  64.   5        category =>'CATE_OUTLINE');  
  65.   6  end;  
  66.   7  /  
  67.   
  68. PL/SQL 过程已成功完成。  
  69.   
  70. 提交完成。  
  71. SQL> select name, sql_text ,used,category  
  72.   2    from user_outlines  
  73.   3   where category=upper('cate_outline');  
  74.   
  75. NAME                 SQL_TEXT                                           USED       CATEGORY  
  76. -------------------- -------------------------------------------------- ---------- ------------------------------   
  77. SYS_OUTLINE_12071817 select * from t where id=:v_id                     UNUSED     CATE_OUTLINE  
  78. 153216201  
  79.   
  80. SQL>  alter system set  USE_STORED_OUTLINES =cate_outline;  
  81.   
  82. 系统已更改。  
  83.   
  84. SQL> set autot traceonly  
  85.   
  86. SQL> exec :v_id :=1;  
  87.   
  88. PL/SQL 过程已成功完成。  
  89.   
  90. 提交完成。  
  91. SQL> select * from t where id=:v_id;  
  92.   
  93.   
  94. 执行计划  
  95. ----------------------------------------------------------   
  96. Plan hash value: 1601196873  
  97.   
  98. --------------------------------------------------------------------------   
  99. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  100. --------------------------------------------------------------------------   
  101. |   0 | SELECT STATEMENT  |      |    16 |   208 |     6   (0)| 00:00:01 |  
  102. |*  1 |  TABLE ACCESS FULL| T    |    16 |   208 |     6   (0)| 00:00:01 |  
  103. --------------------------------------------------------------------------   
  104.   
  105. Predicate Information (identified by operation id):  
  106. ---------------------------------------------------   
  107.   
  108.    1 - filter("ID"=TO_NUMBER(:V_ID))  
  109.   
  110. Note  
  111. -----   
  112.    - outline "SYS_OUTLINE_12071817153216201" used for this statement  
  113.   
  114.   
  115. 统计信息  
  116. ----------------------------------------------------------   
  117.          35  recursive calls  
  118.         123  db block gets  
  119.          44  consistent gets  
  120.           0  physical reads  
  121.         632  redo size  
  122.         402  bytes sent via SQL*Net to client  
  123.         385  bytes received via SQL*Net from client  
  124.           2  SQL*Net roundtrips to/from client  
  125.           2  sorts (memory)  
  126.           0  sorts (disk)  
  127.           1  rows processed  
  128.   
  129. SQL> exec :v_id :=10;  
  130.   
  131. PL/SQL 过程已成功完成。  
  132.   
  133. 提交完成。  
  134. SQL> select * from t where id=:v_id;  
  135.   
  136.   
  137. 执行计划  
  138. ----------------------------------------------------------   
  139. Plan hash value: 1601196873  
  140.   
  141. --------------------------------------------------------------------------   
  142. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  143. --------------------------------------------------------------------------   
  144. |   0 | SELECT STATEMENT  |      |    16 |   208 |     6   (0)| 00:00:01 |  
  145. |*  1 |  TABLE ACCESS FULL| T    |    16 |   208 |     6   (0)| 00:00:01 |  
  146. --------------------------------------------------------------------------   
  147.   
  148. Predicate Information (identified by operation id):  
  149. ---------------------------------------------------   
  150.   
  151.    1 - filter("ID"=TO_NUMBER(:V_ID))  
  152.   
  153. Note  
  154. -----   
  155.    - outline "SYS_OUTLINE_12071817153216201" used for this statement  
  156.   
  157.   
  158. 统计信息  
  159. ----------------------------------------------------------   
  160.           0  recursive calls  
  161.           0  db block gets  
  162.          24  consistent gets  
  163.           0  physical reads  
  164.           0  redo size  
  165.         402  bytes sent via SQL*Net to client  
  166.         385  bytes received via SQL*Net from client  
  167.           2  SQL*Net roundtrips to/from client  
  168.           0  sorts (memory)  
  169.           0  sorts (disk)  
  170.           1  rows processed  
  171.   
  172. SQL> create index ind_t_id on t(id);  
  173.   
  174. 索引已创建。  
  175.   
  176. SQL> comment on column t.id is 'dddd';  
  177.   
  178. 注释已创建。  
  179.   
  180. SQL> select * from t where id=:v_id;  
  181.   
  182.   
  183. 执行计划  
  184. ----------------------------------------------------------   
  185. Plan hash value: 1601196873  
  186.   
  187. --------------------------------------------------------------------------   
  188. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  189. --------------------------------------------------------------------------   
  190. |   0 | SELECT STATEMENT  |      |    16 |   208 |     6   (0)| 00:00:01 |  
  191. |*  1 |  TABLE ACCESS FULL| T    |    16 |   208 |     6   (0)| 00:00:01 |  
  192. --------------------------------------------------------------------------   
  193.   
  194. Predicate Information (identified by operation id):  
  195. ---------------------------------------------------   
  196.   
  197.    1 - filter("ID"=TO_NUMBER(:V_ID))  
  198.   
  199. Note  
  200. -----   
  201.    - outline "SYS_OUTLINE_12071817153216201" used for this statement  
  202.   
  203.   
  204. 统计信息  
  205. ----------------------------------------------------------   
  206.           1  recursive calls  
  207.           0  db block gets  
  208.          24  consistent gets  
  209.           0  physical reads  
  210.           0  redo size  
  211.         402  bytes sent via SQL*Net to client  
  212.         385  bytes received via SQL*Net from client  
  213.           2  SQL*Net roundtrips to/from client  
  214.           0  sorts (memory)  
  215.           0  sorts (disk)  
  216.           1  rows processed  
  217.   
  218. SQL> alter system set use_stored_outlines=false;  
  219.   
  220. 系统已更改。  
  221.   
  222. SQL> select * from t where id=:v_id;  
  223.   
  224.   
  225. 执行计划  
  226. ----------------------------------------------------------   
  227. Plan hash value: 3343177607  
  228.   
  229. -----------------------------------------------------------------------------   
  230. | Id  | Operation        | Name     | Rows  | Bytes | Cost (%CPU)| Time     |  
  231. -----------------------------------------------------------------------------   
  232. |   0 | SELECT STATEMENT |          |   100 |  1300 |     1   (0)| 00:00:01 |  
  233. |*  1 |  INDEX RANGE SCAN| IND_T_ID |   100 |  1300 |     1   (0)| 00:00:01 |  
  234. -----------------------------------------------------------------------------   
  235.   
  236. Predicate Information (identified by operation id):  
  237. ---------------------------------------------------   
  238.   
  239.    1 - access("ID"=TO_NUMBER(:V_ID))  
  240.   
  241. Note  
  242. -----   
  243.    - dynamic sampling used for this statement  
  244.   
  245.   
  246. 统计信息  
  247. ----------------------------------------------------------   
  248.          13  recursive calls  
  249.           0  db block gets  
  250.          30  consistent gets  
  251.           4  physical reads  
  252.           0  redo size  
  253.         402  bytes sent via SQL*Net to client  
  254.         385  bytes received via SQL*Net from client  
  255.           2  SQL*Net roundtrips to/from client  
  256.           0  sorts (memory)  
  257.           0  sorts (disk)  
  258.           1  rows processed  
linux
相关资讯       Oracle SQL  Oracle 基础  Oracle SQL调优 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款