会话级:
SQL> alter session set timed_statistics=True;
实例级:
SQL> alter system set timed_statistics=True scope=both;
2、 设置SQL_TRACE,可以在会话级,也可以在数据库级。
会话级:
SQL> alter session set sql_trace=true;
或者:
SQL>EXEC DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(SID,SERIAL#,TRUE);
实例级:
SQL> alter system set sql_trace=true scope=both;
Session altered.
SQL> select count(*) from t;
COUNT(*)
----------
250734
SQL> alter session set timed_statistics=true;
Session altered.
SQL> select count(*) from t;
COUNT(*)
----------
250734
SQL> alter session set sql_trace =false;
Session altered.
p.spid || ‘.trc‘ trace_file_name
FROM (SELECT p.spid
FROM vmystatm,vmystatm,vsession s, vprocess p
WHERE m.statistic# = 1
AND s.SID = m.SID
AND p.addr = s.paddr) p,
(SELECT t.INSTANCE
FROM vprocess p WHERE m.statistic# = 1 AND s.SID = m.SID AND p.addr = s.paddr) p, (SELECT t.INSTANCE FROM vthread t, vparameter v
WHERE v.NAME = ‘thread‘
AND (v.VALUE = 0 OR t.thread# = TO_NUMBER(v.VALUE))) i,
(SELECT VALUE FROM vparameter v WHERE v.NAME = ‘thread‘ AND (v.VALUE = 0 OR t.thread# = TO_NUMBER(v.VALUE))) i, (SELECT VALUE FROM vparameter WHERE NAME = ‘user_dump_dest‘) d
/
TRACE_FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/diag/rdbms/db11/db112/trace/db112_ora_7677.trc
TKPROF: Release 11.2.0.1.0 - Development on Wed Feb 19 22:13:19 2014
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Trace file: /u01/app/oracle/diag/rdbms/db11/db112/trace/db112_ora_7677.trc
Sort options: fchela
********************************************************************************
count = number of times OCI procedure was executed
cpu = cpu time in seconds executing
elapsed = elapsed time in seconds executing
disk = number of physical reads of buffers from disk
query = number of buffers gotten for consistent read
current = number of buffers gotten in current mode (usually for update)
rows = number of rows processed by the fetch or execute call
********************************************************************************
SQL ID: cyzznbykb509s
Plan Hash: 2966233522
select count(*)
from
t
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 2 0.00 0.00 0 0 0 0
Execute 2 0.00 0.00 0 0 0 0
Fetch 4 0.09 1.51 7294 7298 0 2
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 8 0.09 1.52 7294 7298 0 2
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 127
Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=3649 pr=3647 pw=0 time=0 us)
250734 TABLE ACCESS FULL T (cr=3649 pr=3647 pw=0 time=67962 us cost=994 size=0 card=250734)
********************************************************************************
SQL ID: 4tk6t8tfsfqbf
Plan Hash: 0
alter session set sql_trace=true
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 0 0.00 0.00 0 0 0 0
Execute 1 0.00 0.04 7 56 0 0
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 1 0.00 0.04 7 56 0 0
Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 127
********************************************************************************
SQL ID: dqcjr8t9drmkw
Plan Hash: 0
alter session set sql_trace =false
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 2 0.00 0.00 0 0 0 0
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 127
********************************************************************************
SQL ID: cf0ac3jj8sa1c
Plan Hash: 0
alter session set timed_statistics=true
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 2 0.00 0.00 0 0 0 0
Misses in library cache during parse: 0
Parsing user id: 127
********************************************************************************
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 4 0.00 0.00 0 0 0 0
Execute 5 0.00 0.04 7 56 0 0
Fetch 4 0.09 1.51 7294 7298 0 2
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 13 0.10 1.57 7301 7354 0 2
Misses in library cache during parse: 2
Misses in library cache during execute: 1
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 11 0.01 0.02 0 0 0 0
Execute 227 0.07 0.13 0 0 0 0
Fetch 242 0.01 0.25 39 728 0 2129
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 480 0.10 0.41 39 728 0 2129
Misses in library cache during parse: 10
Misses in library cache during execute: 10
5 user SQL statements in session.
227 internal SQL statements in session.
232 SQL statements in session.
********************************************************************************
Trace file: /u01/app/oracle/diag/rdbms/db11/db112/trace/db112_ora_7677.trc
Trace file compatibility: 11.1.0.7
Sort options: fchela
2 sessions in tracefile.
5 user SQL statements in trace file.
227 internal SQL statements in trace file.
232 SQL statements in trace file.
15 unique SQL statements in trace file.
1760 lines in trace file.
34 elapsed seconds in trace file.
先从os上利用top命令找到当前占用cpu资源最高的一个进程的PID号:14483
然后在数据库中根据PID号找到相应的sid号和serial#:
SQL> select s.sid,s.serial# from vsessions,vsessions,vprocess p where s.paddr=p.addr and p.spid=‘14483‘;
SID SERIAL#
---------- ----------
101 25695
使用dbms_system.set_sql_trace_in_session包来对这个session进行trace:
SQL> exec DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(101,25695,true);
PL/SQL procedure successfully completed.
到user_dump_dest定义的路径下查找刚刚最近生成的trace文件,可以根据时间来排序,找最近的trace文件,也可以根据SID_ORA_SPID.TRC的规则,即ORCL_ORA_14483.TRC找到TRACE文件。
首先解释输出文件中列的含义:
CALL:每次SQL语句的处理都分成三个部分
Parse:这步将SQL语句转换成执行计划,包括检查是否有正确的授权和所需要用到的表、列以及其他引用到的对象是否存在。
Execute:这步是真正的由Oracle来执行语句。对于insert、update、delete操作,这步会修改数据,对于select操作,这步就只是确定选择的记录。
Fetch:返回查询语句中所获得的记录,这步只有select语句会被执行。
COUNT:这个语句被parse、execute、fetch的次数。
CPU:这个语句对于所有的parse、execute、fetch所消耗的cpu的时间,以秒为单位。
ELAPSED:这个语句所有消耗在parse、execute、fetch的总的时间。
DISK:从磁盘上的数据文件中物理读取的块的数量。一般来说更想知道的是正在从缓存中读取的数据而不是从磁盘上读取的数据。
QUERY:在一致性读模式下,所有parse、execute、fetch所获得的buffer的数量。一致性模式的buffer是用于给一个长时间运行的事务提供一个一致性读的快照,缓存实际上在头部存储了状态。
CURRENT:在current模式下所获得的buffer的数量。一般在current模式下执行insert、update、delete操作都会获取buffer。在current模式下如果在高速缓存区发现有新的缓存足够给当前的事务使用,则这些buffer都会被读入了缓存区中。
ROWS: 所有SQL语句返回的记录数目,但是不包括子查询中返回的记录数目。对于select语句,返回记录是在fetch这步,对于insert、update、delete操作,返回记录则是在execute这步。