TSQL更新表超时

时间:2015-05-26 12:17:56

标签: sql sql-server sql-server-2008

存储过程中的sql有几个更新。我已经修好一个,剩下的一个仍然超时。

正在更新的表有超过600000条记录。当单独测试时,sql中的select语句运行得很快,因此它必须是更新部分。

UPDATE tblPackage  
SET IsUpdated = 1,
    ShippingCost = (
        SELECT SUM(SC.ShippingCost)
        FROM tblShippingCost SC
        WHERE SC.PackageID = P.PackageID)
FROM tblShippingCost SC
    INNER JOIN tblPackage P ON P.PackageID = SC.PackageID
    INNER JOIN tblRunPackage RP ON RP.PackageID = P.PackageID
    INNER JOIN tblRun R ON R.RunID = RP.RunID
    INNER JOIN tblOrderItem OI ON OI.OrderItemID = R.OrderItemID
    INNER JOIN tblOrder O ON O.OrderID = OI.OrderID
WHERE 
    O.InvoiceID IS NULL 
    AND SC.PackageID = P.PackageID

我欢迎任何建议。

谢谢!

2 个答案:

答案 0 :(得分:1)

你加入了tblshippingcost额外的时间,这不会给出错误的结果。但是加入和计算需要更多时间。尝试从您的更新中删除该部分,如下所示:

UPDATE tblPackage  
SET
  IsUpdated = 1,
  ShippingCost = (
       SELECT SUM(SC.ShippingCost)
       FROM tblShippingCost SC
       WHERE SC.PackageID = P.PackageID
     )
FROM 
    tblPackage P
INNER JOIN 
  tblRunPackage RP ON RP.PackageID = P.PackageID
INNER JOIN
  tblRun R ON R.RunID = RP.RunID
INNER JOIN 
  tblOrderItem OI ON OI.OrderItemID = R.OrderItemID
INNER JOIN 
  tblOrder O ON O.OrderID = OI.OrderID
WHERE 
    O.InvoiceID IS NULL 

答案 1 :(得分:0)

试试这个

UPDATE tblPackage  
SET IsUpdated = 1,
    ShippingCost = SUM(SC.ShippingCost)
FROM tblShippingCost SC
    INNER JOIN tblPackage P ON P.PackageID = SC.PackageID
    INNER JOIN tblRunPackage RP ON RP.PackageID = P.PackageID
    INNER JOIN tblRun R ON R.RunID = RP.RunID
    INNER JOIN tblOrderItem OI ON OI.OrderItemID = R.OrderItemID
    INNER JOIN tblOrder O ON O.OrderID = OI.OrderID
WHERE 
    O.InvoiceID IS NULL 
    AND SC.PackageID = P.PackageID