future2012lg

博客园 首页 新随笔 联系 订阅 管理

 DBMS_XPLAN包中display_cursor函数不同于display函数,display_cursor用于显示SQL语句的真实的执行计划,在大多数情况下,显示真实
的执行计划有助于更好的分析SQL语句的全过程,尤其是运行此SQL语句实时的I/O开销。通过对比预估的I/O与真实的I/O开销来判断SQL语句所存
在问题,如缺少统计信息,SQL语句执行的次数,根据实际中间结果集的大小来选择合适的连接方式等。本文仅仅讲述display_cursor函数的使
用。

        有关执行计划中各字段模块的描述请参考: 执行计划中各字段各模块描述 
        有关由SQL语句来获取执行计划请参考:     使用 EXPLAIN PLAN 获取SQL语句执行计划 
        有关使用autotrace来获取执行计划请参考: 启用 AUTOTRACE 功能
        有关dbms_xplan之display函数请参考:     dbms_xplan之display函数的使用

一、display_cursor函数用法
1、display_cursor函数语法

  1. DBMS_XPLAN.DISPLAY_CURSOR(                         
  2.  sql_id        IN  VARCHAR2  DEFAULT  NULL,        
  3.  child_number  IN  NUMBER    DEFAULT  NULL,        
  4.  format        IN  VARCHAR2  DEFAULT  'TYPICAL');  

2、display_cursor函数参数描述
        sql_id
                指定位于库缓存执行计划中SQL语句的父游标。默认值为null。当使用默认值时当前会话的最后一条SQL语句的执行计划将被返回
                可以通过查询V$SQL 或 V$SQLAREA的SQL_ID列来获得SQL语句的SQL_ID。
        child_number
                指定父游标下子游标的序号。即指定被返回执行计划的SQL语句的子游标。默认值为0。如果为null,则sql_id所指父游标下所有子游标
                的执行计划都将被返回。
        format
                控制SQL语句执行计划的输出部分,即哪些可以显示哪些不显示。使用与display函数的format参数与修饰符在这里同样适用。
                除此之外当在开启statistics_level=all时或使用gather_plan_statistics提示可以获得执行计划中实时的统计信息
                有关详细的format格式描述请参考:dbms_xplan之display函数的使用 中format参数的描述

        下面给出启用统计信息时format新增的修饰符
                iostats   控制I/O统计的显示
                last      默认,显示所有执行计算过的统计。如果指定该值,则只显示最后一次执行的统计信息
                memstats  控制pga相关统计的显示
                allstats  此为iostats memstats的快捷方式,即allstats包含了iostats和memstats
                run_stats_last 等同于iostats last。只能用于oracle 10g R1
                run_stats_tot  等同于iostats。只能用于oracle 10g R1                  

