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函数语法
 
DBMS_XPLAN.DISPLAY_CURSOR(                        
 sql_id        IN  VARCHAR2  DEFAULT  NULL,       
 child_number  IN  NUMBER    DEFAULT  NULL,       
 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、当前数据库版本以及加载执行计划到库缓存               
 
SQL> select * from v$version where rownum<2;                       
                                                                   
BANNER                                                             
----------------------------------------------------------------   
Oracle Database 10g Release 10.2.0.3.0 - 64bit Production          
                                                                             
SQL> SELECT ename,dname,loc                                        
  2  FROM   emp e, dept d                                           
  3  WHERE  e.deptno = d.deptno                                    
  4  AND    e.empno  = 7788;                                       
                                                                   
ENAME      DNAME          LOC                                       
---------- -------------- -------------                            
SCOTT      RESEARCH       DALLAS                                   
        2、查看真实的执行计划             
 
/*----------------不传递任何参数给display_cursor函数,显示当前会话最后一条SQL语句的执行计划-------------*/             
/**************************************************/                                                                   
/* Author: Robinson Cheng                         */                                                                   
/* Blog:   https://blog.csdn.net/robinson_0612     */                                                                   
/* MSN:    robinson_0612@hotmail.com              */                                                                   
/* QQ:     645746311                              */                                                                   
/**************************************************/                                                                   
SQL> select * from table(dbms_xplan.display_cursor(null,null));                                                        
                                                                                                                        
PLAN_TABLE_OUTPUT                                                                                                      
------------------------------------------------------------------------------------------                             
SQL_ID  a67wqmkfb9j65, child number 0                                                                                  
-------------------------------------                                                                                   
SELECT ename,dname,loc FROM   emp e, dept d WHERE  e.deptno = d.deptno AND                                             
e.empno  = 7788                                                                                                         
                                                                                                                       
Plan hash value: 2385808155                                                                                             
                                                                                                                       
----------------------------------------------------------------------------------------                                
| Id  | Operation                    | Name    | Rows  | Bytes | Cost (%CPU)| Time     |                               
----------------------------------------------------------------------------------------                               
|   0 | SELECT STATEMENT             |         |       |       |     3 (100)|          |                               
|   1 |  NESTED LOOPS                |         |     1 |    63 |     3   (0)| 00:00:01 |                               
|   2 |   TABLE ACCESS BY INDEX ROWID| EMP     |     1 |    33 |     2   (0)| 00:00:01 |                               
|*  3 |    INDEX UNIQUE SCAN         | PK_EMP  |     1 |       |     1   (0)| 00:00:01 |                               
|   4 |   TABLE ACCESS BY INDEX ROWID| DEPT    |   409 | 12270 |     1   (0)| 00:00:01 |                               
|*  5 |    INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)|          |                               
----------------------------------------------------------------------------------------                               
                                                                                                                       
Predicate Information (identified by operation id):                                                                     
---------------------------------------------------                                                                    
                                                                                                                       
   3 - access("E"."EMPNO"=7788)                                                                                        
   5 - access("E"."DEPTNO"="D"."DEPTNO")                                                                               
                                                                                                                       
/*------------------- 获得SQL语句的SQL_ID,可以看出此SQL_ID与上面显示的执行计划中的SQL_ID一致----------*/             
SQL> select sql_id,address,plan_hash_value,hash_value,child_number from v$sql                                          
  2  where sql_text like '%SELECT ename%' and sql_text not like '%from v$sql%';                                        
                                                                                                                       
SQL_ID        ADDRESS          PLAN_HASH_VALUE HASH_VALUE CHILD_NUMBER                                                  
------------- ---------------- --------------- ---------- ------------                                                 
a67wqmkfb9j65 0000000091DBFBC8      2385808155 2629092549            0                                                  
                                                                                                                       
/*-------------- 传递SQL_ID以及format参数,并配合修饰符控制执行计划的输出------------------------*/                   
SQL> select * from table(dbms_xplan.display_cursor('a67wqmkfb9j65',null,'typical -predicate -rows'));                  
                                                                                                                       
PLAN_TABLE_OUTPUT                                                                                                       
------------------------------------------------------------------------------------                                   
SQL_ID  a67wqmkfb9j65, child number 0                                                                                   
-------------------------------------                                                                                  
SELECT ename,dname,loc FROM   emp e, dept d WHERE  e.deptno = d.deptno                                                 
AND    e.empno  = 7788                                                                                                 
                                                                                                                        
