Chinaunix首页 | 论坛 | 博客
  • 博客访问: 785117
  • 博文数量: 56
  • 博客积分: 451
  • 博客等级: 下士
  • 技术积分: 1431
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-20 11:14
文章分类

全部博文(56)

文章存档

2013年(35)

2012年(21)

分类: Oracle

2013-07-10 10:44:24

AUTOTRACE是一个SQL*Plus工具,用于跟踪SQL的执行计划,收集执行时所耗用资源的统计信息,是SQL优化工具之一,是分析SQL的执行计划,执行效率的一个非常简单方便的工具,在绝大多数情况下,也是非常有用的工具。利用AutoTrace工具提供的SQL执行计划和执行状态可以为我们优化SQL的时候提供优化的依据,以及优化效果的明显的对比效果。

Autotrace几个常用选项的说明:
SET AUTOTRACE OFF ---------------- 不生成AUTOTRACE 报告,这是缺省模式

SET AUTOTRACE ON EXPLAIN ------ AUTOTRACE只显示优化器执行路径报告
SET AUTOTRACE ON STATISTICS -- 只显示执行统计信息
SET AUTOTRACE ON ----------------- 包含执行计划和统计信息
SET AUTOTRACE TRACEONLY ------ 同set autotrace on,但是不显示查询输出 

OFF模式不用多说了,这里我们举例说明 SET AUTOTRACE ON 和 SET AUTOTRACE TRACEONLY的区别,让大家加深AUTOTRACE的理解

在OFF的情况下执行如下语句,输入下面结果:

点击(此处)折叠或打开

  1. SQL> select * from hr.jobs;

  2. JOB_ID JOB_TITLE MIN_SALARY MAX_SALARY
  3. ---------- ----------------------------------- ---------- ----------
  4. AD_PRES President 20080 40000
  5. AD_VP Administration Vice President 15000 30000
  6. AD_ASST Administration Assistant 3000 6000
  7. FI_MGR Finance Manager 8200 16000
  8. FI_ACCOUNT Accountant 4200 9000
  9. AC_MGR Accounting Manager 8200 16000
  10. AC_ACCOUNT Public Accountant 4200 9000
  11. SA_MAN Sales Manager 10000 20080
  12. SA_REP Sales Representative 6000 12008
  13. PU_MAN Purchasing Manager 8000 15000
  14. PU_CLERK Purchasing Clerk 2500 5500

  15. JOB_ID JOB_TITLE MIN_SALARY MAX_SALARY
  16. ---------- ----------------------------------- ---------- ----------
  17. ST_MAN Stock Manager 5500 8500
  18. ST_CLERK Stock Clerk 2008 5000
  19. SH_CLERK Shipping Clerk 2500 5500
  20. IT_PROG Programmer 4000 10000
  21. MK_MAN Marketing Manager 9000 15000
  22. MK_REP Marketing Representative 4000 9000
  23. HR_REP Human Resources Representative 4000 9000
  24. PR_REP Public Relations Representative 4500 10500
然后设置SET AUTOTRACE ON后,同样执行上列SQL语句

点击(此处)折叠或打开

  1. SQL> set autotrace on
  2. SQL> select * from hr.jobs;

  3. JOB_ID JOB_TITLE MIN_SALARY MAX_SALARY
  4. ---------- ----------------------------------- ---------- ----------
  5. AD_PRES President 20080 40000
  6. AD_VP Administration Vice President 15000 30000
  7. AD_ASST Administration Assistant 3000 6000
  8. FI_MGR Finance Manager 8200 16000
  9. FI_ACCOUNT Accountant 4200 9000
  10. AC_MGR Accounting Manager 8200 16000
  11. AC_ACCOUNT Public Accountant 4200 9000
  12. SA_MAN Sales Manager 10000 20080
  13. SA_REP Sales Representative 6000 12008
  14. PU_MAN Purchasing Manager 8000 15000
  15. PU_CLERK Purchasing Clerk 2500 5500

  16. JOB_ID JOB_TITLE MIN_SALARY MAX_SALARY
  17. ---------- ----------------------------------- ---------- ----------
  18. ST_MAN Stock Manager 5500 8500
  19. ST_CLERK Stock Clerk 2008 5000
  20. SH_CLERK Shipping Clerk 2500 5500
  21. IT_PROG Programmer 4000 10000
  22. MK_MAN Marketing Manager 9000 15000
  23. MK_REP Marketing Representative 4000 9000
  24. HR_REP Human Resources Representative 4000 9000
  25. PR_REP Public Relations Representative 4500 10500

  26. 19 rows selected.


  27. Execution Plan
  28. ----------------------------------------------------------
  29. Plan hash value: 944056911

  30. --------------------------------------------------------------------------
  31. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  32. --------------------------------------------------------------------------
  33. | 0 | SELECT STATEMENT | | 19 | 627 | 3 (0)| 00:00:01 |
  34. | 1 | TABLE ACCESS FULL| JOBS | 19 | 627 | 3 (0)| 00:00:01 |
  35. --------------------------------------------------------------------------


  36. Statistics
  37. ----------------------------------------------------------
  38.           0 recursive calls
  39.           0 db block gets
  40.           9 consistent gets
  41.           0 physical reads
  42.           0 redo size
  43.        1700 bytes sent via SQL*Net to client
  44.         534 bytes received via SQL*Net from client
  45.           3 SQL*Net roundtrips to/from client
  46.           0 sorts (memory)
  47.           0 sorts (disk)
  48.          19 rows processed
可以看到和OFF情况下,多了
Execution Plan和Statistics信息

然后我们再看下SET AUTOTRACE TRACEONLY情况下,执行同样SQL语句的结果:

点击(此处)折叠或打开

  1. SQL> set autotrace traceonly
  2. SQL> select * from hr.jobs;

  3. 19 rows selected.


  4. Execution Plan
  5. ----------------------------------------------------------
  6. Plan hash value: 944056911

  7. --------------------------------------------------------------------------
  8. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  9. --------------------------------------------------------------------------
  10. | 0 | SELECT STATEMENT | | 19 | 627 | 3 (0)| 00:00:01 |
  11. | 1 | TABLE ACCESS FULL| JOBS | 19 | 627 | 3 (0)| 00:00:01 |
  12. --------------------------------------------------------------------------


  13. Statistics
  14. ----------------------------------------------------------
  15.           0 recursive calls
  16.           0 db block gets
  17.           9 consistent gets
  18.           0 physical reads
  19.           0 redo size
  20.        1700 bytes sent via SQL*Net to client
  21.         534 bytes received via SQL*Net from client
  22.           3 SQL*Net roundtrips to/from client
  23.           0 sorts (memory)
  24.           0 sorts (disk)
  25.          19 rows processed
可以看到没有显示查询结果,只显示出来执行计划和统计信息。




阅读(1881) | 评论(0) | 转发(0) |
0

上一篇:oracle手动建库

下一篇:理解DBA_blockers

给主人留下些什么吧!~~