AUTOTRACE是一个SQL*Plus工具,用于跟踪SQL的执行计划,收集执行时所耗用资源的统计信息。系统账户本身具有AUTOTRACE,其他账户需要通过手动赋予

一. 用系统账户登录(DBA)

 SQL> set autotrace traceonly
SQL> select * from hr.jobs; 19 rows selected. Execution Plan
----------------------------------------------------------
Plan hash value: 944056911 --------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 19 | 627 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| JOBS | 19 | 627 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------- Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
9 consistent gets
0 physical reads
0 redo size
1452 bytes sent via SQL*Net to client
396 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
19 rows processed SQL>

二.授予其他用户AUTOTRACE 功能

2.1 创建基础表  运行$ORACLE_HOME/rdbms/admin/utlxplan脚本来创建plan_table

 SQL> conn /as sysdba                        --用系统账户登录
Connected.
SQL> start $ORACLE_HOME/rdbms/admin/utlxplan --执行utlxplan脚本 Table created. SQL> create public synonym plan_table for plan_table;--为表plan_table创建公共同义词 Synonym created. --如果需要限制Autotrace权限,可以把对public的授权改为对特定user的授权。
SQL> grant all on plan_table to public;--将同义词表plan_table授予给所有用户 Grant succeeded. SQL>

2.2 创建角色  运行$ORACLE_HOME/sqlplus/admin/plustrce.sql脚本

 SQL> start $ORACLE_HOME/sqlplus/admin/plustrce.sql  --执行创建角色的脚本
SQL>
--以下是脚本执行过程:首先 删除PLUSTRACE角色,重建PLUSTRACE角色,赋予权限
SQL> drop role plustrace;
drop role plustrace
*
ERROR at line 1:
ORA-01919: role 'PLUSTRACE' does not exist SQL> create role plustrace; Role created. SQL>
SQL> grant select on v_$sesstat to plustrace; Grant succeeded. SQL> grant select on v_$statname to plustrace; Grant succeeded. SQL> grant select on v_$mystat to plustrace; Grant succeeded. SQL> grant plustrace to dba with admin option; Grant succeeded. SQL>
SQL> set echo off
SQL>

2.3 角色的授予

  在创建角色后,DBA首先被授予了该角色,且可以将角色授予其它组和用户。可以手工把plustrace授予给public,则该数据库内所有的用户都将拥有plustrace角色的权限。也可以单独授予给某个组和用户。

 --授予给单独用户
SQL> grant plustrace to hr; Grant succeeded. --授予给所有用户
SQL> grant plustrace to public; Grant succeeded. SQL>

  现在所有用户即可使用autotrace 功能

三、AUTOTRACE的几个选项

 --在sql提示符下输入set autot后将会给出设置autotrace的提示,如下:
SQL> set autot
Usage: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]] set autotrace off :缺省值,将不生成autotrace 报告
set autotrace on :包含执行计划和统计信息
set autotrace traceonly :等同于set autotrace on,但不显示查询输出的结果
set autotrace on explain :只显示优化器执行路径报告
set autotrace on statistics :只显示执行统计信息

四.hr用户示例

 SQL> conn hr/hr123
Connected.
SQL> set autotrace traceonly
SQL> select * from jobs; 19 rows selected. Execution Plan
----------------------------------------------------------
Plan hash value: 944056911 --------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 19 | 627 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| JOBS | 19 | 627 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------- Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
9 consistent gets
0 physical reads
0 redo size
1452 bytes sent via SQL*Net to client
396 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
19 rows processed SQL>

最新文章

  1. Vi命令备忘
  2. 【待整理】Linux故障排查
  3. Teamcenter 2007 "由于某些意外的 DBMS 错误失败"
  4. 75 int类型数组中除了一个数出现一次或两次以外,其他数都出现三次,求这个数。[2行核心代码]
  5. 301页面转向 php
  6. jQuery 源码基本框架
  7. yii2 resetful 授权验证
  8. JTA事务管理
  9. Fibonacci_array
  10. python创建udp服务端和客户端
  11. Team Foundation Server 2013 KEY(密钥)
  12. bootstrap css布局
  13. RowFilter遇上特殊字符*%'[]\
  14. Spring Boot 之 RESTfull API简单项目的快速搭建(三)
  15. asp.net的forms身份验证 单用户身份验证
  16. jQuery 判断浏览器
  17. chrome 此网页正试图从未经验证的来源加载脚本
  18. CDOJ 1502 string(简单贪心)
  19. FocusBI:SSAS体系结构(原创)
  20. IO模式

热门文章

  1. 支持https请求以及https请求的抓包
  2. Tick and Tick
  3. Spring 中jdbcTemplate 实现执行多条sql语句
  4. 分享一个java线程专栏
  5. MySQL 更改默认编码为 utf8 (转)
  6. hdu 4669 动态规划
  7. 屏蔽cBox广告的方法 无需安装软件
  8. Git CMD - remote: Manage set of tracked repositories
  9. fresco的源码学习自我总结
  10. 【分享】.Net有哪些大型项目、大型网站的案例?