启用tkprof
SQL> alter session set timed_statistics=true;
会话已更改。
SQL> alter session set events
'10046 trace name context forever,level 12';
会话已更改。
tkprof tracefile outputfile [explain= ] [table= ] [print= ] [insert= ] [sys= ] [sort= ] ...
参数说明:
tracefile:你要分析的trace文件
outputfile:格式化后的文件
explain=user/password@connectstring
table=schema.tablename
注1:这两个参数是一起使用的,通过连接数据库对在trace文件中出现的每条sql语句查看执行计划,并将之输出到outputfile中
注2:该table必须是数据库中不存在的,如果存在会报错
print=n:只列出最初N个sql执行语句
insert=filename:会产生一个sql文件,运行此文件可将收集到的数据insert到数据库表中
sys=no:过滤掉由sys执行的语句
record=filename:可将非嵌套执行的sql语句过滤到指定的文件中去
waits=yes|no:是否统计任何等待事件
aggregate=yes|no:是否将相同sql语句的执行信息合计起来,默认为yes
sort= option:设置排序选项,选项如下:
prscnt:number of times parse was called
prscpu:cpu time parsing
prsela:elapsed time parsing
prsdsk:number of disk reads during parse
prsqry:number of buffers for consistent read during parse
prscu:number of buffers for current read during parse
prsmis:number of misses in library cache during parse
execnt:number of execute was called
execpu:cpu time spent executing
exeela:elapsed time executing
exedsk:number of disk reads during execute
exeqry:number of buffers for consistent read during execute
execu:number of buffers for current read during execute
exerow:number of rows processed during execute
exemis:number of library cache misses during execute
fchcnt:number of times fetch was called
fchcpu:cpu time spent fetching
fchela:elapsed time fetching
fchdsk:number of disk reads during fetch
fchqry:number of buffers for consistent read during fetch
fchcu:number of buffers for current read during fetch
fchrow:number of rows fetched
userid:userid of user that parsed the cursor
可根据自己的需要设置排序
举例:
1.列出前2条sql语句的执行情况:
C:/>tkprof c:/oc_ora_2892.trc c:/cc.txt print=2
2.将数据保存到数据库:
C:/>tkprof c:/oc_ora_2892.trc c:/cc.txt insert=c:/insert.sql
执行后会在c:/产生insert.sql文件,执行该文件即可将数据保存到数据库,以下为insert.sql部分内容:
REM Edit and/or remove the following CREATE TABLE
REM statement as your needs dictate.
CREATE TABLE tkprof_table
(
date_of_insert DATE
,cursor_num NUMBER
,depth NUMBER
,user_id NUMBER
,parse_cnt NUMBER
,parse_cpu NUMBER
,parse_elap NUMBER
,parse_disk NUMBER
,parse_query NUMBER
,parse_current NUMBER
,parse_miss NUMBER
,exe_count NUMBER
,exe_cpu NUMBER
,exe_elap NUMBER
,exe_disk NUMBER
,exe_query NUMBER
,exe_current NUMBER
,exe_miss NUMBER
,exe_rows NUMBER
,fetch_count NUMBER
,fetch_cpu NUMBER
,fetch_elap NUMBER
,fetch_disk NUMBER
,fetch_query NUMBER
,fetch_current NUMBER
,fetch_rows NUMBER
,ticks NUMBER
,sql_statement LONG
);
INSERT INTO tkprof_table VALUES
(
SYSDATE, 1, 0, 61, 1, 0, 418, 0, 0, 0, 1
, 1, 15625, 1435, 0, 0, 0, 0, 0
, 4, 0, 4417, 0, 24, 0, 36, 13450151
, ’select * from tblinventoryhistory
‘);
3.提取sql执行语句:
C:/>tkprof c:/oc_ora_2892.trc c:/cc.txt record=sqlstr.sql
sqlstr.sql中的内容:
alter session set sql_trace=true ;
alter session set events '10046 trace name context forever,level 12';
select * from tblinventoryhistory ;
select * from tblorder ;
select * from tblproduct ;
select * from tbluser ;
select * from tblroute ;
4.产生执行计划:
C:/>tkprof c:/oc_ora_2892.trc c:/ff.txt explain=ocuser/ocuser table=ocuser.test1
在产生的ff.txt文件中会体现其执行计划:
Rows Execution Plan
——- —————————————————
0 SELECT STATEMENT GOAL: CHOOSE
0 TABLE ACCESS (FULL) OF 'TBLROUTE'