Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate explain plan for entire stored procedure

I usually generate explain plans using the following in 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

But what If I want to generate explain plan for a stored procedure?

Is there a way to generate explain plan for the entire stored procedure? The SP has no input/output parameters.

like image 591
learn_plsql Avatar asked Dec 22 '10 15:12

learn_plsql


2 Answers

What you are generating is correctly called an "execution plan". "Explain plan" is a command used to generate and view an execution plan, much as AUTOTRACE TRACEONLY does in your example.

By definition, an execution plan is for a single SQL statement. A PL/SQL block does not have an execution plan. If it contains one or more SQL statements, then each of those will have an execution plan.

One option is to manually extract the SQL statements from the PL/SQL code and use the process you've already shown.

Another option is to active SQL tracing then run the procedure. This will produce a trace file on the server that contains the execution plans for all statements executed in the session. The trace is in fairly raw form so it is generally easiest to format it using Oracle's TKPROF tool; there are also various third-party tools that process these trace files as well.

like image 74
Dave Costa Avatar answered Nov 15 '22 04:11

Dave Costa


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
like image 30
Panky031 Avatar answered Nov 15 '22 04:11

Panky031