基于另外两个表的子选择更新表

时间:2013-08-12 01:33:03

标签: mysql

我真的很新,我真的需要帮助。我正在尝试根据从另一个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_TAL表中选择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'
   );

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

1 个答案:

答案 0 :(得分: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在优化视图方面并不总是做得很好。