Plan hash value: 2385808155                                                                                            
                                                                                                                        
--------------------------------------------------------------------------------                                       
| Id  | Operation                    | Name    | Bytes | Cost (%CPU)| Time     |                                        
--------------------------------------------------------------------------------                                       
|   0 | SELECT STATEMENT             |         |       |     3 (100)|          |                                        
|   1 |  NESTED LOOPS                |         |    63 |     3   (0)| 00:00:01 |                                       
|   2 |   TABLE ACCESS BY INDEX ROWID| EMP     |    33 |     2   (0)| 00:00:01 |                                       
|   3 |    INDEX UNIQUE SCAN         | PK_EMP  |       |     1   (0)| 00:00:01 |                                       
|   4 |   TABLE ACCESS BY INDEX ROWID| DEPT    | 12270 |     1   (0)| 00:00:01 |                                       
|   5 |    INDEX UNIQUE SCAN         | PK_DEPT |       |     0   (0)|          |                                       
--------------------------------------------------------------------------------                                       
        3、查看真实执行计划并获得统计信息
                前提条件
                        设置参数statistics_level为all,可以基于session级别以及实例级别
                        或者启用gather_plan_statistics提示     
 
/*-------------查看实例参数statistics_level的值,并在会话级别将其设定为all  ---------*/                                 
SQL> show parameter statistics_le                                                                                       
                                                                                                                        
NAME                                 TYPE        VALUE                                                                  
------------------------------------ ----------- ------------------------------                                         
statistics_level                     string      ALL                                                                    
                                                                                                                        
SQL> alter session set statistics_level=all;                                                                            
                                                                                                                        
Session altered.                                                                                                         
                                                                                                                        
SQL> select e.ename,e.sal,s.grade                                                                                        
  2  from emp e                                                                                                         
  3  join salgrade s                                                                                                     
  4  on e.sal between losal and hisal                                                                                   
  5  and e.deptno = 20;                                                                                                  
                                                                                                                        
ENAME             SAL      GRADE                                                                                         
---------- ---------- ----------                                                                                        
SCOTT            3000          4                                                                                        
FORD             3000          4                                                                                        
JONES            2975          4                                                                                        
ADAMS            1100          1                                                                                        
SMITH             800          1                                                                                        
                                                                                                                        
/*------- 执行上述SQL语句后获得其真实的执行计划,使用了iostats last -predicate -note 修饰符控制显示输出-----*/         
SQL> set pagesize 0                                                                                                     
SQL> select * from table(dbms_xplan.display_cursor(null,null,'iostats last -predicate -note'));                         
SQL_ID  243b0tpjxj6wv, child number 0                                                                                   
-------------------------------------                                                                                    
select e.ename,e.sal,s.grade from emp e join salgrade s on e.sal between losal and                                      
hisal and e.deptno = 20                                                                                                  
                                                                                                                        
Plan hash value: 4204027666                                                                                              
                                                                                                                        
-------------------------------------------------------------------------------------------                             
| Id  | Operation            | Name     | Starts | E-Rows | A-Rows |   A-Time   | Buffers |                             
-------------------------------------------------------------------------------------------                             
|   1 |  MERGE JOIN          |          |      1 |      1 |      5 |00:00:00.01 |      14 |                             
|   2 |   SORT JOIN          |          |      1 |      5 |      5 |00:00:00.01 |       7 |                             
|   3 |    TABLE ACCESS FULL | EMP      |      1 |      5 |      5 |00:00:00.01 |       7 |                             
|   4 |   FILTER             |          |      5 |        |      5 |00:00:00.01 |       7 |                             
|   5 |    SORT JOIN         |          |      5 |      5 |     14 |00:00:00.01 |       7 |                             
|   6 |     TABLE ACCESS FULL| SALGRADE |      1 |      5 |      5 |00:00:00.01 |       7 |                             
-------------------------------------------------------------------------------------------                             
                                                                                                                        
/*---------------- 修改会话级别的参数statistics_level为typical并验证修改结果----------------*/                         
SQL> alter session set statistics_level=typical;                                                                        
                                                                                                                                
