dbms_output.put_line

发布于 2024-10-15 23:33:33 字数 72 浏览 4 评论 0原文

dbms_output.put_line 是否会降低 plsql 代码的性能?

Does dbms_output.put_line decrease the performance in plsql code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

追风人 2024-10-22 23:33:33

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

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

问候,
抢。

Every extra line of code decreases the performance of code. After all, it is an extra instruction to be executed, which at least consumes some CPU. So yes, dbms_output.put_line decreases the performance.

The real question is: does the benefit of this extra line of code outweigh the performance penalty? Only you can answer that question.

Regards,
Rob.

阳光①夏 2024-10-22 23:33:33

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

这是一个包含更多详细信息的 AskTom 问题: 包中保留的 dbms_output.put_line 语句对性能有影响吗?

Yes, it's another piece of code that needs to be executed, but unless the output is actually turned on, I think the overhead is quite minimal.

Here's an AskTom question with more details: Is there a performance impact for dbms_output.put_line statements left in packages?

壹場煙雨 2024-10-22 23:33:33

您可以查看条件编译,以便DBMS_OUTPUT.PUT_LINE仅在如果过程是使用适当的选项编译的,则为预解析的代码。

一个问题是,DBMS_OUTPUT.ENABLE 是否被调用。
如果是这样,DBMS_OUTPUT.PUT_LINE 中的任何值都将记录在会话的内存结构中。如果您继续将内容推入其中而不将其取出(某些应用程序服务器连接可能会出现这种情况),您可能会发现几天后内存中有很多内容。

You can look into conditional compilation so that the DBMS_OUTPUT.PUT_LINE are only in the pre-parsed code if the procedure is compiled with the appropriate option.

One question is, has DBMS_OUTPUT.ENABLE been called.
If so, any value in a DBMS_OUTPUT.PUT_LINE will be recorded in the session's memory structure. If you continue pushing stuff in there and never taking it out (which might be the case with some application server connections) you might find that after a few days you have a LOT of stuff in memory.

深海里的那抹蓝 2024-10-22 23:33:33

我使用日志表而不是 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;

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

希望这有帮助...比 dbms_output 好得多;)

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 ;)

○闲身 2024-10-22 23:33:33

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

It depends on the ratio of how many times you call dbms_output.put_line versus what else you do in PL/SQL.

凉城凉梦凉人心 2024-10-22 23:33:33

使用 DMBS_OUTPUT 也可能是导致以下错误的原因:

ORA-04036: 实例使用的 PGA 内存超出 PGA_AGGREGATE_LIMIT

Using DMBS_OUTPUT might also be the cause of the following error:

ORA-04036: PGA memory used by the instance exceeds PGA_AGGREGATE_LIMIT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文