DBMS_OUTPUT.PUT_LINE

时间:2011-02-07 08:56:30

标签: performance oracle plsql dbms-output

dbms_output.put_line会降低plsql代码中的效果吗?

5 个答案:

答案 0 :(得分:8)

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

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

的问候,
罗布。

答案 1 :(得分:5)

是的,这是需要执行的另一段代码,但除非输出实际打开,否则我认为开销很小。

这是一个AskTom问题,其中包含更多详细信息:Is there a performance impact for dbms_output.put_line statements left in packages?

答案 2 :(得分:4)

如果使用适当的选项编译过程,您可以查看conditional compilation以便DBMS_OUTPUT.PUT_LINE仅在预解析的代码中。

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

答案 3 :(得分: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次操作只记录一次,如:

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;

请注意,自治事务将确保插入日志stmt,即使发生错误并且您回滚其他所有内容(因为它是一个单独的事务)。

希望这有助于...比dbms_output更好;)

答案 4 :(得分:0)

这取决于您调用dbms_output.put_line的次数与您在PL / SQL中执行的操作的次数之比。

相关问题