1

我真的是新来的,我真的需要帮助。我正在尝试根据从另一个 select 语句连接的数据更新表。

SELECT DISTINCT A.business_unit_AP
                , A.VOUCHER_ID
                , A.JOURNAl_ID
                , A.UNPOST_SEQ
                , A.APPL_JRNL_ID
                , A.PYMNT_CNT
                , A.VOUCHER_LINE_NUM
                , A.DISTRIB_LINE_NUM,A.dst_acct_type
                , A.LEDGER
                , A.PROCESS_INSTANCE 
FROM PS_PROJ_RES_TMP A 
LEFT OUTER JOIN ps_proj_res_cal_vw B
     ON a.business_unit_ap = B.BUSINESS_UNIT_AP
     AND B.PROJECT_ID = A.PROJECT_ID
     AND A.ACTIVITY_ID = B.ACTIVITY_ID 
     AND A.RESOURCE_ID = B.RESOURCE_ID
WHERE A.SYSTEM_SOURCE ='BAP'
     AND b.business_unit_ap is null*

所以基本上上面的语句从 PS_PROJ_RES_TMP 表中选择 PS_PROJ_RES_CAL_VW 中不存在的数据。我对吗?然后根据拉取的数据,我再更新 PS_VCHR_ACCTG_LINE。

我制定了这个脚本,但它花了太长时间。我最终更新了整个表格。

UPDATE PS_VCHR_ACCTG_LINE C 
SET C.PC_DISTRIB_STATUS = 'N' 
WHERE EXISTS 
      (
         SELECT DISTINCT A.business_unit_AP
               , A.VOUCHER_ID
               , A.JOURNAl_ID
               , A.UNPOST_SEQ
               , A.APPL_JRNL_ID
               , A.PYMNT_CNT
               , A.VOUCHER_LINE_NUM
               , A.DISTRIB_LINE_NUM
               , A.dst_acct_type
               , A.LEDGER
               , A.PROCESS_INSTANCE 
          FROM PS_PROJ_RES_TMP A 
          LEFT OUTER JOIN ps_proj_res_cal_vw B
              ON a.business_unit_ap = B.BUSINESS_UNIT_AP
              AND B.PROJECT_ID = A.PROJECT_ID
              AND A.ACTIVITY_ID = B.ACTIVITY_ID 
              AND A.RESOURCE_ID = B.RESOURCE_ID
          WHERE b.business_unit_ap is null
          AND b.PROJECT_ID  is null
          AND b.ACTIVITY_ID is null
          AND b.RESOURCE_ID is null
          AND C.BUSINESS_UNIT = a.business_unit_ap
          AND C.VOUCHER_ID = A.VOUCHER_ID
          AND C.unpost_seq = A.unpost_seq
          AND c.appl_jrnl_id = A.appl_jrnl_id
          AND c.PYMNT_CNT = A.pymnt_cnt
          AND C.voucher_line_num = A.voucher_line_num
          AND C.distrib_line_num = A.distrib_line_num
          AND C.dst_acct_type = A.dst_acct_type
          AND C.ledger = A.ledger
          AND A.SYSTEM_SOURCE ='BAP'
   );

我哪里做错了?非常感谢你的帮助 :)

4

1 回答 1

0

局外人几乎不可能找出您的查询的问题。更新所有行的问题似乎是因为查询中的逻辑,在表中的数据上,导致所有行都匹配。

您可以做一件简单的事情来提高性能。使用distinct时不需要存在。所以以下应该是等价的:

UPDATE PS_VCHR_ACCTG_LINE C 
SET C.PC_DISTRIB_STATUS = 'N' 
WHERE EXISTS 
      (
         SELECT 1
          FROM PS_PROJ_RES_TMP A 
          LEFT OUTER JOIN ps_proj_res_cal_vw B
              ON a.business_unit_ap = B.BUSINESS_UNIT_AP
              AND B.PROJECT_ID = A.PROJECT_ID
              AND A.ACTIVITY_ID = B.ACTIVITY_ID 
              AND A.RESOURCE_ID = B.RESOURCE_ID
          WHERE b.business_unit_ap is null
          AND b.PROJECT_ID  is null
          AND b.ACTIVITY_ID is null
          AND b.RESOURCE_ID is null
          AND C.BUSINESS_UNIT = a.business_unit_ap
          AND C.VOUCHER_ID = A.VOUCHER_ID
          AND C.unpost_seq = A.unpost_seq
          AND c.appl_jrnl_id = A.appl_jrnl_id
          AND c.PYMNT_CNT = A.pymnt_cnt
          AND C.voucher_line_num = A.voucher_line_num
          AND C.distrib_line_num = A.distrib_line_num
          AND C.dst_acct_type = A.dst_acct_type
          AND C.ledger = A.ledger
          AND A.SYSTEM_SOURCE ='BAP';

如果您的原始查询运行得很快,那么我建议将结果放在一个临时表中并将其用于更新。

我推测性能问题实际上是由于ps_proj_res_cal_vw,我进一步推测这是某种观点。MySQL 在优化视图方面并不总是做得很好。

于 2013-08-12T02:18:15.023 回答