首页
学习
活动
专区
圈层
工具
发布

SQL优化
EN

Stack Overflow用户
提问于 2017-04-13 13:03:07
回答 1查看 73关注 0票数 0

现在我正面临一个优化问题。我有一个17000+的列表,其中一些是不活动的。该列表由客户端提供到EXCEL文件中,他要求我重新发送它们(显然只有那些活动的)。

为此,我必须根据客户提供的列表过滤生产数据库。不幸的是,我无法将列表从生产中加载到一个分隔表中,然后与主项目表连接,但我能够将其加载到一个UAT数据库中,并与产品数据库链接。

生产文章的主数据包含20万000+行,但是过滤它,我可以将其编辑到大约80000行。

我命令只从生产中收回活跃的文章,我在考虑使用集合,但似乎最后一个过滤器要花很长时间。

这是我的代码:

代码语言:javascript
复制
declare
    type t_art is table of number index by pls_integer;
    v_art t_art;
    v_filtered t_art;
    idx number := 0;
begin
    for i in (select * from test_table@UAT_DATABASE)
    loop
    idx := idx + 1;
        v_art(idx) := i.art_nr;
    end loop;

    for j in v_art.first .. v_art.last
    loop
        select distinct art_nr
        bulk collect into v_filtered
        from production_article_master_data 
        where status = 0 -- status is active
        and sperr_stat in (0, 2)
        and trunc(valid_until) >= trunc(sysdate)
        and art_nr = v_art(j);
    end loop;
end;

说明:在UAT数据库中,我通过DBLink将列表插入到生产中的关联数组(v_art)中。然后,对于v_art(17000+ distinct项目)中的每个值,我使用生产项目主数据进行过滤,在第二个ASSOCITIAVE数组中返回,只有有效的项目(可能有6-8000)。

不幸的是,这种过滤操作需要几个小时。有人能给我一些提示吗?如何在orde中改进这一点以缩短执行时间?

谢谢,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-13 13:38:54

只需使用SQL并连接两个表:

代码语言:javascript
复制
select distinct p.art_nr
from   production_article_master_data p
       INNER JOIN
       test_table@UAT_DATABASE t
       ON ( p.art_nr = t.art_nr )
where  status = 0 -- status is active
and    sperr_stat in (0, 2)
and    trunc(valid_until) >= trunc(sysdate)

如果您必须在PL/SQL中这样做,那么:

代码语言:javascript
复制
CREATE OR REPLACE TYPE numberlist IS TABLE OF NUMBER;
/

declare
  -- If you are using Oracle 12c you should be able to declare the
  -- type in the PL/SQL block. In earlier versions you will need to
  -- declare it in the SQL scope instead.

  -- TYPE numberlist IS TABLE OF NUMBER;

  v_art      NUMBERLIST;
  v_filtered NUMBERLIST;
begin
  select art_nr
  BULK COLLECT INTO v_art
  from test_table@UAT_DATABASE;

  select distinct art_nr
  bulk collect into v_filtered
  from production_article_master_data 
  where status = 0 -- status is active
  and sperr_stat in (0, 2)
  and trunc(valid_until) >= trunc(sysdate)
  and art_nr MEMBER OF v_art;
end;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43393109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档