2017-07-19 11 views
0
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; 
/

製造のためにdatafixを書きました。そして、問題があります。 for文selectは約100万のIDを返します。このクエリを最適化することは可能でしょうか?このクエリを最適化する方法はありますか?

答えて

3

あなたが作ることができる最も効果的な最適化は、単一のセットベースの操作でループ内で行ごとの挿入と検索を置き換えることです:

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

DC_EXP_BO.INF_DATEDOCUMENTが適切に使用して、DATEデータ型であると仮定すると、日付セマンティクスはより実践的であり、確かに安全です。しかし、それはあなたの質問に投稿したWHERE句の代わりに文字列です。

+0

これは機能しました。十分な評判がないので、私はあなたのカルマに+を加えることはできません。 –

+0

あなたは質問をしました。自分の質問に投票して回答を受け入れることができます。 – APC

+0

@APC:Nice Approach !!! –

関連する問題