I have the following query.
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
He works as a champion. About 2 seconds. Then, when you try to make it dynamic using the following.
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
Suddenly, it is very slow.
any good ideas?
EDIT: Probalby should have mentioned this, but this is contained in the stored procedure.
The @StartWeekKey parameter has been added as a parameter for proc, and it returns to launch in a few seconds.
source share