简体   繁体   English

有没有一种方法可以优化此查询?

[英]Is there a way to optimize this query?

declare
  n_docid number;
  n_orgid number;
  n_docstateid number;
  n_docstateversion number;
  n_doctypeid number;
  n_count number;
begin
  for I in (select DOCID from DC_EXP_BO where INF_DATEDOCUMENT > '01.01.17' and 
INF_DATEDOCUMENT < '31.12.17')
loop
begin
  select DOCID, DOCSTATEID, DOCTYPEID, DOCSTATE_VERSION into 
  n_docid, n_docstateid, n_doctypeid, n_docstateversion from DOC 
  where DOCID = I.DOCID;

  select ORGID into n_orgid from ORG
  where systemname = (select distinct RB_CODEGRBS from DC_EXP_BO where DOCID = n_docid);


  select count(*) into n_count from ROUTECONTEXT
  where DOCID = n_docid and ORGID = n_orgid;

  if (n_count = 0) then
  insert into ROUTECONTEXT 
    (ROUTECONTEXTID, VERSION, DOCID, LOCALDOCSTATEID, OWNERID, LASTPRINTDATE, PRINTED, RECEIVED, ORGID, ARCHIVE, EXPORTSTATUS, DOCTYPEID, DOCSTATEID, DOCSTATE_VERSION, DELETED)
  values 
    (sq_routeContext.NEXTVAL, 0, n_docid, n_docstateid, null, null, 0, 0, n_orgid, 0, 'NOT_EXPORTED', n_doctypeid, n_docstateid, n_docstateversion, 0);
  end if;
exception
  when no_data_found then
continue;
end;
end loop;
end;
/

We wrote datafix for production. 我们写了datafix用于生产。 And there is a problem. 而且有一个问题。 For statement select can return about 1 million IDs. 对于语句选择,可以返回大约一百万个ID。 Is it possible someway to optimize this query? 是否可以通过某种方式优化此查询?

The most effective optimization you can make is to replace the row-by-row inserts and lookups in a loop with a single set-based operation: 您可以进行的最有效的优化是使用单个基于集合的操作替换循环中的逐行插入和查找:

insert into ROUTECONTEXT 
    (ROUTECONTEXTID, VERSION, DOCID, LOCALDOCSTATEID, OWNERID, LASTPRINTDATE, PRINTED, RECEIVED, ORGID, ARCHIVE, EXPORTSTATUS, DOCTYPEID, DOCSTATEID, DOCSTATE_VERSION, DELETED)
select sq_routeContext.NEXTVAL, 
          0, 
          DOC.DOCID, 
          DOC.DOCSTATEID, 
          null,
          null,
          0,
          0,
          org.ORGID,
          0,
          'NOT_EXPORTED',
          DOC.DOCSTATEID, 
          DOC.DOCTYPEID, 
          DOC.DOCSTATE_VERSION ,
          0
from DC_EXP_BO
    join doc
        on DC_EXP_BO.DOCID = DOC.DOCID 
    join org 
        on org.systemname =  DC_EXP_BO.RB_CODEGRBS 
where DC_EXP_BO.INF_DATEDOCUMENT > date '2017.01.01'
and DC_EXP_BO.INF_DATEDOCUMENT < date '2017.12.31' 
and not exists ( select null 
                 from ROUTECONTEXT
                 where ROUTECONTEXT.DOCID = doc.docid 
                 and ROUTECONTEXT.ORGID = org.orgid 
               )
/

Assuming DC_EXP_BO.INF_DATEDOCUMENT is a DATE datatype, using proper date semantics may be more performative and will certainly be safer. 假设DC_EXP_BO.INF_DATEDOCUMENT是DATE数据类型,则使用适当的日期语义可能更具执行性,并且肯定会更安全。 But it's a string replace that bit of the WHERE clause with what you posted in your question. 但这是一个字符串,用您在问题中发布的内容替换了WHERE子句的这一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM