Oracle PLSQL长时间运行问题

huangapple go评论60阅读模式
英文:

Oracle PLSQL long run issue

问题

有以下代码长期问题,请求协助纠正。提前致以感谢。

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;

上述代码存在长期问题,需要简单和容易地进行修正。

<details>
<summary>英文:</summary>

there is long run issue with below code kindly help to rectify this issue. Thanks in advance.


    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(&#39;Aggregation[table]&#39;)}} partition ({{getSourcePartitionNameLatest(&#39;Aggregation[table]&#39;)}});
    
    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(&#39;Aggregation[table]&#39;)}} partition ({{getSourcePartitionNameLatest(&#39;Aggregation[table]&#39;)}}) AGG,
              {{sourceTableLatest(&#39;mfu_table&#39;)}} partition ({{getSourcePartitionNameLatest(&#39;mfu_table&#39;)}}) 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;



the above code take long run issue need to rectify the issue with simple and easy.

</details>


# 答案1
**得分**: 2

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

```sql
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)
英文:

You don't need a loop, you can run it with a single UPDATE statement. Apart from that, you should switch to modern (well, it exists for more than 20 years) ANSI join syntax.

UPDATE @table_name SET ENTRP_CUST_ID_NUM = ( 
SELECT mfu_source.Enterprise_Customer_ID 
FROM {{sourceTableLatest(&#39;Aggregation[table]&#39;)}} PARTITION ({{getSourcePartitionNameLatest(&#39;Aggregation[table]&#39;)}}) AGG
JOIN {{sourceTableLatest(&#39;mfu_table&#39;)}} PARTITION ({{getSourcePartitionNameLatest(&#39;mfu_table&#39;)}}) 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 &#39;x&#39; 
FROM {{sourceTableLatest(&#39;Aggregation[table]&#39;)}} PARTITION ({{getSourcePartitionNameLatest(&#39;Aggregation[table]&#39;)}}) AGG
JOIN {{sourceTableLatest(&#39;mfu_table&#39;)}} PARTITION ({{getSourcePartitionNameLatest(&#39;mfu_table&#39;)}}) 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)

答案2

得分: 1

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

申明
l_status varchar2(10);
开头
插入到 @table_name (ENTRP_CUST_ID_NUM, CUST_SROGT_ID, TRD_REF_NUM, CNTRY_CD)
选择 ENTRP_CUST_ID_NUM, CUST_SROGT_ID, TRD_REF_NUM, CNTRY_CD 
从   {{sourceTableLatest(&#39;Aggregation[table]&#39;)}} 分区 ({{getSourcePartitionNameLatest(&#39;Aggregation[table]&#39;)}});
合并到 @table_name dst
使用 (
选择 mfu_source.Customer_Surrogate_ID CUST_SROGT_ID,
Agg.TRD_REF_NUM TRD_REF_NUM,
mfu_source.Enterprise_Customer_ID ENTRP_CUST_ID_NUM
从   {{sourceTableLatest(&#39;Aggregation[table]&#39;)}} 分区 ({{getSourcePartitionNameLatest(&#39;Aggregation[table]&#39;)}}) AGG
内连接 {{sourceTableLatest(&#39;mfu_table&#39;)}} 分区 ({{getSourcePartitionNameLatest(&#39;mfu_table&#39;)}}) mfu_source
在    mfu_source.TA_LINK_ID     = AGG.TRD_REF_NUM
或 mfu_source.TRANSACTION_ID = AGG.TRD_REF_NUM
其中  去除空格(AGG.ENTRP_CUST_ID_NUM) 是 空
) src
在 dst.TRD_REF_NUM = src.TRD_REF_NUM
当 匹配时 然后
更新
设置 dst.ENTRP_CUST_ID_NUM = src.ENTRP_CUST_ID_NUM;
提交;
结尾;
/ 
英文:

You can use a single MERGE statement (rather than multiple statements in a loop):

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(&#39;Aggregation[table]&#39;)}} partition ({{getSourcePartitionNameLatest(&#39;Aggregation[table]&#39;)}});
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(&#39;Aggregation[table]&#39;)}} partition ({{getSourcePartitionNameLatest(&#39;Aggregation[table]&#39;)}}) AGG
INNER JOIN {{sourceTableLatest(&#39;mfu_table&#39;)}} partition ({{getSourcePartitionNameLatest(&#39;mfu_table&#39;)}}) 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;
/

huangapple
  • 本文由 发表于 2023年6月19日 15:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定