dbms_xplan之display_cursor函式的使用(R0.1)

thamsyangsw發表於2014-03-20
 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

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

相關文章