tsql - use of variable causes update to run very slow -
i have following query.
update t set unitssold = sub.unitssold, dbo.table1 t join (select s.customerkey, s.weekkey, s.productkey, sum(s.unitssold) [unitssold], dbo.table2 s weekkey >= 335 group s.weekkey, s.customerkey, s.productkey) sub on sub.weekkey = t.weekkey , sub.customerkey = t.customerkey , sub.productkey = t.productkey it runs champ. 2 seconds. when try , make dynamic via following.
declare @startweekkey int set @startweekkey = 335 update t set unitssold = sub.unitssold, dbo.table1 t join (select s.customerkey, s.weekkey, s.productkey, sum(s.unitssold) [unitssold], dbo.table2 s weekkey >= @startweekkey group s.weekkey, s.customerkey, s.productkey) sub on sub.weekkey = t.weekkey , sub.customerkey = t.customerkey , sub.productkey = t.productkey all of sudden, it's super slow.
any ideas?
edit: probalby should have mentioned this, contained in stored proc.
added @startweekkey parameter proc , goes running in few seconds.
this question seems have been asked several times before , general answer has statistics.
try:
update statistics table_or_indexed_view_name to statistics date , see if makes difference.
Comments
Post a Comment