查看存储过程的执行计划

存储过程没有执行计划,查看的是存储过程中SQL语句的执行计划,这里用10046来看

----------------------------------------------------  
创建存储过程
Create Or Replace Procedure Lee_Xc(Eno Number) Is

Begin

  Select Empno, Ename, Dname
    From Emp, Dept
   Where Emp.Deptno = Dept.Deptno
     And Emp.Deptno = Eno;
  Commit;
End;
/
----------------------------------------------------  
追踪10046

SQL> alter session set events '10046 trace name context forever, level 12';
 
Session altered
 
SQL> exec   LEE_HK(8800186378);
 
PL/SQL procedure successfully completed
 
SQL> alter session set events '10046 trace name context off';
 
Session altered
 
SQL> 
SQL> SELECT    d.VALUE
  2         || '/'
  3         || LOWER (RTRIM (i.INSTANCE, CHR (0)))
  4         || '_ora_'
  5         || p.spid
  6         || '.trc' as "trace_file_name"
  7       FROM (SELECT p.spid
  8               FROM v$mystat m, v$session s, v$process p
  9              WHERE m.statistic# = 1 AND s.SID = m.SID AND p.addr = s.paddr) p,
 10           (SELECT t.INSTANCE
 11               FROM v$thread t, v$parameter v
 12             WHERE v.NAME = 'thread'
 13               AND (v.VALUE = 0 OR t.thread# = TO_NUMBER (v.VALUE))) i,
 14           (SELECT VALUE
 15               FROM v$parameter
 16            WHERE NAME = 'user_dump_dest') d
 17  ;
 
trace_file_name
--------------------------------------------------------------------------------
/oracle/admin/jsjdata0/udump/jsjdata0_ora_19658.trc

----------------------------------------------------  
格式转换一下
[oracle@jsb2011data udump]$ tkprof jsjdata0_ora_19984.trc  lee1 sys=no explain=gcbb/gcbb


[oracle@jsb2011data udump]$ more lee5.prf 

TKPROF: Release 10.2.0.1.0 - Production on Thu Apr 26 14:50:56 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Trace file: jsjdata0_ora_19984.trc
Sort options: default

********************************************************************************
count    = number of times OCI procedure was executed
cpu      = cpu time in seconds executing 
elapsed  = elapsed time in seconds executing
disk     = number of physical reads of buffers from disk
query    = number of buffers gotten for consistent read
current  = number of buffers gotten in current mode (usually for update)
rows     = number of rows processed by the fetch or execute call
********************************************************************************

alter session set events '10046 trace name context forever, level 12' 


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        1      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 54  (SCOTT)

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       1        0.00          0.00
  SQL*Net message from client                     1        0.03          0.03
********************************************************************************

begin :id := sys.dbms_transaction.local_transaction_id; end;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        4      0.00       0.00          0          0          0           0
Execute      4      0.00       0.00          0          0          0           4
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        8      0.00       0.00          0          0          0           4

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 54  (SCOTT)

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       4        0.00          0.00
  SQL*Net message from client                     4        8.29         16.08
********************************************************************************

select 'x' 
from
 dual


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        2      0.00       0.00          0          0          0           0
Execute      2      0.00       0.00          0          0          0           0
Fetch        2      0.00       0.00          0          0          0           2
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        6      0.00       0.00          0          0          0           2

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 54  (SCOTT)

Rows     Row Source Operation
-------  ---------------------------------------------------
      1  FAST DUAL  (cr=0 pr=0 pw=0 time=10 us)


Rows     Execution Plan
-------  ---------------------------------------------------
      0  SELECT STATEMENT   MODE: ALL_ROWS
      1   FAST DUAL


Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       4        0.00          0.00
  SQL*Net message from client                     4        0.01          0.03
********************************************************************************

begin lee_xc(7788); end;     --这里可以看到输入的参数 


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 54  (SCOTT)

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  log file sync                                   1        0.00          0.00
  SQL*Net message to client                       1        0.00          0.00
  SQL*Net message from client                     1        0.03          0.03
********************************************************************************

UPDATE EMP SET SAL = 30000  --这里看到带参的SQL
WHERE
 EMPNO = :B1 


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          1          2           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        1      0.00       0.00          0          1          2           1

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 54  (SCOTT)   (recursive depth: 1)

Rows     Execution Plan                                                    ---这里就是执行计划了
-------  ---------------------------------------------------
      0  UPDATE STATEMENT   MODE: ALL_ROWS
      0   UPDATE OF 'EMP'
      0    INDEX   MODE: ANALYZED (UNIQUE SCAN) OF 'PK_EMP' (INDEX 
               (UNIQUE))

********************************************************************************

COMMIT


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          1           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        1      0.00       0.00          0          0          1           0

Misses in library cache during parse: 0
Parsing user id: 54  (SCOTT)   (recursive depth: 1)
********************************************************************************

alter session set events '10046 trace name context off'  


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           0

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 54  (SCOTT)



********************************************************************************

OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        8      0.00       0.00          0          0          0           0
Execute      9      0.00       0.00          0          0          0           5
Fetch        2      0.00       0.00          0          0          0           2
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total       19      0.00       0.00          0          0          0           7

Misses in library cache during parse: 2
Misses in library cache during execute: 1

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                      10        0.00          0.00
  SQL*Net message from client                    10        8.29         16.17
  log file sync                                   1        0.00          0.00


OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      2      0.00       0.00          0          1          3           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          1          3           1

Misses in library cache during parse: 0

   11  user  SQL statements in session.
    0  internal SQL statements in session.
   11  SQL statements in session.
    2  statements EXPLAINed in this session.
********************************************************************************
Trace file: jsjdata0_ora_19984.trc
Trace file compatibility: 10.01.00
Sort options: default

       1  session in tracefile.
      11  user  SQL statements in trace file.
       0  internal SQL statements in trace file.
      11  SQL statements in trace file.
       7  unique SQL statements in trace file.
       2  SQL statements EXPLAINed using schema:
           SCOTT.prof$plan_table
             Default table was used.
             Table was created.
             Table was dropped.
     144  lines in trace file.
      16  elapsed seconds in trace file.

查看数据库中存储过程执行计划是优化查询性能和分析存储过程行为的重要手段。不同的数据库管理系统(如 SQL Server、MySQL、Oracle 等)提供了不同的方式来获取执行计划。以下是一些常见数据库系统中查看存储过程执行计划的方法: ### SQL Server 在 SQL Server 中,可以通过以下方式查看存储过程执行计划: 1. **使用 SQL Server Management Studio (SSMS)**: - 打开 SSMS,连接到数据库实例。 - 新建查询窗口,输入 `SET SHOWPLAN_ALL ON;` 或者使用图形化执行计划选项(“显示实际执行计划”)。 - 执行存储过程调用,例如:`EXEC YourStoredProcedureName @Param1 = Value1;` - 执行完成后,查看生成的执行计划。 2. **查询系统视图**: 可以使用以下查询来获取存储过程执行计划相关信息: ```sql SELECT qs.execution_count, qs.total_logical_reads, qs.total_logical_writes, qs.total_worker_time, qs.total_elapsed_time, qp.query_plan FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp WHERE st.text LIKE '%YourStoredProcedureName%'; ``` 3. **使用动态管理视图(DMV)**: 可以结合 `sys.dm_exec_procedure_stats` 和 `sys.dm_exec_query_plan` 获取执行计划[^3]。 ### MySQL 在 MySQL 中,查看存储过程执行计划相对较为间接,通常需要查看存储过程中包含的 SQL 语句,并使用 `EXPLAIN` 命令分析其执行计划: 1. **查看存储过程定义**: ```sql SHOW CREATE PROCEDURE YourProcedureName; ``` 2. **使用 `EXPLAIN` 分析 SQL 语句**: 将存储过程中的 SQL 语句单独提取出来,然后在其前加上 `EXPLAIN`: ```sql EXPLAIN SELECT * FROM your_table WHERE condition; ``` ### Oracle 在 Oracle 数据库中,可以通过以下方式查看存储过程执行计划: 1. **使用 `EXPLAIN PLAN FOR`**: ```sql EXPLAIN PLAN FOR BEGIN YourStoredProcedureName(p1 => value1, p2 => value2); END; / ``` 2. **查询 `PLAN_TABLE`**: ```sql SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY); ``` ### PostgreSQL 在 PostgreSQL 中,可以使用 `EXPLAIN ANALYZE` 来查看存储过程(通常以函数形式实现)的执行计划: ```sql EXPLAIN ANALYZE SELECT * FROM your_function_name(); ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值