【问题标题】:how to generate explain plan for entire stored procedure如何为整个存储过程生成解释计划
【发布时间】:2011-05-29 11:18:32
【问题描述】:

我通常在 sqlplus 中使用以下内容生成解释计划:

SET AUTOTRACE ON
SET TIMING ON
SET TRIMSPOOL ON
SET LINES 200
SPOOL filename.txt
SET AUTOTRACE TRACEONLY;

{query goes here}

SPOOL OFF
SET AUTOTRACE OFF

但是如果我想为存储过程生成解释计划呢?

有没有办法为整个存储过程生成解释计划? SP 没有输入/输出参数。

【问题讨论】:

    标签: oracle stored-procedures sql-execution-plan


    【解决方案1】:
    Hi I have done like below for the stored procedure:
    SET AUTOTRACE ON
    SET TIMING ON
    SET TRIMSPOOL ON
    SET LINES 200
    SPOOL filename.txt
    SET AUTOTRACE TRACEONLY;
    @your stored procedure path
    SPOOL OFF
    SET AUTOTRACE OFF
    
    And got the below statistics: 
    
       Statistics
    -----------------------------------------------------------
                   6  CPU used by this session
                   8  CPU used when call started
                  53  DB time
                   6  Requests to/from client
              188416  cell physical IO interconnect bytes
                 237  consistent gets
                 112  consistent gets - examination
                 237  consistent gets from cache
                 110  consistent gets from cache (fastpath)
                2043  db block gets
                   1  db block gets direct
                2042  db block gets from cache
                 567  db block gets from cache (fastpath)
                  27  enqueue releases
                  27  enqueue requests
                   4  messages sent
                  31  non-idle wait count
                  19  non-idle wait time
                  44  opened cursors cumulative
                   2  opened cursors current
                  22  physical read total IO requests
              180224  physical read total bytes
                   1  physical write total IO requests
                8192  physical write total bytes
                   1  pinned cursors current
                 461  recursive calls
                   4  recursive cpu usage
                2280  session logical reads
             1572864  session pga memory
                  19  user I/O wait time
                   9  user calls
                   1  user commits
    No Errors.
    Autotrace Disabled
    

    【讨论】:

      【解决方案2】:

      您正在生成的内容正确地称为“执行计划”。 “解释计划”是用于生成和查看执行计划的命令,就像 AUTOTRACE TRACEONLY 在您的示例中所做的那样。

      根据定义,执行计划是针对单个 SQL 语句的。 PL/SQL 块没有执行计划。如果它包含一个或多个 SQL 语句,那么每个 SQL 语句都会有一个执行计划。

      一种选择是从 PL/SQL 代码中手动提取 SQL 语句并使用您已经展示的过程。

      另一个选项是激活 SQL 跟踪,然后运行该过程。这将在服务器上生成一个跟踪文件,其中包含会话中执行的所有语句的执行计划。跟踪是相当原始的形式,因此通常最容易使用 Oracle 的 TKPROF 工具对其进行格式化;还有各种第三方工具也可以处理这些跟踪文件。

      【讨论】:

      • 好的。我将采用手动提取方式。但是,现在如果我在存储过程中有一些循环块怎么办。我将如何为这些循环块运行执行计划,因为它们有一个 BEGIN 和 END
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 2012-11-12
      • 2015-09-01
      • 2019-09-27
      • 1970-01-01
      • 2020-08-29
      相关资源
      最近更新 更多