暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle PL/SQL性能调优

askTom 2018-04-12
217

问题描述

我的存储过程搜索潜在的重复的PeopleSoft凭证 (PS_VOUCHER_LINE,PS_DISTRIB_LINE,PS_VENDOR,PS_PYMNT_VCHR_XREF)。
第一步是获取当前正在处理但尚未完全处理的记录的唯一级联/分隔值的集合 (我只考虑表中的总记录的有限集合)。
然后我解析出单独的值w/分隔符。
然后我的FOR/循环对所有记录 (不仅仅是记录在处理中) 的值进行评估。我有另一个FOR/循环,在主FOR/循环中,检查以确定是否有2个或更多 (在整个表内)。

问题: 确定是否有2个或更多的评估是 “资源猪”。

问题1: 如果我显式打开/关闭游标 (而不是FOR/循环),这会减少运行时间,因为游标将在缓存中?
问题2: 如果我更改为一个包,这会减少运行时间,因为它会在运行时被缓存?

专家解答

如果将代码重构为批量处理SQL,您可能会看到的最大好处。

例如,考虑一下:

SQL> create table t1 as select substr(owner,1,1) x, substr(object_name,1,2) y, d.* from dba_objects d where rownum < 1000;

Table created.

SQL> create table t2 as select substr(object_name,1,2) x, substr(owner,1,1) y, d.* from dba_objects d;

Table created.
复制


我想看到T1中的x | | y与t2中的x | | y冲突的发生。

我可以这样写PL/SQL:

SQL> set timing on
SQL> set serverout on
SQL> declare
  2    c int;
  3  begin
  4  for i in ( select * from t1 ) loop
  5    select count(*)
  6    into   c
  7    from t2
  8    where  x||y = i.x||i.y;
  9
 10    if c > 0 then
 11      dbms_output.put_line('Clash with x/y for '||i.x||i.y);
 12    end if;
 13  end loop;
 14  end;
 15  /

...
...
Clash with x/y for SAP
Clash with x/y for SAP
Clash with x/y for SAP
Clash with x/y for SAP

PL/SQL procedure successfully completed.

Elapsed: 00:00:07.81
复制


这是一排的检查。

或者我可以把逻辑放到一个单一的SQL...

SQL> begin
  2  for i in (
  3    select t1.x, t1.y
  4    from   t1
  5    where  x||y in ( select x||y from t2 )
  6    )
  7  loop
  8    dbms_output.put_line('Clash with x/y for '||i.x||i.y);
  9  end loop;
 10  end;
 11  /
...
...
Clash with x/y for SAP
Clash with x/y for SAP
Clash with x/y for SAP
Clash with x/y for SAP

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.12
复制


速度快了数百倍。

对于您的其他问题:

Q1: 否
Q2: 否
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论