ITPUB??ì3
订阅ITPUB精粹播报,社区精彩内容不错过
ITPUB论坛 » Oracle专题深入讨论 » 使用dbms_xplan工具查看执行计划


您有 1 条公共消息
  • 来自: 公共消息 标题: ITPUB国庆假期数 ... 内容: 全新编排的Oracle数据库课程,第一线数据库工程师传授亲身经验,完全摆 ...

    标题: [Tips] 使用dbms_xplan工具查看执行计划
    离线 西门吹牛
    高级会员


    精华贴数 4
    个人空间 0
    技术积分 12801 (125)
    社区积分 2675 (665)
    注册日期 2002-4-29
    论坛徽章:3
    会员2007贡献徽章会员2006贡献徽章授权会员   
          

    发表于 2004-12-23 15:05 
    使用dbms_xplan工具查看执行计划

    使用dbms_xplan工具查看执行计划

    9i有一个新的包 dbms_xplan,对查询plan_table表是一个很有用的工具,相对于以前写一个复杂的SQL语句,然后从plan_table看执行计划,不如调用 dbms_xplan 包,还可以显示格式,这个工具的使用也非常方便。
    调用的语法类似
    select * from table(dbms_xplan.display(format=>'BASIC'))
    使用 TABLE() 操作符,或者 CAST 操作。

    DISPLAY 函数有三个参数
    TABLE_NAME        指出优化计划放在哪个表里面,默认是 PLAN_TABLE.
    STATEMENT_ID      指的是plan table中的statement_id字段,默认是last ID 或者 NULL.
    FORMAT            指的是显示的格式


    FORMAT参数有三个可选值,原文如下
    BASIC           It provides only the minimum amount of information, as in
                    case of the example above, similar to a query from
                    PLAN_TABLE directly.
    TYPICAL           This is the default value. It provides a variety of the
                    information useful for understanding how the optimizer
                    works for this statement. For instance, in case of partitioned
                    table operation, the columns PARTITION_START,
                    PARTITION_STOP, PARTITION_ID, and
                    FILTER_PREDICATES are displayed in addition to COST
                    for that step, the number of rows expected to be retrieved,
                    and number of bytes those rows may have. This provides
                    the information to understand statements involving
                    partitioned objects.
    ALL                This setting displays all the information displayed for the
                    BASIC and TYPICAL values, and also displays parallel
                    query operations and the related SQL statements, if those
                    are involved.   
    SERIAL             This setting gets results similar to those retrieved by the
                    TYPICAL setting, but the queries are explained serially even
                    if a parallel query will be used.

    一般推荐使用typical 参数,把SQLPLUS的linesize 参数调整到至少 120

    下面是测试步骤

    用sys用户建立
    PLUSTRACE 角色
    $ORACLE_HOME\E:\oracle\ora92\sqlplus\admin\Plustrce.sql

    二:把权限授予某个人
    grant plustrace to mjs;

    三:建立表
    建表SQL脚本为在${ORACLE_HOME}/rdbms/admin/下的utlxplan.sql。


    四:使用说明
    1:我们用一个大表来举例说明如何使用

    dw_for_bo@MJS.SENSKY.COM> select count(*) from tbl_fact_sublog;

      COUNT(*)
    ----------
       1757960
       
    2:一个很平常的SQL语句
    用常规方法如下分析执行计划
    analyze table tbl_fact_sublog compute statistics;

    set autotrace traceonly

    select id,handsetname,count(*) from tbl_fact_sublog group by id,handsetname ;
    结果如下:

    1757960 rows selected.


    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=30264 Card=1757960 B
              ytes=24611440)

       1    0   SORT (GROUP BY) (Cost=30264 Card=1757960 Bytes=24611440)
       2    1     TABLE ACCESS (FULL) OF 'TBL_FACT_SUBLOG' (Cost=2284 Card
              =1757960 Bytes=24611440)





    Statistics
    ----------------------------------------------------------
              0  recursive calls
             88  db block gets
          23749  consistent gets
          35593  physical reads
              0  redo size
       47677309  bytes sent via SQL*Net to client
        1289670  bytes received via SQL*Net from client
         117199  SQL*Net roundtrips to/from client
              0  sorts (memory)
              1  sorts (disk)
        1757960  rows processed





    用 dbms_xplan 方法分析

    delete from plan_table;

    explain plan for select id,handsetname,count(*) from tbl_fact_sublog group by id,handsetname ;


    dw_for_bo@MJS.SENSKY.COM> select * from table( dbms_xplan.display );

    PLAN_TABLE_OUTPUT
    -------------------------------------------------------------------------------------

    ---------------------------------------------------------------------------------
    | Id  | Operation            |  Name            | Rows  | Bytes |TempSpc| Cost  |
    ---------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT     |                  |  1757K|    23M|       | 30264 |
    |   1 |  SORT GROUP BY       |                  |  1757K|    23M|    80M| 30264 |
    |   2 |   TABLE ACCESS FULL  | TBL_FACT_SUBLOG  |  1757K|    23M|       |  2284 |
    ---------------------------------------------------------------------------------

    Note: cpu costing is off

    10 rows selected.

    可见,用dbms_xplan这个包可以发现排序的时候需要大概 80M的临时空间



    dw_for_bo@MJS.SENSKY.COM> select * from table( dbms_xplan.display('PLAN_TABLE',null,'BASIC'));

    PLAN_TABLE_OUTPUT
    -----------------------------------------------------

    -------------------------------------------------
    | Id  | Operation            |  Name            |
    -------------------------------------------------
    |   0 | SELECT STATEMENT     |                  |
    |   1 |  SORT GROUP BY       |                  |
    |   2 |   TABLE ACCESS FULL  | TBL_FACT_SUBLOG  |
    -------------------------------------------------

    8 rows selected.

    用BASIC参数得到的信息就少多了。



    另:一个有趣的现象,如果我删除统计信息,结果是什么样的?
    analyze table tbl_fact_sublog delete statistics;

    dw_for_bo@MJS.SENSKY.COM> set autotrace traceonly
    dw_for_bo@MJS.SENSKY.COM> select id,handsetname,count(*) from tbl_fact_sublog group by id,handsetname ;

    1757960 rows selected.


    Execution Plan
    ---------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   SORT (GROUP BY)
       2    1     TABLE ACCESS (FULL) OF 'TBL_FACT_SUBLOG'




    Statistics
    ---------------------------------------------------------
              0  recursive calls
             88  db block gets
          23749  consistent gets
          35641  physical reads
              0  redo size
       47677309  bytes sent via SQL*Net to client
        1289670  bytes received via SQL*Net from client
         117199  SQL*Net roundtrips to/from client
              0  sorts (memory)
              1  sorts (disk)
        1757960  rows processed





    用 dbms_xplan 方法分析

    delete from plan_table;

    explain plan for select id,handsetname,count(*) from tbl_fact_sublog group by id,handsetname ;


    dw_for_bo@MJS.SENSKY.COM> select * from table( dbms_xplan.display );

    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------

    -------------------------------------------------------------------------
    | Id  | Operation            |  Name            | Rows  | Bytes | Cost  |
    -------------------------------------------------------------------------
    |   0 | SELECT STATEMENT     |                  |       |       |       |
    |   1 |  SORT GROUP BY       |                  |       |       |       |
    |   2 |   TABLE ACCESS FULL  | TBL_FACT_SUBLOG  |       |       |       |
    -------------------------------------------------------------------------

    Note: rule based optimization

    10 rows selected.

    从提示看出,用的是基于规则的优化器,而且没有显示排序大概需要多少空间,看来还是经过分析后用CBO比较好


    参考文章:otn相关例子
                       Donald K. Burleson等人所著的 《Oracle Space Management Handbook》


    __________________
    春莺啼岸柳弄春晴,柳弄春晴夜月明。明月夜晴春弄柳,晴春弄柳岸啼莺。夏香莲碧水动风凉,水动风凉夏日长。长日夏凉风动水,凉风动水碧莲香。秋秋江楚雁宿沙洲,雁宿沙洲浅水流。流水浅洲沙宿雁,洲沙宿雁楚江秋。冬红炉透炭炙寒风,炭炙寒风御隆冬。冬隆御风寒炙炭,风寒炙炭透炉红。
    只看该作者    顶部
    离线 biti_rainy
    人生就是如此



    精华贴数 39
    个人空间 0
    技术积分 112866 (4)
    社区积分 12143 (174)
    注册日期 2001-12-12
    论坛徽章:56
    现任管理团队成员ITPUB元老年度论坛发贴之星年度论坛发贴之星2010数据库技术大会纪念徽章ITPUB北京2009年会纪念徽章
    ITPUB北京九华山庄2008年会纪念徽章管理团队2007贡献徽章参与2007年甲骨文全球大会(中国上海)纪念ITPUB北京香山2007年会纪念徽章管理团队2006纪念徽章会员2007贡献徽章

    发表于 2004-12-23 16:55 
    这是经常做的一件事情了,只做分析

    set  pagesize 0
    set  linesize 150
    SQL> explain  plan  for  select *
      2  from
      3   nirvana.sfa_repeat_log  where account_id = :1   order by  gmt_created desc
      4  ;

    Explained.

    SQL> @?/rdbms/admin/utlxplp;

    ----------------------------------------------------------------------------------------
    | Id  | Operation                    |  Name                   | Rows  | Bytes | Cost  |
    ----------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT             |                         |     2 |   200 |    15 |
    |   1 |  SORT ORDER BY               |                         |     2 |   200 |    15 |
    |   2 |   TABLE ACCESS BY INDEX ROWID| SFA_REPEAT_LOG          |     2 |   200 |     6 |
    |*  3 |    INDEX RANGE SCAN          | SFA_REPEAT_LOG_AID_IND  |     2 |       |     3 |
    ----------------------------------------------------------------------------------------

    Predicate Information (identified by operation id):
    ---------------------------------------------------

       3 - access("SFA_REPEAT_LOG"."ACCOUNT_ID"=TO_NUMBER(:Z))

    Note: cpu costing is off

    16 rows selected.


    [oracle@ocn1 admin]$ pwd
    /opt/oracle/products/9.2.0/rdbms/admin
    [oracle@ocn1 admin]$ more  utlxplp.sql
    Rem
    Rem $Header: utlxplp.sql 23-jan-2002.08:55:23 bdagevil Exp $
    Rem
    Rem utlxplp.sql
    Rem
    Rem Copyright (c) 1998, 2002, Oracle Corporation.  All rights reserved.  
    Rem
    Rem    NAME
    Rem      utlxplp.sql - UTiLity eXPLain Parallel plans
    Rem
    Rem    DESCRIPTION
    Rem      script utility to display the explain plan of the last explain plan
    Rem      command. Display also Parallel Query information if the plan happens to
    Rem      run parallel
    Rem
    Rem    NOTES
    Rem      Assume that the table PLAN_TABLE has been created. The script
    Rem      utlxplan.sql should be used to create that table
    Rem
    Rem      With SQL*plus, it is recomended to set linesize and pagesize before
    Rem      running this script. For example:
    Rem         set linesize 130
    Rem         set pagesize 0
    Rem
    Rem    MODIFIED   (MM/DD/YY)
    Rem    bdagevil    01/23/02 - rewrite with new dbms_xplan package
    Rem    bdagevil    04/05/01 - include CPU cost
    Rem    bdagevil    02/27/01 - increase Name column
    Rem    jihuang     06/14/00 - change order by to order siblings by.
    Rem    jihuang     05/10/00 - include plan info for recursive SQL in LE row source
    Rem    bdagevil    01/05/00 - make deterministic with order-by
    Rem    bdagevil    05/07/98 - Explain plan script for parallel plans           
    Rem    bdagevil    05/07/98 - Created
    Rem

    set markup html preformat on

    Rem
    Rem Use the display table function from the dbms_xplan package to display the last
    Rem explain plan. Use default mode which will display only relevant information
    Rem
    select * from table(dbms_xplan.display());
    [oracle@ocn1 admin]$


    __________________
    只看该作者    顶部
    离线 l2g32003
    LiGuang


    精华贴数 2
    个人空间 0
    技术积分 1176 (1948)
    社区积分 599 (1681)
    注册日期 2003-4-25
    论坛徽章:2
    会员2006贡献徽章授权会员    
          

    发表于 2004-12-23 20:13 
    要是楼主能把10G中的也整合进来就好了


    __________________
    jesse livermore巴鲁克杰伊.古尔德-----------------------------------------授权会员 的徽章怎么像饭盒
    只看该作者    顶部
    离线 xzh2000
    仙人抚我须 结发授长生



    精华贴数 13
    个人空间 0
    技术积分 47346 (23)
    社区积分 5411 (393)
    注册日期 2002-7-17
    论坛徽章:37
    现任管理团队成员ITPUB元老九尾狐狸玉石琵琶紫蜘蛛蓝色妖姬
    八级虎吧徽章红孩儿铁扇公主玉兔嫦娥授权会员

    发表于 2004-12-24 10:57 
    explain plan for ....
    select * from table(dbms_xplan.display())
    在调试bind var的SQL是特合适的选择.从来没有注意到还可以显示排序需要的内存。


    __________________
    过目即忘  插柳成荫
    只看该作者    顶部
    离线 foreverlee
    SQLPLUS



    精华贴数 0
    个人空间 0
    技术积分 1768 (1233)
    社区积分 72 (5306)
    注册日期 2004-4-6
    论坛徽章:2
    授权会员生肖徽章2007版:猴    
          

    发表于 2004-12-24 16:17 
    SQL> explain plan for
      2  select * from tb1;

    已解释。

    SQL> @E:\oracle\ora92\rdbms\admin\utlxplp;

    PLAN_TABLE_OUTPUT
    -------------------------------------------------------------------------------


    --------------------------------------------------------------------
    | Id  | Operation            |  Name       | Rows  | Bytes | Cost  |
    --------------------------------------------------------------------
    |   0 | SELECT STATEMENT     |             |       |       |       |
    |   1 |  TABLE ACCESS FULL   | TB1         |       |       |       |
    --------------------------------------------------------------------

    Note: rule based optimization

    已选择9行。


    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'




    Statistics
    ----------------------------------------------------------
           2170  recursive calls
             23  db block gets
            678  consistent gets
             37  physical reads
           4308  redo size
            897  bytes sent via SQL*Net to client
            503  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
             63  sorts (memory)
              0  sorts (disk)
              9  rows processed

    SQL> analyze table tb1 compute statistics;

    表已分析。
    SQL> explain plan for
      2  select * from tb1;

    已解释。

    SQL> @E:\oracle\ora92\rdbms\admin\utlxplp;

    PLAN_TABLE_OUTPUT
    -------------------------------------------------------------------------------


    --------------------------------------------------------------------
    | Id  | Operation            |  Name       | Rows  | Bytes | Cost  |
    --------------------------------------------------------------------
    |   0 | SELECT STATEMENT     |             |  1000 | 11000 |     2 |
    |   1 |  TABLE ACCESS FULL   | TB1         |  1000 | 11000 |     2 |
    --------------------------------------------------------------------

    Note: cpu costing is off

    已选择9行。


    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'




    Statistics
    ----------------------------------------------------------
             11  recursive calls
              0  db block gets
              6  consistent gets
              0  physical reads
              0  redo size
            892  bytes sent via SQL*Net to client
            503  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
              9  rows processed

    请问:
    Note: cpu costing is off是什么意思?


    __________________
    http://blog.itpub.net/foreverlee
    Concentrate on Oracle Performance Tuning.
    MSN: foreverlee0619@msn.com
    只看该作者    顶部
    离线 chenmolin
    高级会员


    精华贴数 1
    个人空间 0
    技术积分 2595 (782)
    社区积分 280 (2556)
    注册日期 2003-3-25
    论坛徽章:3
    ITPUB元老会员2006贡献徽章授权会员   
          

    发表于 2004-12-27 11:50 
    对于cpu的cost统计没有给出。


    __________________
    =====================Email:chenmolin@163.=====================
    只看该作者    顶部
    离线 vcpubljj
    中级会员



    精华贴数 0
    个人空间 0
    技术积分 298 (8051)
    社区积分 2 (30950)
    注册日期 2002-11-22
    论坛徽章:0
          
          

    发表于 2004-12-29 10:51 
    o


    只看该作者    顶部
    离线 wzy25
    黑骑士



    精华贴数 34
    个人空间 0
    技术积分 34652 (32)
    社区积分 567 (1740)
    注册日期 2001-12-14
    论坛徽章:39
    现任管理团队成员ITPUB元老2010数据库技术大会纪念徽章管理团队2007贡献徽章参与2007年甲骨文全球大会(中国上海)纪念月度精华徽章
    ITPUB北京香山2007年会纪念徽章管理团队2006纪念徽章会员2007贡献徽章会员2006贡献徽章授权会员2010新春纪念徽章

    发表于 2004-12-31 10:25 
    这个东西很好勇,我经常用。


    __________________
    wrong_x@hotmail.com
    http://wzy25.itpub.net
    提供保险核心业务系统咨询,评估业务。提供上海北京及北京周边地区oracle db,oracle as,weblogic技术支持,培训
    只看该作者    顶部
    在线/呼叫 Yong Huang
    版主



    精华贴数 3
    个人空间 0
    技术积分 7427 (238)
    社区积分 192 (3169)
    注册日期 2001-10-9
    论坛徽章:12
    现任管理团队成员ITPUB元老管理团队2006纪念徽章会员2006贡献徽章授权会员2010新春纪念徽章
    2010新春纪念徽章祖国60周年纪念徽章ITPUB8周年纪念徽章2009日食纪念2009新春纪念徽章2008新春纪念徽章

    发表于 2005-1-2 11:39 
    I want to add some comments that may or may not be relevant.

    (1) One issue of Oracle Magazine, the 1st issue of 2004 I think, AskTom column talks about TempSpc in xplan output. But at least one time I couldn't get numbers for that, even if the query is sure to use temp space. I didn't spend much time finding out why.

    (2) In 10g, you don't need to create plan_table in your schema to explain plan. It uses the table plan_table$ belonging to SYS through a public synonym. The table is a session duration-based global temporary table. Makes perfect sense!

    (3) Recently somebody on news:comp.databases.oracle.server asked how to check syntax of long SQL code without running it (which would take many hours). I suggested using explain plan. I.e., prepend EXPLAIN PLAN FOR to each SQL statement. If running that .sql script shows no errors, all syntax is good.

    Any comments and corrections are welcome.

    Yong Huang


    只看该作者    顶部
    离线 wlidflower
    版主


    精华贴数 0
    个人空间 0
    技术积分 4983 (363)
    社区积分 81 (5004)
    注册日期 2004-7-31
    论坛徽章:16
    管理团队成员管理团队2006纪念徽章会员2007贡献徽章会员2006贡献徽章授权会员生肖徽章2007版:鼠
          

    发表于 2005-1-4 21:12 
    比较好用的东西
    我也常用


    只看该作者    顶部
    相关内容


    CopyRight 1999-2006 itpub.net All Right Reserved.
    北京皓辰网域网络信息技术有限公司. 版权所有
    网站律师 隐私政策 知识产权声明
    广播电视节目制作经营许可证:编号(京)字第1149号
    京ICP证:060528号 联系我们

    北京市公安局海淀分局网监中心备案编号:1101082001