在SQL*PLUS中应用AUTOTRACE REPORT
2024-07-21 02:08:18
供稿:网友
在sql*plus中应用autotrace report
作者:刘颖博
时间:2004-1-12
mail:[email protected],请指正
转载请注明出处及作者
在sql*plus中,当你成功的执行一个dml语句,比如select, delete, update ,insert,你可以通过sql优化器和语句的执行统计自动的获得一份报告。这份报告对于dml语句的性能监控和调优都是很有用处的。这份报告就是本文要讲的autotrace 报告。
配置autotrace报告(configuring the autotrace report)
你可以通过以下的autotrace系统变量来配置autotrace报告. 如下表:
table autotrace settings
autotrace setting
result
set autotrace off
不能获得autotrace报告. 这是默认的.
set autotrace on explain
仅仅显示优化器执行计划的autotrace报告
set autotrace on statistics
仅仅显示sql语句执行的统计结果的autotrace报告
set autotrace on
包括上面两项内容的autotrace报告
set autotrace traceonly
与set autotrace on类似,所有的统计和数据都在,但不可以打印
其实,平时我们应该较多的就是set autotrace on 、set autotrace off,谁会在乎多看一点autotrace报告呢! j
安装autotrace报告(setups required for the autotrace report)
要用这个特性,用户必须被赋予plustrace角色,而plustrace角色需要dba来赋予。另外,该用户必须创建plan_table表。
i. 首先创建plustrace角色并且赋给dba:
connect sys/sys’s password as sysdba
@$oracle_home/sqlplus/admin/plustrce.sql
ii. 赋权限给用户
connect / as sysdba
grant plustrace to user(预赋权的用户名);
这样,就可以在该用户下设置autotrace报告的显示与否了。
autotrace报告中涉及到的两个方面的内容
i. sql语句的执行计划
执行计划就是sql优化器执行语句的查询执行顺序,每一行的执行计划都会有个行号,这个行号是连续的
ii. sql语句的数据库统计
数据库统计是服务器记录的执行当前的这条语句所需要的系统资源等,主要包括下表的内容
database statistic name
description
recursive calls
number of recursive calls generated at both the user and system level. oracle maintains tables used for internal processing. when oracle needs to make a change to these tables, it internally generates an internal sql statement, which in turn generates a recursive call.
db block gets
number of times a current block was requested.
consistent gets
number of times a consistent read was requested for a block.
physical reads
total number of data blocks read from disk. this number equals the value of "physical reads direct" plus all reads into buffer cache.
redo size
total amount of redo generated in bytes.
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)
number of sort operations that were performed completely in memory and did not require any disk writes.
sorts (disk)
number of sort operations that required at least one disk write.
rows processed
number of rows processed during the operation.
简单使用的例子:
sql> show user
user is "sys"
(注:当前是sysdba用户)
(注:创建plustrace角色并且赋给dba)
sql> start $oracle_home/sqlplus/admin/plustrce.sql
(注:start 等价于@)
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>
sql> grant select on v_$session to plustrace;
grant succeeded.
sql> grant plustrace to dba with admin option;
grant succeeded.
sql>
sql> set echo off
(注:赋角色plustrace给所需用户,这里用户是bill)
sql> grant plustrace to bill;
grant succeeded.
(注:接下来,用bill用户连接,测试autotrace)
sql> connect bill/bill’s password;
connected.
sql> show user
user is "bill"
(注:为了看的清楚些,建立一个测试的表test,然后察看autotrace报告)
sql> create table test(id number(1));
table created.
sql> insert into test values(1);
1 row created.
sql> /
1 row created.
sql> /
1 row created.
sql> /
1 row created.
sql> commit;
commit complete.
sql> select * from test;
id
----------
1
1
1
1
sql> set autotrace on
sql> select * from test;
id
----------
1
1
1
1
execution plan
----------------------------------------------------------
0 select statement optimizer=choose
1 0 table access (full) of 'test'
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
547 bytes sent via sql*net to client
655 bytes received via sql*net from client
2 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
上面我们就可以看到,当你设置了set autotrace on后,执行相应的dml语句,就会有相应的autotrace报告出现了。当然,我们的这种设置是session级别的。这样,我们就可以根据autotrace报告对我们执行的dml语句进行分析和调优了!
参考:
oracle的官方文档(otn.oracle.com)