2011-02-07 16 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中的任何值都將記錄在會話的內存結構中。如果你繼續推動那些東西並且永遠不會把它拿出來(這可能是一些應用服務器連接的情況),你可能會發現在幾天後你的內存中有很多東西。

相關問題