查看执行计划

本文详细介绍了如何使用autotrace、explainplanfor和tkprof等工具来生成和优化SQL执行计划,包括如何设置autotrace参数、使用explainplanfor进行计划分析以及通过tkprof工具转换跟踪文件以获取性能数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何产生执行计划?

1) autotrace

准备PLAN_TABLE,/rdbms/admin/utlxplan.sql  /sqlplus/admin/plustrace.sql

grant plustrace to user_name;

用法: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]

举例:SET AUTOT [RACE] OFF 

         停止AutoTrace

         SET AUTOT [RACE] ON 

         开启AutoTrace,显示AUTOTRACE信息和SQL执行结果

         SET AUTOT[RACE] TRACEONLY 

         开启AutoTrace,仅显示AUTOTRACE信息

         SET AUTOT[RACE] ON EXPLAIN  

         开启AutoTrace,仅显示AUTOTRACE的EXPLAIN信息

         SET AUTOT[RACE] ON STATISTICS

          开启AutoTrace,仅显示AUTOTRACE的STATISTICS信息

[c-sharp] view plain copy print ?
  1. SQL> set autotrace on;  
  2. SQL> select * from dual;  
  3.   
  4. D  
  5. -  
  6. X  
  7.   
  8.   
  9. Execution Plan  
  10. ----------------------------------------------------------  
  11. Plan hash value: 272002086  
  12.   
  13. --------------------------------------------------------------------------  
  14. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  15. --------------------------------------------------------------------------  
  16. |   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |  
  17. |   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |  
  18. --------------------------------------------------------------------------  
  19.   
  20.   
  21. Statistics  
  22. ----------------------------------------------------------  
  23.           1  recursive calls  
  24.           0  db block gets  
  25.           3  consistent gets  
  26.           0  physical reads  
  27.           0  redo size  
  28.         407  bytes sent via SQL*Net to client  
  29.         385  bytes received via SQL*Net from client  
  30.           2  SQL*Net roundtrips to/from client  
  31.           0  sorts (memory)  
  32.           0  sorts (disk)  
  33.           1  rows processed  
[c-sharp] view plain copy print ?
  1. SQL> set autotrace traceonly;  
  2. SQL> select * from dual;  
  3.   
  4.   
  5. Execution Plan  
  6. ----------------------------------------------------------  
  7. Plan hash value: 272002086  
  8.   
  9. --------------------------------------------------------------------------  
  10. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  11. --------------------------------------------------------------------------  
  12. |   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |  
  13. |   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |  
  14. --------------------------------------------------------------------------  
  15.   
  16.   
  17. Statistics  
  18. ----------------------------------------------------------  
  19.           0  recursive calls  
  20.           0  db block gets  
  21.           3  consistent gets  
  22.           0  physical reads  
  23.           0  redo size  
  24.         407  bytes sent via SQL*Net to client  
  25.         385  bytes received via SQL*Net from client  
  26.           2  SQL*Net roundtrips to/from client  
  27.           0  sorts (memory)  
  28.           0  sorts (disk)  
  29.           1  rows processed  
[c-sharp] view plain copy print ?
  1. SQL> set autotrace on explain;  
  2. SQL> select * from dual;  
  3.   
  4. D  
  5. -  
  6. X  
  7.   
  8.   
  9. Execution Plan  
  10. ----------------------------------------------------------  
  11. Plan hash value: 272002086  
  12.   
  13. --------------------------------------------------------------------------  
  14. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  15. --------------------------------------------------------------------------  
  16. |   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |  
  17. |   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |  
  18. --------------------------------------------------------------------------  
  19.    
[c-sharp] view plain copy print ?
  1. SQL>  set autotrace on statistics;  
  2. SQL> /  
  3.   
  4. D  
  5. -  
  6. X  
  7.   
  8. Statistics  
  9. ----------------------------------------------------------  
  10.           0  recursive calls  
  11.           0  db block gets  
  12.           3  consistent gets  
  13.           0  physical reads  
  14.           0  redo size  
  15.         407  bytes sent via SQL*Net to client  
  16.         385  bytes received via SQL*Net from client  
  17.           2  SQL*Net roundtrips to/from client  
  18.           0  sorts (memory)  
  19.           0  sorts (disk)  
  20.           1  rows processed  

 recursive calls:递归调用统计数据(指由于你需要执行其他SQL语句而必须执行的SQL);

db block gets:通过update/delete/select for update读的次数;

consistent gets : 通过不带for update的select 读的次数;

physical reads:物理读,从磁盘上读取数据块的数量;

redo size:重做尺寸统计信息显示在执行过程中产生了多少重做数据;

bytes sent via SQL*Net to client:Total number of bytes sent to the client from the foreground processes;

bytes received via SQL*Net from client:Total number of bytes received from the client over Oracle Net;

SQL*Net roundtrips to/from client:Total number of Oracle Net messages sent to and received from the client;

sorts ( memory):在内存中排序的次数;

sorts(disk):在磁盘中排序的次数;

rows processed:结果返回的行数。

2) explain plan for

准备PLAN_TABLE,/rdbms/admin/utlxplan.sql

用法:explain plan for set statement= ‘…..’

          explain plan for select ….

[c-sharp] view plain copy print ?
  1. SQL> explain plan for SELECT /*+ordered use_hash(d,e) */e.ename,d.deptno  
  2.   2  from lpx_emp e,lpx_dept d   
  3.   3  WHERE e.deptno = d.deptno;  
  4.    
  5. Explained  
  6.    
  7. SQL> select * from table(dbms_xplan.display());  
  8.    
  9. PLAN_TABLE_OUTPUT  
  10. --------------------------------------------------------------------------------  
  11. Plan hash value: 2846336464  
  12. -------------------------------------------------------------------------------  
  13. | Id  | Operation          | Name     | Rows  | Bytes | Cost (%CPU)| Time     |  
  14. -------------------------------------------------------------------------------  
  15. |   0 | SELECT STATEMENT   |          |    14 |   462 |     7  (15)| 00:00:01 |  
  16. |*  1 |  HASH JOIN         |          |    14 |   462 |     7  (15)| 00:00:01 |  
  17. |   2 |   TABLE ACCESS FULL| LPX_EMP  |    14 |   280 |     3   (0)| 00:00:01 |  
  18. |   3 |   TABLE ACCESS FULL| LPX_DEPT |     4 |    52 |     3   (0)| 00:00:01 |  
  19. -------------------------------------------------------------------------------  
  20. Predicate Information (identified by operation id):  
  21. ---------------------------------------------------  
  22.    1 - access("E"."DEPTNO"="D"."DEPTNO")  
  23. Note  
  24. -----  
  25.    - dynamic sampling used for this statement  
  26.    
  27. 19 rows selected  

 

 

3) tkprof      

        oracle在打开跟踪功能后,将被跟踪session中正在执行的SQL的性能状态数据都收集到一个跟踪文件中。这个跟踪文件提供了许多有用的信息,例如一个sql的解析次数、执行次数、fetch次数、物理读次数、逻辑读次数、CPU使用时间等,利用这些信息可以诊断你的sql的问题,从而用来优化你的系统。TKPROF工具对其进行转换。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值