我有以下查询。
UPDATE t
SET UnitsSold = sub.UnitsSold,
FROM dbo.table1 t
JOIN (SELECT s.CustomerKey,
s.WeekKey,
s.ProductKey,
Sum(s.UnitsSold) AS [UnitsSold],
FROM dbo.table2 s
WHERE WeekKey >= 335
GROUP BY s.WeekKey,
s.CustomerKey,
s.ProductKey) AS sub
ON sub.WeekKey = t.WeekKey
AND sub.CustomerKey = t.CustomerKey
AND sub.ProductKey = t.ProductKey
它像冠军一样奔跑。大约2秒。然后尝试通过以下方式使其动态化。
DECLARE @StartWeekKey AS INT
SET @StartWeekKey = 335
UPDATE t
SET UnitsSold = sub.UnitsSold,
FROM dbo.table1 t
JOIN (SELECT s.CustomerKey,
s.WeekKey,
s.ProductKey,
Sum(s.UnitsSold) AS [UnitsSold],
FROM dbo.table2 s
WHERE WeekKey >= @StartWeekKey
GROUP BY s.WeekKey,
s.CustomerKey,
s.ProductKey) AS sub
ON sub.WeekKey = t.WeekKey
AND sub.CustomerKey = t.CustomerKey
AND sub.ProductKey = t.ProductKey
突然间,它超级慢。
有什么好主意吗?
编辑:Probalby 应该提到这一点,但这包含在存储过程中。
将@StartWeekKey 作为参数添加到过程中,它会在几秒钟内恢复运行。