今天浏览metalink,看到这篇interpreting raw sql_trace,比较老的一篇文章了,但是确实很有用,所以决定大略翻译一下吧。
我们知道有几种方法可以得到一个sql语句执行时后台的trace文件,一个是用sql_trace,一个是用dbms_support包或者dbms_system包,还有一种就是直接使用10046 event。
使用10046 event的方法大致如下:
alter session set events 10046 trace name context forever, level 12;<br>
your sql statement…
alter session set events 10046 trace name context off;
其中的level有1,4,8,12几个选项,其中1相当于设置sql_trace=true之后的结果,4包括1的结果和绑定变量的实际值,8包括1的结果和等待事件的情况,12则同时包含1的结果,绑定变量的实际值和等待事件情况,所以可以说level 12是最为详细的trace了。
同时我们也知道,对于trace结果,oracle提供了tkprof实用程序用来格式化trace文件,提供一份更容易读懂的trace结果。
那么为什么还要直接读取trace文件呢?最重要的是tkprof的结果是不包含绑定变量值的,同时也不包括真正的sql执行顺序,而trace文件中我们则可以看到按照时间排列的parse,binds,executes,fetch等等,这在某西场合下是很有用处的。还有就是,如果你能够直接读取这些让人看得眼晕的trace,是不是会有一种很爽,很大师的感觉:-)
当然如果我们要根据一些标准(比如cpu时长,磁盘读取量等)进行trace中的sql排序,那么tkprof是我们唯一的选择,可以参看coolyl的tkprof工具介绍和分析。
下面是metalink中的这篇文章的大体翻译,大部分名词用英文反而更好,就不强加翻译了,相信大家都看得懂。当然也是比较懒的原因:-)
文本总结了trace结果原始输出文件中的内容。
—————————————————————————-
appname mod=%s mh=%lu act=%s ah=%lu
—————————————————————————-
appname:application name setting。在oracle 7.2和以上版本中出现。这个名称可以由dbms_application_info包来设定。
mod:module name
mh:module hash value
act:action
ah:action hash value
比如:appname mod=sql*plus mh=3669949024 act= ah=4029777240
—————————————————————————-
parsing in cursor #<cursor> len=x dep=x uid=x oct=x lid=x tim=x hv=x ad=x
<statement>
end of stmt
—————————————————————————-
<cursor>:cursor number
len :length of sql statement,sql语句的长度
dep :recursive depth of the cursor,当前sql语句的递规深度,如果为0则表示是用户提交的sql,为1则是由于用户sql而导致oracle后台自己执行的sql,为2则是由1级sql继续诱发的下一级sql。
uid :schema user id of parsing user
oct :oracle command type.
lid :privilege user id.
tim :timestamp。在oracle9i之前单位是1/100秒,9i则是1/1,000,000秒。利用这个值可以计算一个sql执行了到底多长时间。这个值就是当前行被写入trace文件时数据库v$timer视图的值。
hv :hash id.
ad :sqltext address,sqltext的地址,跟v$sqlarea和v$sqltext视图中的address字段值相等。
<statement> :the actual sql statement being parsed.
—————————————————————————-
parse error #%d:len=%ld dep=%d uid=%ld oct=%d lid=%ld tim=%lu err=%d
<statement> …
—————————————————————————-
parse error :在oracle 7.2以上版本中解析的错误会写入trace文件中。
len :length of sql statement.
dep :recursive depth of the statement
uid :user id.
oct :oracle command type (if known).
lid :privilege user id.
tim :timestamp.
err :oracle error code (e.g. ora-xxxxx) reported
<statement> :the sql statement that errored.
—————————————————————————-
parse #<cursor>:c=0,e=0,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=4,tim=0
exec #<cursor>:c=0,e=0,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=4,tim=0
fetch #<cursor>:c=0,e=0,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=4,tim=0
unmap #<cursor>:c=0,e=0,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=4,tim=0
—————————————————————————-
parse :parse a statement. 解析一个sql
exec :execute a pre-parsed statement. 执行已经解析完毕的sql
fetch :fetch rows from a cursor. 从游标中得到数据,通常指select返回记录
unmap :如果游标使用了临时表(temporary table), 当游标关闭的时候将会看到unmap
c :cpu time (100ths of a second in oracle7 ,8 and 9).
e :elapsed time (100ths of a second oracle7, 8. microseconds in oracle 9 onwards).
p :number of physical reads.
cr :number of buffers retrieved for cr reads.
cu :number of buffers retrieved in current mode.
mis :cursor missed in the cache.
r :number of rows processed.
dep :recursive call depth (0 = user sql, >0 = recursive).
og :optimizer goal: 1=all_rows, 2=first_rows, 3=rule, 4=choose
tim :timestamp (large number in 100ths of a second).
比如:fetch #2:c=0,e=106,p=0,cr=3,cu=0,mis=0,r=1,dep=1,og=4,tim=6005498548671
—————————————————————————-
error #%d:err=%d tim=%lu
—————————————————————————-
执行或者fetch之后出现的sql error
err :oracle error code (e.g. ora-xxxxx) at the top of the stack.
tim :timestamp.
—————————————————————————-
stat #<cursor> id=n cnt=0 [pid=0 pos=0 obj=0 op=sort aggregate ]
—————————————————————————-
<cursor>的执行计划.
<cursor> :cursor which the statistics apply to.
id :line of the explain plan which the row count applies to (从1开始).
cnt :number of rows for this row source.
pid :parent id of this row source.
pos :position in explain plan.
obj :object id of row source (if this is a base object).
op : the row source access operation.
比如:
stat #2 id=2 cnt=0 pid=1 pos=1 obj=510 op=table access by index rowid object_usage (cr=2 r=0 w=0 time=83 us)
stat #2 id=3 cnt=1 pid=2 pos=1 obj=511 op=index range scan i_stats_obj# (cr=1 r=0 w=0 time=43 us)
—————————————————————————-
xctend rlbk=%d rd_only=%d
—————————————————————————-
xctend是事务结束的标志.
rlbk :1 if a rollback was performed, 0 if no rollback (commit).
rd_only :1 if transaction was read only, 0 if changes occurred.
—————————————————————————-
binds #%d:
bind 0: dty=2 mxl=22(22) mal=00 scl=00 pre=00 oacflg=03 oacfl2=0 size=24 offset=0
bfp=02fedb44 bln=22 avl=00 flg=05
value=10
—————————————————————————-
bind :variables bound to a cursor.
bind n :the bind position being bound.
dty :data type.
mxl :maximum length of the bind variable (private max len in paren).
mal :array length.
scl :scale.
pre :precision.
oacflg :special flag indicating bind options
oacflg2 :continuation of oacflg
size :amount of memory to be allocated for this chunk
offset :offset into this chunk for this bind buffer
bfp :bind address.
bln :bind buffer length.
avl :actual value length (array length too).
flg :special flag indicating bind status
value :the actual value of the bind variable.
比如:
binds #4:
bind 0: dty=2 mxl=22(22) mal=00 scl=00 pre=00 oacflg=08 oacfl2=1 size=24 offset=0
bfp=ffffffff7ce64ee0 bln=22 avl=01 flg=05
value=0
bind 1: dty=1 mxl=32(11) mal=00 scl=00 pre=00 oacflg=18 oacfl2=1 size=32 offset=0
bfp=ffffffff7ce6b128 bln=32 avl=11 flg=05
value=”tabcompart$”
bind 2: dty=2 mxl=22(22) mal=00 scl=00 pre=00 oacflg=08 oacfl2=1 size=24 offset=0
bfp=ffffffff7ce6bae8 bln=24 avl=02 flg=05
value=1
—————————————————————————-
wait #<cursor>: nam=”<event name>” ela=0 p1=0 p2=0 p3=0
—————————————————————————-
wait :an event that we waited for.
nam :what was being waited for.
ela :elapsed time for the operation.
p1 :p1 for the given wait event.
p2 :p2 for the given wait event.
p3 :p3 for the given wait event.
比如 (full table scan):
wait #1: nam=”db file scattered read” ela= 5 p1=4 p2=1435 p3=25
在游标1上经历了”db file scattered read”等待事件,一共等了0.05秒,在读取file 4,从1435 block开始,读了25个block
比如 (index scan):
wait #1: nam=”db file sequential read” ela= 4 p1=4 p2=1224 p3=1
在游标1上经历了”db file sequential read”等待事件,一共等了0.04秒,在读取file 4,block 1224,读取了这一个block
对于每一个等待事件的含义和p1,p2,p3表示的意思,可以参考oracle database reference文档的oracle wait events章节。
