DBMS_PROFILER包专门分析plsql,可以分析到每行,非常有用的工具。
这个包默认不安装需要手动安装,脚本为?\rdbms\admin下的profload.sql和proftab.sql,第二个为工具表的sql。
安装完成就会有这个包
SQL> desc dbms_profiler
FUNCTION FLUSH_DATA RETURNS BINARY_INTEGER
PROCEDURE FLUSH_DATA
PROCEDURE GET_VERSION
参数名称 类型 输入/输出默认值?
------------------------------ ----------------------- ------ --------
MAJOR BINARY_INTEGER OUT
MINOR BINARY_INTEGER OUT
FUNCTION INTERNAL_VERSION_CHECK RETURNS BINARY_INTEGER
FUNCTION PAUSE_PROFILER RETURNS BINARY_INTEGER
PROCEDURE PAUSE_PROFILER
FUNCTION RESUME_PROFILER RETURNS BINARY_INTEGER
PROCEDURE RESUME_PROFILER
PROCEDURE ROLLUP_RUN
参数名称 类型 输入/输出默认值?
------------------------------ ----------------------- ------ --------
RUN_NUMBER NUMBER IN
PROCEDURE ROLLUP_UNIT
参数名称 类型 输入/输出默认值?
------------------------------ ----------------------- ------ --------
RUN_NUMBER NUMBER IN
UNIT NUMBER IN
FUNCTION START_PROFILER RETURNS BINARY_INTEGER
。。。。。
写一个过程作为测试用,这里把output注释掉,为了输出看起来方便写
SQL> create or replace procedure array_process is
2 type numlist is table of t%rowtype;
3 r numlist;
4 rf sys_refcursor;
5 a number := 0;
6 begin
7 open rf for select * from t;
8 loop
9 fetch rf bulk collect into r limit 16;
10 exit when rf%notfound;
11 for j in r.first .. r.last loop
12 a := a + 1;
13 end loop;
14 end loop;
15 -- dbms_output.put_line(a);
16 end;
17 /
过程已创建。
执行下面的匿名块,分析array_process
SQL> declare
2 l_result binary_integer;
3 begin
4 l_result := dbms_profiler.start_profiler(run_comment=>'ARRAY_PROCESS:'||sys
date);
5 array_process;
6 l_result := dbms_profiler.stop_profiler;
7 end;
8 /
PL/SQL 过程已成功完成。
分析完成后执行下面的语句,刚才的分析结果在左后(runid=4)
SQL> set linesize 200
SQL> set trimout on
SQL> column runid for 99999
SQL> col run_comment for a33
SQL> col run_total_time for 9999999999999999
SQL>
SQL> select runid,
2 run_date,
3 run_comment,
4 run_total_time
5 from plsql_profiler_runs
6 order by runid;
RUNID RUN_DATE RUN_COMMENT RUN_TOTAL_TIME
------ -------------- --------------------------------- -----------------
1 23-11月-11 ARRAY_PROCESS:23-11月-11 282747156563
2 23-11月-11 ARRAY_PROCESS:23-11月-11 118532195921
3 23-11月-11 ARRAY_PROCESS:23-11月-11 120801539596
4 23-11月-11 ARRAY_PROCESS:23-11月-11 118431468798
之后就可以查看分析的详细信息
col unit_number for 99999
col unit_type for a20
col unit_owner for a12
col unit_name for a12
select u.runid, u.unit_number, u.unit_type, u.unit_owner, u.unit_name,
d.line#, d.total_occur, d.total_time, d.min_time, d.max_time
from plsql_profiler_units u
join plsql_profiler_data d on u.runid = d.runid and u.unit_number = d.unit_number
where u.runid = 4
order by u.unit_number, d.line#
/
执行上面的sql,输出的结果很多,spool到一个文件里查看方便些
下面就是分析结果的一部分,line为过程中的每一行,total_occur为这一行执行的次数,执行次数比较多的是11、12行
NIT_NUMBER UNIT_NAME LINE# TOTAL_OCCUR TOTAL_TIME MIN_TIME MAX_TIME
----------- ------------ ---------- ----------- ---------- ---------- ----------
R
2 1 0 0 0 0
2 4 0 0 0 0
2 5 2 27955376 1270348 26685027
2 6 1 4796529 4796529 4796529
2 7 0 0 0 0
3 ARRAY_PROCES 1 1 19247711 19247711 19247711
S
3 ARRAY_PROCES 5 1 523537 523537 523537
S
3 ARRAY_PROCES 7 1 119251121 631324 118619796
S
3 ARRAY_PROCES 9 626 1.0052E+11 73503160 1190478652
S
3 ARRAY_PROCES 10 626 428546445 600528 3287509
S
3 ARRAY_PROCES 11 10625 4228217731 161680 152383948
S
3 ARRAY_PROCES 12 10000 3431197130 261768 3264411
S
3 ARRAY_PROCES 16 1 59036580 59036580 59036580
S
下的的sql可以把该过程的代码输出,方便查看
SQL> select line || ':' || text
2 from all_source
3 where wner='SYS'
4 and type='PROCEDURE'
5 and name = 'ARRAY_PROCESS'
6 /
LINE||':'||TEXT
-----------------------------------------------
-----------------------------------------------
----------------------------------------
1:procedure array_process is
2: type numlist is table of t%rowtype;
3: r numlist;
4: rf sys_refcursor;
5: a number := 0;
6:begin
7: open rf for select * from t;
8: loop
9: fetch rf bulk collect into r limit 16;
10: exit when rf%notfound;
11: for j in r.first .. r.last loop
12: a := a + 1;
13: end loop;
14: end loop;
15: -- dbms_output.put_line(a);
16:end;
已选择16行。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25361369/viewspace-711849/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/25361369/viewspace-711849/