Oracle PLSQL长期运行问题

6uxekuva  于 2023-06-22  发布在  Oracle
关注(0)|答案(2)|浏览(181)

下面的代码有一个长期运行的问题,请帮助纠正这个问题。先谢谢你了。

Declare
l_status varchar2(10);

Begin

Insert into @table_name (ENTRP_CUST_ID_NUM, CUST_SROGT_ID, TRD_REF_NUM, CNTRY_CD)
  Select ENTRP_CUST_ID_NUM, CUST_SROGT_ID, TRD_REF_NUM, CNTRY_CD 
           from {{sourceTableLatest('Aggregation[table]')}} partition ({{getSourcePartitionNameLatest('Aggregation[table]')}});

Commit;

For i in ( Select mfu_source.Customer_Surrogate_ID CUST_SROGT_ID, Agg.TRD_REF_NUM TRD_REF_NUM, mfu_source.Enterprise_Customer_ID ENTRP_CUST_ID_NUM
          from {{sourceTableLatest('Aggregation[table]')}} partition ({{getSourcePartitionNameLatest('Aggregation[table]')}}) AGG,
          {{sourceTableLatest('mfu_table')}} partition ({{getSourcePartitionNameLatest('mfu_table')}}) mfu_source
         Where (mfu_source.TA_LINK_ID=AGG.TRD_REF_NUM or mfu_source.TRANSACTION_ID=AGG.TRD_REF_NUM)
         and trim(AGG.ENTRP_CUST_ID_NUM) is null) 
         Loop
         
         
    Update @table_name set ENTRP_CUST_ID_NUM= i.ENTRP_CUST_ID_NUM where TRD_REF_NUM=i.TRD_REF_NUM;
    
    Commit;
End Loop;

END;

上面的代码需要长时间运行的问题,需要纠正的问题简单易行。

j9per5c4

j9per5c41#

你不需要一个循环,你可以用一个UPDATE语句来运行它。除此之外,您应该切换到现代的(嗯,它已经存在了20多年)ANSI连接语法。

UPDATE @table_name SET ENTRP_CUST_ID_NUM = ( 
    SELECT mfu_source.Enterprise_Customer_ID 
        FROM {{sourceTableLatest('Aggregation[table]')}} PARTITION ({{getSourcePartitionNameLatest('Aggregation[table]')}}) AGG
            JOIN {{sourceTableLatest('mfu_table')}} PARTITION ({{getSourcePartitionNameLatest('mfu_table')}}) mfu_source ON mfu_source.TA_LINK_ID = AGG.TRD_REF_NUM OR mfu_source.TRANSACTION_ID = AGG.TRD_REF_NUM
    WHERE TRIM(AGG.ENTRP_CUST_ID_NUM) IS NULL 
        AND TRD_REF_NUM = Agg.TRD_REF_NUM)
WHERE EXISTS 
    (SELECT 'x' 
        FROM {{sourceTableLatest('Aggregation[table]')}} PARTITION ({{getSourcePartitionNameLatest('Aggregation[table]')}}) AGG
            JOIN {{sourceTableLatest('mfu_table')}} PARTITION ({{getSourcePartitionNameLatest('mfu_table')}}) mfu_source ON mfu_source.TA_LINK_ID = AGG.TRD_REF_NUM OR mfu_source.TRANSACTION_ID = AGG.TRD_REF_NUM
    WHERE TRIM(AGG.ENTRP_CUST_ID_NUM) IS NULL 
        AND TRD_REF_NUM = Agg.TRD_REF_NUM)
slwdgvem

slwdgvem2#

您可以使用单个MERGE语句(而不是循环中的多个语句):

Declare
  l_status varchar2(10);
Begin
  Insert into @table_name (ENTRP_CUST_ID_NUM, CUST_SROGT_ID, TRD_REF_NUM, CNTRY_CD)
  Select ENTRP_CUST_ID_NUM, CUST_SROGT_ID, TRD_REF_NUM, CNTRY_CD 
  from   {{sourceTableLatest('Aggregation[table]')}} partition ({{getSourcePartitionNameLatest('Aggregation[table]')}});

  MERGE INTO @table_name dst
  USING (
    Select mfu_source.Customer_Surrogate_ID CUST_SROGT_ID,
           Agg.TRD_REF_NUM TRD_REF_NUM,
           mfu_source.Enterprise_Customer_ID ENTRP_CUST_ID_NUM
    from   {{sourceTableLatest('Aggregation[table]')}} partition ({{getSourcePartitionNameLatest('Aggregation[table]')}}) AGG
           INNER JOIN {{sourceTableLatest('mfu_table')}} partition ({{getSourcePartitionNameLatest('mfu_table')}}) mfu_source
           ON    mfu_source.TA_LINK_ID     = AGG.TRD_REF_NUM
              or mfu_source.TRANSACTION_ID = AGG.TRD_REF_NUM
    Where  trim(AGG.ENTRP_CUST_ID_NUM) is null
  ) src
  ON dst.TRD_REF_NUM = src.TRD_REF_NUM
  WHEN MATCHED THEN
    UPDATE
    SET dst.ENTRP_CUST_ID_NUM = src.ENTRP_CUST_ID_NUM;

  Commit;
END;
/

相关问题