如何获得PL/SQL函数的执行统计信息?

问题描述:

我有一个从我们的Java代码调用的PL/SQL函数。如何获得PL/SQL函数的执行统计信息?

我有执行PL/SQL函数的SQL_ID,我可以访问我的只读DB用户的V $视图。查询需要一段时间才能执行?有没有一种方法可以剖析PL/SQL函数的执行情况,以检查执行卡住的地方?

我知道如何做到这一点的SQL查询V$SQL,V$ACTIVE_SESSION_HISTORYV$SESSION_LONGOPS,但我无法弄清楚如何做到这一点的PL/SQL代码。

PL/SQL函数需要4分钟才能执行,所以我可以在那段时间手动执行相当多的V $查询。我应该检查哪些V $视图以查找执行计划/功能中的一行?这甚至有可能吗?

+3

'有没有一种方法来分析的PL/SQL函数..'?是。 [DBMS_HPROF + plshprof实用工具](https://docs.oracle.com/cd/B28359_01/appdev.111/b28424/adfns_profiler.htm) –

+1

您还应该考虑使用代码 - 您可以登录到表格和/或dbms_application_info - 两者都可以帮助您在运行期间发现问题,而无需执行配置文件(虽然这也可能有用,但在生产中发生问题时通常不可能) – Boneist

+2

此外到DBMS_HPROF,这是旧的DBMS_PROFILER这是更基本的,但不需要服务器文件访问。 (这是内置于[PL/SQL Developer](https://www.allroundautomations.com/plsqldev.html),如果这是您所拥有的工具,使其非常方便。) –

也许你可以为你的问题使用DBMS_PROFILER。但是如果你想使用这种方法,你必须安装一些基础设施。 我不想描述如何安装“proftab.sql”的过程,this link shows how it works.

它还显示了一些快速示例如何跟踪特定功能。 我一些测试后提供我的版本的分析,查询:

select ppr.runid, 
    ppr.run_comment1, 
    decode(nvl(ppd.total_occur, 0), 0, 0, ppd.total_time/ppd.total_occur/1000000) as Avg_msek, 
    ppd.total_time/1000000 totaltime_msek, 
    ppd.total_occur, 
    uc.name, 
    uc.line, 
    uc.text as Source 
    from plsql_profiler_data ppd, plsql_profiler_units ppu, user_source uc, plsql_profiler_runs ppr 
where ppd.runid = ppu.runid 
    and ppu.runid = ppr.runid 
-- and ppr.run_comment1 = 'MYTEST' --show only a specific testrun 
-- and ppr.runid   = (select max(runid) from plsql_profiler_runs) /*to get the last run*/ 
    and ppd.unit_number = ppu.unit_number 
    and ppu.unit_name = uc.name 
    and ppd.line#(+) = uc.line 
    and uc.type in ('PACKAGE BODY', 'TYPE BODY') 
--order by uc.name, uc.line; --Show all code by line 
--order by totaltime_msek desc; --Sort by slowest lines 
order by total_occur desc, avg_msek desc --Sort by calls and slowest ones