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

浅谈Oracle执行计划

[日期:2012-06-08] 来源:Linux社区  作者:ylw6006 [字体:大 中 小]

3:使用dbms_stats包对表进行收集统计信息,在本例中可以看出进行分析过的表,将不会继续使用动态采样的方式收集信息

  1. SQL> exec dbms_stats.gather_table_stats('HR','T',CASCADE=>TRUE);  
  2. PL/SQL procedure successfully completed.  
  3.  
  4. SQL> select * from t where id=99;  
  5.  
  6. Execution Plan  
  7. ----------------------------------------------------------  
  8. Plan hash value: 4153437776  
  9. --------------------------------------------------------------------------------------  
  10.  
  11. | Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time|  
  12.  
  13. --------------------------------------------------------------------------------------  
  14.  
  15. |   0 | SELECT STATEMENT            |        |    14 |   378 |     2   (0)| 00:00:01 |  
  16.  
  17. |   1 |  TABLE ACCESS BY INDEX ROWID| T      |    14 |   378 |     2   (0)| 00:00:01 |  
  18.  
  19. |*  2 |   INDEX RANGE SCAN          | I_T_ID |    14 |       |     1   (0)| 00:00:01 |  
  20.  
  21. --------------------------------------------------------------------------------------  
  22. Predicate Information (identified by operation id):  
  23. ---------------------------------------------------  
  24.    2 - access("ID"=99)  
  25.  
  26. Statistics  
  27. ----------------------------------------------------------  
  28.           0  recursive calls  
  29.           0  db block gets  
  30.           3  consistent gets  
  31.           0  physical reads  
  32.           0  redo size  
  33.         491  bytes sent via SQL*Net to client  
  34.         420  bytes received via SQL*Net from client  
  35.           2  SQL*Net roundtrips to/from client  
  36.           0  sorts (memory)  
  37.           0  sorts (disk)  
  38.           1  rows processed 

4:对表分析后的表进行update操作,在未重新进行分析前,将产生错误的执行计划,重新分析后正常;因而在生产环境中,经常有DML操作的表应当根据实际情况进行分析,否则将产生类似的问题

  1. SQL> set autot off  
  2. SQL> update t set id=99 where id=1;  
  3. 77261 rows updated.  
  4.  
  5. SQL> update t set id=1 where rownum=1;  
  6. 1 row updated.  
  7.  
  8. SQL> commit;  
  9. Commit complete.  
  10.  
  11. SQL> select id,count(*) from t group by id;  
  12.  
  13.         ID   COUNT(*)  
  14. ---------- ----------  
  15.          1          1  
  16.         99      77261  
  17.  
  18. SQL> set autot traceonly  
  19. SQL> select * from t where id=99;  
  20. 77261 rows selected.  
  21.  
  22. Execution Plan  
  23. ----------------------------------------------------------  
  24. Plan hash value: 4153437776  
  25. --------------------------------------------------------------------------------------  
  26.  
  27. | Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time|  
  28.  
  29. --------------------------------------------------------------------------------------  
  30.  
  31. |   0 | SELECT STATEMENT            |        |    14 |   378 |     2   (0)| 00:00:01 |  
  32.  
  33. |   1 |  TABLE ACCESS BY INDEX ROWID| T      |    14 |   378 |     2   (0)| 00:00:01 |  
  34.  
  35. |*  2 |   INDEX RANGE SCAN          | I_T_ID |    14 |       |     1   (0)| 00:00:01 |  
  36.  
  37. --------------------------------------------------------------------------------------  
  38. Predicate Information (identified by operation id):  
  39. ---------------------------------------------------  
  40.    2 - access("ID"=99)  
  41.  
  42. Statistics  
  43. ----------------------------------------------------------  
  44.           0  recursive calls  
  45.           0  db block gets  
  46.       11017  consistent gets  
  47.           0  physical reads  
  48.       13840  redo size  
  49.     3170997  bytes sent via SQL*Net to client  
  50.       57070  bytes received via SQL*Net from client  
  51.        5152  SQL*Net roundtrips to/from client  
  52.           0  sorts (memory)  
  53.           0  sorts (disk)  
  54.       77261  rows processed  
  55.  
  56.  
  57. SQL> select /*+ full(t) */ *  from t where id=99;  
  58. 77261 rows selected.  
  59.  
  60. Execution Plan  
  61. ----------------------------------------------------------  
  62. Plan hash value: 1601196873  
  63. --------------------------------------------------------------------------  
  64. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  65. --------------------------------------------------------------------------  
  66. |   0 | SELECT STATEMENT  |      |    14 |   378 |   101   (1)| 00:00:02 |  
  67. |*  1 |  TABLE ACCESS FULL| T    |    14 |   378 |   101   (1)| 00:00:02 |  
  68. --------------------------------------------------------------------------  
  69. Predicate Information (identified by operation id):  
  70. ---------------------------------------------------  
  71.    1 - filter("ID"=99)  
  72.  
  73. Statistics  
  74. ----------------------------------------------------------  
  75.           1  recursive calls  
  76.           0  db block gets  
  77.        5477  consistent gets  
  78.           0  physical reads  
  79.           0  redo size  
  80.     2357600  bytes sent via SQL*Net to client  
  81.       57070  bytes received via SQL*Net from client  
  82.        5152  SQL*Net roundtrips to/from client  
  83.           0  sorts (memory)  
  84.           0  sorts (disk)  
  85.       77261  rows processed  
  86.  
  87. SQL> exec dbms_stats.gather_table_stats('HEROSTART_CN','T',CASCADE=>TRUE);  
  88. PL/SQL procedure successfully completed.  
  89.  
  90. SQL> select * from t where id=99;  
  91. 77261 rows selected.  
  92.  
  93. Execution Plan  
  94. ----------------------------------------------------------  
  95. Plan hash value: 1601196873  
  96. --------------------------------------------------------------------------  
  97. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  98. --------------------------------------------------------------------------  
  99. |   0 | SELECT STATEMENT  |      | 77241 |  2036K|   101   (1)| 00:00:02 |  
  100. |*  1 |  TABLE ACCESS FULL| T    | 77241 |  2036K|   101   (1)| 00:00:02 |  
  101. --------------------------------------------------------------------------  
  102. Predicate Information (identified by operation id):  
  103. ---------------------------------------------------  
  104.    1 - filter("ID"=99)  
  105.  
  106. Statistics  
  107. ----------------------------------------------------------  
  108.           0  recursive calls  
  109.           0  db block gets  
  110.       10845  consistent gets  
  111.           0  physical reads  
  112.           0  redo size  
  113.     3170997  bytes sent via SQL*Net to client  
  114.       57070  bytes received via SQL*Net from client  
  115.        5152  SQL*Net roundtrips to/from client  
  116.           0  sorts (memory)  
  117.           0  sorts (disk)  
  118.       77261  rows processed 
linux
相关资讯       Oracle存储过程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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