二、演示使用display_cursor函数获取执行计划    
        1、当前数据库版本以及加载执行计划到库缓存                

  1. SQL> select * from v$version where rownum<2;                        
  2.                                                                     
  3. BANNER                                                              
  4. ----------------------------------------------------------------    
  5. Oracle Database 10g Release 10.2.0.3.0 - 64bit Production           
  6.                                                                               
  7. SQL> SELECT ename,dname,loc                                         
  8.   2  FROM   emp e, dept d                                           
  9.   3  WHERE  e.deptno = d.deptno                                     
  10.   4  AND    e.empno  = 7788;                                        
  11.                                                                     
  12. ENAME      DNAME          LOC                                       
  13. ---------- -------------- -------------                             
  14. SCOTT      RESEARCH       DALLAS                                    

        2、查看真实的执行计划              

  1. /*----------------不传递任何参数给display_cursor函数,显示当前会话最后一条SQL语句的执行计划-------------*/              
  2. /**************************************************/                                                                    
  3. /* Author: Robinson Cheng                         */                                                                    
  4. /* Blog:   http://blog.csdn.net/robinson_0612     */                                                                    
  5. /* MSN:    robinson_0612@hotmail.com              */                                                                    
  6. /* QQ:     645746311                              */                                                                    
  7. /**************************************************/                                                                    
  8. SQL> select * from table(dbms_xplan.display_cursor(null,null));                                                         
  9.                                                                                                                         
  10. PLAN_TABLE_OUTPUT                                                                                                       
  11. ------------------------------------------------------------------------------------------                              
  12. SQL_ID  a67wqmkfb9j65, child number 0                                                                                   
  13. -------------------------------------                                                                                   
  14. SELECT ename,dname,loc FROM   emp e, dept d WHERE  e.deptno = d.deptno AND                                              
  15. e.empno  = 7788                                                                                                         
  16.                                                                                                                         
  17. Plan hash value: 2385808155                                                                                             
  18.                                                                                                                         
  19. ----------------------------------------------------------------------------------------                                
  20. | Id  | Operation                    | Name    | Rows  | Bytes | Cost (%CPU)| Time     |                                
  21. ----------------------------------------------------------------------------------------                                
  22. |   0 | SELECT STATEMENT             |         |       |       |     3 (100)|          |                                
  23. |   1 |  NESTED LOOPS                |         |     1 |    63 |     3   (0)| 00:00:01 |                                
  24. |   2 |   TABLE ACCESS BY INDEX ROWID| EMP     |     1 |    33 |     2   (0)| 00:00:01 |                                
  25. |*  3 |    INDEX UNIQUE SCAN         | PK_EMP  |     1 |       |     1   (0)| 00:00:01 |                                
  26. |   4 |   TABLE ACCESS BY INDEX ROWID| DEPT    |   409 | 12270 |     1   (0)| 00:00:01 |                                
  27. |*  5 |    INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)|          |                                
  28. ----------------------------------------------------------------------------------------                                
  29.                                                                                                                         
  30. Predicate Information (identified by operation id):                                                                     
  31. ---------------------------------------------------                                                                     
  32.                                                                                                                         
  33.    3 - access("E"."EMPNO"=7788)                                                                                         
  34.    5 - access("E"."DEPTNO"="D"."DEPTNO")                                                                                
  35.                                                                                                                         
  36. /*------------------- 获得SQL语句的SQL_ID,可以看出此SQL_ID与上面显示的执行计划中的SQL_ID一致 ----------*/              
  37. SQL> select sql_id,address,plan_hash_value,hash_value,child_number from v$sql                                           
  38.   2  where sql_text like '%SELECT ename%' and sql_text not like '%from v$sql%';                                         
  39.                                                                                                                         
  40. SQL_ID        ADDRESS          PLAN_HASH_VALUE HASH_VALUE CHILD_NUMBER                                                  
  41. ------------- ---------------- --------------- ---------- ------------                                                  
  42. a67wqmkfb9j65 0000000091DBFBC8      2385808155 2629092549            0                                                  
  43.                                                                                                                         
  44. /*-------------- 传递SQL_ID以及format参数,并配合修饰符控制执行计划的输出 ------------------------*/                    
  45. SQL> select * from table(dbms_xplan.display_cursor('a67wqmkfb9j65',null,'typical -predicate -rows'));                   
  46.                                                                                                                         
  47. PLAN_TABLE_OUTPUT                                                                                                       
  48. ------------------------------------------------------------------------------------                                    
  49. SQL_ID  a67wqmkfb9j65, child number 0                                                                                   
  50. -------------------------------------                                                                                   
  51. SELECT ename,dname,loc FROM   emp e, dept d WHERE  e.deptno = d.deptno                                                  
  52. AND    e.empno  = 7788                                                                                                  
  53.                                                                                                                         
  54. Plan hash value: 2385808155                                                                                             
  55.                                                                                                                         
  56. --------------------------------------------------------------------------------                                        
  57. | Id  | Operation                    | Name    | Bytes | Cost (%CPU)| Time     |                                        
  58. --------------------------------------------------------------------------------                                        
  59. |   0 | SELECT STATEMENT             |         |       |     3 (100)|          |                                        
  60. |   1 |  NESTED LOOPS                |         |    63 |     3   (0)| 00:00:01 |                                        
  61. |   2 |   TABLE ACCESS BY INDEX ROWID| EMP     |    33 |     2   (0)| 00:00:01 |                                        
  62. |   3 |    INDEX UNIQUE SCAN         | PK_EMP  |       |     1   (0)| 00:00:01 |                                        
  63. |   4 |   TABLE ACCESS BY INDEX ROWID| DEPT    | 12270 |     1   (0)| 00:00:01 |                                        
  64. |   5 |    INDEX UNIQUE SCAN         | PK_DEPT |       |     0   (0)|          |                                        
  65. --------------------------------------------------------------------------------                                        

        3、查看真实执行计划并获得统计信息
                前提条件
                        设置参数statistics_level为all,可以基于session级别以及实例级别
                        或者启用gather_plan_statistics提示      

  1. /*-------------查看实例参数statistics_level的值,并在会话级别将其设定为all  ---------*/                                  
  2. SQL> show parameter statistics_le                                                                                        
  3.                                                                                                                          
  4. NAME                                 TYPE        VALUE                                                                   
  5. ------------------------------------ ----------- ------------------------------                                          
  6. statistics_level                     string      ALL                                                                     
  7.                                                                                                                          
  8. SQL> alter session set statistics_level=all;                                                                             
  9.                                                                                                                          
  10. Session altered.                                                                                                         
  11.                                                                                                                          
  12. SQL> select e.ename,e.sal,s.grade                                                                                        
  13.   2  from emp e                                                                                                          
  14.   3  join salgrade s                                                                                                     
  15.   4  on e.sal between losal and hisal                                                                                    
  16.   5  and e.deptno = 20;                                                                                                  
  17.                                                                                                                          
  18. ENAME             SAL      GRADE                                                                                         
  19. ---------- ---------- ----------                                                                                         
  20. SCOTT            3000          4                                                                                         
  21. FORD             3000          4                                                                                         
  22. JONES            2975          4                                                                                         
  23. ADAMS            1100          1                                                                                         
  24. SMITH             800          1                                                                                         
  25.                                                                                                                          
  26. /*------- 执行上述SQL语句后获得其真实的执行计划,使用了iostats last -predicate -note 修饰符控制显示输出 -----*/          
  27. SQL> set pagesize 0                                                                                                      
  28. SQL> select * from table(dbms_xplan.display_cursor(null,null,'iostats last -predicate -note'));                          
  29. SQL_ID  243b0tpjxj6wv, child number 0                                                                                    
  30. -------------------------------------                                                                                    
  31. select e.ename,e.sal,s.grade from emp e join salgrade s on e.sal between losal and                                       
  32. hisal and e.deptno = 20                                                                                                  
  33.                                                                                                                          
  34. Plan hash value: 4204027666                                                                                              
  35.                                                                                                                          
  36. -------------------------------------------------------------------------------------------                              
  37. | Id  | Operation            | Name     | Starts | E-Rows | A-Rows |   A-Time   | Buffers |                              
  38. -------------------------------------------------------------------------------------------                              
  39. |   1 |  MERGE JOIN          |          |      1 |      1 |      5 |00:00:00.01 |      14 |                              
  40. |   2 |   SORT JOIN          |          |      1 |      5 |      5 |00:00:00.01 |       7 |                              
  41. |   3 |    TABLE ACCESS FULL | EMP      |      1 |      5 |      5 |00:00:00.01 |       7 |                              
  42. |   4 |   FILTER             |          |      5 |        |      5 |00:00:00.01 |       7 |                              
  43. |   5 |    SORT JOIN         |          |      5 |      5 |     14 |00:00:00.01 |       7 |                              
  44. |   6 |     TABLE ACCESS FULL| SALGRADE |      1 |      5 |      5 |00:00:00.01 |       7 |                              
  45. -------------------------------------------------------------------------------------------                              
  46.                                                                                                                          
  47. /*---------------- 修改会话级别的参数statistics_level为typical并验证修改结果 ----------------*/                          
  48. SQL> alter session set statistics_level=typical;                                                                         
  49.                                                                                                                                  
  50. SQL> col name format a40                                                                                                 
  51. SQL> col value format a25                                                                                                
  52. SQL> col display_value format a25                                                                                        
  53. SQL> select name, value, display_value, isses_modifiable                                                                 
  54.   2  from v$parameter                                                                                                    
  55.   3  where isses_modifiable = 'TRUE'                                                                                     
  56.   4  and name like '%&input_name%';                                                                                      
  57. Enter value for input_name: statistics_level                                                                             
  58. old   4: and name like '%&input_name%'                                                                                   
  59. new   4: and name like '%statistics_level%'                                                                              
  60.                                                                                                                          
  61. NAME                                     VALUE                     DISPLAY_VALUE             ISSES                       
  62. ---------------------------------------- ------------------------- ------------------------- -----                       
  63. statistics_level                         TYPICAL                   TYPICAL                   TRUE                        
  64.                                                                                                                          
  65. /*-------- 使用提示gather_plan_statistics,并获得其真实执行计划,使用了allstats -rows修饰符控制显示输出 ---*/            
  66. SQL> set pagesize 180                                                                                                    
  67. SQL> SELECT /*+ gather_plan_statistics */ ename,dname,loc                                                                
  68.   2  FROM   emp e, dept d                                                                                                
  69.   3  WHERE  e.deptno = d.deptno                                                                                          
  70.   4  AND    d.deptno=20 ORDER BY 1,2,3;                                                                                  
  71.                                                                                                                          
  72. ENAME      DNAME          LOC                                                                                            
  73. ---------- -------------- -------------                                                                                  
  74. ADAMS      RESEARCH       DALLAS                                                                                         
  75. FORD       RESEARCH       DALLAS                                                                                         
  76. JONES      RESEARCH       DALLAS                                                                                         
  77. SCOTT      RESEARCH       DALLAS                                                                                         
  78. SMITH      RESEARCH       DALLAS                                                                                         
  79.                                                                                                                          
  80. SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats -rows'));                                         
  81.                                                                                                                          
  82. PLAN_TABLE_OUTPUT                                                                                                        
  83. -------------------------------------------------------------------------------------------------                        
  84. SQL_ID  d2hh42yzqqjz7, child number 0                                                                                    
  85. -------------------------------------                                                                                    
  86. SELECT /*+ gather_plan_statistics */ ename,dname,loc FROM   emp e, dept d WHERE  e.deptno = d.deptno AND                 
  87. d.deptno=20 ORDER BY 1,2,3                                                                                               
  88.                                                                                                                          
  89. Plan hash value: 3339094711                                                                                              
  90.                                                                                                                          
  91. ---------------------------------------------------------------------------------------------------------------------    
  92. | Id  | Operation                     | Name    | Starts | A-Rows |   A-Time   | Buffers |  OMem |  1Mem |  O/1/M   |    
  93. ---------------------------------------------------------------------------------------------------------------------    
  94. |   1 |  SORT ORDER BY                |         |      1 |      5 |00:00:00.01 |       9 |  2048 |  2048 |     1/0/0|    
  95. |   2 |   NESTED LOOPS                |         |      1 |      5 |00:00:00.01 |       9 |       |       |          |    
  96. |   3 |    TABLE ACCESS BY INDEX ROWID| DEPT    |      1 |      1 |00:00:00.01 |       2 |       |       |          |    
  97. |*  4 |     INDEX UNIQUE SCAN         | PK_DEPT |      1 |      1 |00:00:00.01 |       1 |       |       |          |    
  98. |*  5 |    TABLE ACCESS FULL          | EMP     |      1 |      5 |00:00:00.01 |       7 |       |       |          |    
  99. ---------------------------------------------------------------------------------------------------------------------    
  100.                                                                                                                          
  101. Predicate Information (identified by operation id):                                                                      
  102. ---------------------------------------------------                                                                      
  103.                                                                                                                          
  104.    4 - access("D"."DEPTNO"=20)                                                                                           
  105.    5 - filter("E"."DEPTNO"=20)                                                                                           
  106.                                                                                                                          
  107. Note                                                                                                                     
  108. -----                                                                                                                    
  109.    - dynamic sampling used for this statement                                                                            

三、总结
        1、与display函数不同,display_cursor显示的为真实的执行计划
        2、对于format参数,使用与display函数的各个值,同样适用于display_cursor函数
        3、当statistics_level为all或使用gather_plan_statistics提示可以获得执行时的统计信息
        4、根据真实与预估的统计信息可以初步判断SQL效率低下的原因,如统计信息的准确性、主要的开销位于那些步骤等  

转载:http://blog.csdn.net/leshami/article/details/6866925

posted on 2014-12-09 10:09  future2012lg  阅读(589)  评论(0编辑  收藏  举报