dbms_output.put_line

前端 未结 5 1275
旧时难觅i
旧时难觅i 2020-11-28 15:23

Does dbms_output.put_line decrease the performance in plsql code?

5条回答
  •  囚心锁ツ
    2020-11-28 16:12

    I use a log table instead of dbms_output. Make sure to setup as autonomous transaction, something like (modify for your needs of course):

    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;
    

    Make sure you create your log table of course. In your code, if you're doing many operations in a loop, you may want to only log once per x num operations, something like:

    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;
    

    Note that the autonomous transaction will ensure that your log stmt gets inserted, even if an error occurs and you rollback everything else (since its a separate transaction).

    Hope this helps...much better than dbms_output ;)

提交回复
热议问题