Oracle AUTOTRACE使用
AUTOTRACE可以辅助我们写出高质量的SQL
执行如下命令可以看到结果
SQL> set autotrace on; SQL> select * from dual; Execution Plan ---------------------------------------------------------- Plan hash value: 272002086 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 2 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS FULL| DUAL | 1 | 2 | 2 (0)| 00:00:01 | -------------------------------------------------------------------------- Statistics ---------------------------------------------------------- 5 recursive calls 0 db block gets 5 consistent gets 2 physical reads 0 redo size 522 bytes sent via SQL*Net to client 520 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed
如果执行set autotrace on出现如下错误
SQL> set autotrace on; SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled SP2-0611: Error enabling STATISTICS report
那么需要切换sys用户执行
@$ORACLE_HOME/sqlplus/admin/plustrce.sql命令,会出现如下结果。
SQL> @$ORACLE_HOME/sqlplus/admin/plustrce.sql SQL> SQL> drop role plustrace; Role dropped. 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
然后再把 plustrace 角色授权给你需要的用户
SQL> grant PLUSTRACE to USERNAME; Grant succeeded.
切换你需要的用户即可。
设置Autotrace的命令。
序号 |
命令 |
解释 |
1 |
SET AUTOTRACE OFF |
此为默认值,即关闭Autotrace |
2 |
SET AUTOTRACE ON |
产生结果集和解释计划并列出统计 |
3 |
SET AUTOTRACE ON EXPLAIN |
显示结果集和解释计划不显示统计 |
4 |
SETAUTOTRACE TRACEONLY |
显示解释计划和统计,尽管执行该语句但您将看不到结果集 |
5 |
SET AUTOTRACE TRACEONLY STATISTICS |
只显示统计 |
Autotrace执行计划的各列的涵义
序号 |
列名 |
解释 |
1 |
ID_PLUS_EXP |
每一步骤的行号 |
2 |
PARENT_ID_PLUS_EXP |
每一步的Parent的级别号 |
3 |
PLAN_PLUS_EXP |
实际的每步 |
4 |
OBJECT_NODE_PLUS_EXP |
Dblink或并行查询时才会用到 |
AUTOTRACE Statistics常用列解释
序号 |
列名 |
解释 |
1 |
db block gets |
从buffer cache中读取的block的数量 |
2 |
consistent gets |
从buffer cache中读取的undo数据的block的数量 |
3 |
physical reads |
从磁盘读取的block的数量 |
4 |
redo size |
DML生成的redo的大小 |
5 |
sorts (memory) |
在内存执行的排序量 |
7 |
sorts (disk) |
在磁盘上执行的排序量 |