MS SQL服务器更新需要太长时间

时间:2017-02-13 15:57:13

标签: sql-server sql-update

我有一个“更新”语句,包括一些内部联接。我在400K行上运行它需要大约11分钟来执行,这太长了。我在Access db上运行相同的语句,需要2分钟。有什么方法可以提高速度吗?

UPDATE AA 
SET 
AA.Status_Flag = mapper.Status_Flag, 
AA.Review_Required_Flag = mapper.Review_Required_Flag, 
AA.Exemption = mapper.CF_BB_Exemption, AA.Bloomberg_Flag = 'True',
AA.Bloomberg_Classification = mapper.LOGIC_IND 
FROM (AA INNER JOIN  
(SELECT * FROM tbl_BBG_Mapping inner join tbl_BBG_Out ON[tbl_BBG_Out].LOGIC_IND = tbl_BBG_Mapping.Status ) 
AS mapper ON AA.CUSIP = mapper.ID_CUSIP) 
INNER JOIN tbl_SAG_Diff ON
(AA.MSD_ID = tbl_SAG_Diff.MSD_ID) AND(AA.PRODUCT_ID = tbl_SAG_Diff.PRODUCT_ID)
WHERE AA.Added_Date = tbl_SAG_Diff.RUN_DATE;

1 个答案:

答案 0 :(得分:1)

不要join周围使用括号,而不记录为什么。它会对查询的运行方式产生重大影响,如果您不知道自己为什么这么做,就根本不要这样做。它相当于使用查询提示来强制连接顺序。

Forcing Join Order Without Hints - Erik Darling

使用Paste The Plan @ brentozar.com

分享您的表架构,表格大小,分享您的执行计划

不知道您的表架构和相关的DDL,或者看到执行计划......

update AA set 
     AA.Status_Flag              = mapper.Status_Flag
   , AA.Review_Required_Flag     = mapper.Review_Required_Flag
   , AA.Exemption                = mapper.CF_BB_Exemption
   , AA.Bloomberg_Flag           = 'True'
   , AA.Bloomberg_Classification = bbgOut.LOGIC_IND
from AA
  inner join tbl_SAG_Diff as sd
     on AA.MSD_ID     = sd.MSD_ID 
    and AA.PRODUCT_ID = sd.PRODUCT_ID
    /* moved this from the where to the join */
    and AA.Added_Date = sd.RUN_DATE 

    /* join the tables instead of this query, update aliases as needed in `set` */
    /* inner join ( 
     select *
     from tbl_BBG_Mapping
      inner join tbl_BBG_Out 
        on [tbl_BBG_Out].LOGIC_IND = tbl_BBG_Mapping.status
     ) as mapper 
         on AA.CUSIP = mapper.ID_CUSIP
    */
  inner join tbl_BBG_Mapping as mapper
     on AA.CUSIP = mapper.ID_CUSIP
  inner join tbl_BBG_Out as bbgOut
     on mapper.status = bbgOut.LOGIC_IND