SQL> col name format a40                                                                                                
SQL> col value format a25                                                                                               
SQL> col display_value format a25                                                                                       
SQL> select name, value, display_value, isses_modifiable                                                                
  2  from v$parameter                                                                                                   
  3  where isses_modifiable = 'TRUE'                                                                                    
  4  and name like '%&input_name%';                                                                                     
Enter value for input_name: statistics_level                                                                            
old   4: and name like '%&input_name%'                                                                                  
new   4: and name like '%statistics_level%'                                                                              
                                                                                                                        
NAME                                     VALUE                     DISPLAY_VALUE             ISSES                      
---------------------------------------- ------------------------- ------------------------- -----                      
statistics_level                         TYPICAL                   TYPICAL                   TRUE                       
                                                                                                                        
/*-------- 使用提示gather_plan_statistics,并获得其真实执行计划,使用了allstats -rows修饰符控制显示输出---*/            
SQL> set pagesize 180                                                                                                   
SQL> SELECT /*+ gather_plan_statistics */ ename,dname,loc                                                               
  2  FROM   emp e, dept d                                                                                               
  3  WHERE  e.deptno = d.deptno                                                                                         
  4  AND    d.deptno=20 ORDER BY 1,2,3;                                                                                 
                                                                                                                        
ENAME      DNAME          LOC                                                                                            
---------- -------------- -------------                                                                                 
ADAMS      RESEARCH       DALLAS                                                                                         
FORD       RESEARCH       DALLAS                                                                                        
JONES      RESEARCH       DALLAS                                                                                         
SCOTT      RESEARCH       DALLAS                                                                                        
SMITH      RESEARCH       DALLAS                                                                                        
                                                                                                                        
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats -rows'));                                        
                                                                                                                        
PLAN_TABLE_OUTPUT                                                                                                        
-------------------------------------------------------------------------------------------------                       
SQL_ID  d2hh42yzqqjz7, child number 0                                                                                   
-------------------------------------                                                                                   
SELECT /*+ gather_plan_statistics */ ename,dname,loc FROM   emp e, dept d WHERE  e.deptno = d.deptno AND                
d.deptno=20 ORDER BY 1,2,3                                                                                              
                                                                                                                         
Plan hash value: 3339094711                                                                                             
                                                                                                                         
---------------------------------------------------------------------------------------------------------------------   
| Id  | Operation                     | Name    | Starts | A-Rows |   A-Time   | Buffers |  OMem |  1Mem |  O/1/M   |   
---------------------------------------------------------------------------------------------------------------------   
|   1 |  SORT ORDER BY                |         |      1 |      5 |00:00:00.01 |       9 |  2048 |  2048 |     1/0/0|   
|   2 |   NESTED LOOPS                |         |      1 |      5 |00:00:00.01 |       9 |       |       |          |   
|   3 |    TABLE ACCESS BY INDEX ROWID| DEPT    |      1 |      1 |00:00:00.01 |       2 |       |       |          |   
|*  4 |     INDEX UNIQUE SCAN         | PK_DEPT |      1 |      1 |00:00:00.01 |       1 |       |       |          |   
|*  5 |    TABLE ACCESS FULL          | EMP     |      1 |      5 |00:00:00.01 |       7 |       |       |          |    
---------------------------------------------------------------------------------------------------------------------   
                                                                                                                        
Predicate Information (identified by operation id):                                                                     
---------------------------------------------------                                                                     
                                                                                                                         
   4 - access("D"."DEPTNO"=20)                                                                                          
   5 - filter("E"."DEPTNO"=20)                                                                                           
                                                                                                                        
Note                                                                                                                    
-----                                                                                                                   
   - dynamic sampling used for this statement                                                                           
三、总结
        1、与display函数不同,display_cursor显示的为真实的执行计划
        2、对于format参数,使用与display函数的各个值,同样适用于display_cursor函数
        3、当statistics_level为all或使用gather_plan_statistics提示可以获得执行时的统计信息
        4、根据真实与预估的统计信息可以初步判断SQL效率低下的原因,如统计信息的准确性、主要的开销位于那些步骤等

