2011-02-07 20 views

回答

0

这取决于你有多少次打电话dbms_output.put_line与你在PL/SQL做些什么的比率。

+1

另一个重要因素是使用什么样的表达式来构建参数,例如, `dbms_output.put_line(my_slow_function())` – 2011-02-08 00:49:49

7

每个额外的一行代码会降低代码的性能。毕竟,这是一个额外的指令被执行,至少会消耗一些CPU。所以是的,dbms_output.put_line会降低性能。

真正的问题是:这个额外的代码行的好处是否超过了性能损失?只有你可以回答那个问题。

Regards,
Rob。

2

我使用日志表而不是dbms_output。确保设置为自动交易,类似(当然修改您的需要):

create or replace package body somePackage as 
... 
procedure ins_log(
i_msg in varchar2, 
i_msg_type in varchar2, 
i_msg_code in number default 0, 
i_msg_context in varchar2 default null 
) IS PRAGMA AUTONOMOUS_TRANSACTION; 

begin 

    insert into myLogTable 
    (
    created_date, 
    msg, 
    msg_type, 
    msg_code, 
    msg_context 
) 
    values 
    (
    sysdate, 
    i_msg, 
    i_msg_type, 
    i_msg_code, 
    i_msg_context 
); 

    commit; 

end ins_log; 
... 

end; 

确保您创建您的日志表当然。在你的代码,如果你在一个循环做多操作,您可能希望只按X NUM操作,类似登录一次:

create or replace myProcedure as 
    cursor some_cursor is 
    select * from someTable; 

    v_ctr pls_integer := 0; 

begin 

for rec in some_cursor 
loop 
    v_ctr := v_ctr + 1; 

    -- do something interesting 

    if (mod(v_ctr, 1000) = 0) then 
    somePackage.ins_log('Inserted ' || v_ctr || ' records', 
         'Log', 
         i_msg_context=>'myProcedure'); 
    end if; 

end loop; 
commit; 

exception 
    when others then 
    somePackage.ins_log(SQLERRM, 'Err', i_msg_context=>'myProcedure'); 
    rollback; 
    raise; 
end; 

注意,自治事务将确保日志语句被插入,即使发生错误并且您还原其他所有内容(因为它是单独的事务)。

希望这有助于...比DBMS_OUTPUT好得多;)

+0

这通常是一个很好的解决方案,但由于原始的海报关注开销,这可能会更糟糕。与对`dbms_output.put_line`的调用相比,自治事务处理花费的时间要长得多。 – Allan 2011-02-07 19:03:51

4

你可以看看conditional compilation,使DBMS_OUTPUT.PUT_LINE仅在预解析的代码,如果该程序与相应的选项编译。

一个问题是,是否调用了DBMS_OUTPUT.ENABLE。 如果是这样,DBMS_OUTPUT.PUT_LINE中的任何值都将记录在会话的内存结构中。如果你继续推动那些东西并且永远不会把它拿出来(这可能是一些应用服务器连接的情况),你可能会发现在几天后你的内存中有很多东西。

相关问题