查看SQL语句的真实执行计划的更多相关文章

  1. MySQL的EXPLAIN命令用于SQL语句的查询执行计划

    MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提供任何调整建议,但它能够提供重要的信息 ...

  2. 如何使用SQLPLUS分析SQL语句(查询执行计划跟踪)

    方法一:autotrace 1,  connect sys/密码 as sysdba,在sys用户下运行$ORACLE_HOME/sqlplus/admin/plustrce.sql这段sql的实际内 ...

  3. oracle中查看sql语句的执行计划

    1.在pl/sql中打开cmd命令容器 2.在cmd命令窗口中输入:explain plan for select * from t; 3.查看sql语句的执行计划:select * from tab ...

  4. MySQL查看SQL语句执行效率

    Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看 SQL 语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好 ...

  5. MySQL查看SQL语句执行效率(转)

    Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看 SQL 语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好 ...

  6. SQL Server查看Sql语句执行的耗时和IO消耗

    原文:SQL Server查看Sql语句执行的耗时和IO消耗 在做系统过程中,经常需要针对某些场景进行性能优化,那么如何判定性能优化的效果呢?肯定需要知道优化之前Sql语句的耗时和优化之后Sql语句的 ...

  7. SQL Server如何固定执行计划

    SQL Server 其实从SQL Server 2005开始,也提供了类似ORACLE中固定执行计划的功能,只是好像很少人使用这个功能.当然在SQL Server中不叫"固定执行计划&qu ...

  8. SQL性能分析之执行计划

    一直想找一些关于SQL语句性能调试的权威参考,但是有参考未必就能够做好调试的工作.我深信实践中得到的经验是最珍贵的,书本知识只是一个引导.本篇来源于<Inside Microsoft SQL S ...

  9. Oracle 查询真实执行计划

    什么是真实执行计划 获取Oracle的执行计划,有几种方式.(本文使用Oracle 11g XE版本,以及普通用户scott登录) explain plan for 有两个步骤: explain pl ...

随机推荐

  1. idea的maven项目下spring与mybatis整合

    两周前学习mybatis框架,参考了网上多位大神的博客,但因为各种原因(不解释)总是没法成功搭建环境并运行项目.周末花了点时间阅读了文档并整理之前琐碎的内容,解决掉之前遇到的问题.现将整合环境的关键步 ...

  2. c#利用ApplicationContext类 同时启动双窗体的实现

    Application类(位于System.Windows.Forms命名空间)公开了Run方法,可以调用该方法来调度应用程序进入消息循环.Run方法有三个重载 1.第一个重载版本不带任何参数,比较少 ...

  3. C语言strcmp()实现

    函数原型:    extern int strcmp(const char *s1,const char *s2); 比较两个字符串 设这两个字符串为str1,str2, 若str1=str2,则返回 ...

  4. 二进制 转换成十进制 BCD码(加3移位法)

    "原来的二进制数十几位,则左移时就要左移几位" "二进制数调整BCD码的方法是将二进制码左移8次,每次移位后都检查低四位LSD+3是否大于7,如是则加3,否则不加,高4位 ...

  5. 2013-7-30 802.1X企业级加密

    今天做了U9510的企业级加密标杆测试,写了企业级加密标杆设备的操作指南.最后做到server 2003却出了问题,peap能关联,但是TLS怎么都关联不上.用adb shell查看logcat日志, ...

  6. 关于SpringMVC

    SpringMVC 原理:1.用户发送请求给服务器.url:user.do2.服务器收到请求.发现DispatchServlet可以处理.于是调用DispatchServlet.3.DispatchS ...

  7. mysql识别中文

    在配置的INI中加上这些 [mysql]default-character-set=utf8no-auto-rehash# Remove the next comment character if y ...

  8. 杂谈2.cpp

    Ostream类定义的插入运算符(<<)使数据插入到输出流,istream定义的抽取运算符(>>)能够从输入流中抽取信息 Cin和cout都是智能对象,能够根据程序上下文将信息 ...

  9. jsonp原理及同源策略

    [个人学习笔记,如有问题还请前辈纠正] jsonp 是用来跨域读取数据的,为什么从不同的域访问数据要用jsop呢?这源于一个著名的安全策略--同源策略,即: 协议.端口号.域名相同 举例说明:http ...

  10. android 开发 View _8_ 动态图片自定义View

    转载地址:https://blog.csdn.net/mengks1987/article/details/77770922 先来看下效果: 是不是有一种熟悉感,其实这种效果使用序列帧动画也是可以实现 ...