March 7, 2013 at 3:29 pm
Hi All,
So we have this stored procedure that runs fine most of the times, but we have occasionally encountered that it hangs infinitely and also is very slow. I have pasted the stored procedure code below. Let me know what you'll think
SELECT @rowCounter = 1, @totalrows = @@ROWCOUNT
WHILE @rowCounter <= @totalrows
BEGIN
SELECT @currentId = tempId
FROM @temp
WHERE row = @rowCounter
SELECT
@newModeledCost =
case when not exists (select 1 from dbo.DIM_SCENARIO where SCENARIO0_Name = SCENARIO and SCENARIO2_Name = 'Model') then
ISNULL(DriverValue1,0)*ISNULL(DriverValue2,0)*ISNULL(UnitA,0)*ISNULL(UnitB,0)+ISNULL(FixedCost,0)
else
(ISNULL(unita,0) * (ISNULL(DriverValue1,0)/ISNULL(NULLIF(DriverValue2,0),1))* ISNULL(UnitB,0))+ISNULL(FixedCost,0)
end
,
@oldModeledCost = ISNULL(ModeledCost,0),
@newOct = (ISNULL(@newModeledCost,0) * (ISNULL(Oct, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newNov = (ISNULL(@newModeledCost,0) * (ISNULL(Nov, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newDec = (ISNULL(@newModeledCost,0) * (ISNULL(Dec, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newJan = (ISNULL(@newModeledCost,0) * (ISNULL(Jan, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newFeb = (ISNULL(@newModeledCost,0) * (ISNULL(Feb, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newMar = (ISNULL(@newModeledCost,0) * (ISNULL(Mar, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newApr = (ISNULL(@newModeledCost,0) * (ISNULL(Apr, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newMay = (ISNULL(@newModeledCost,0) * (ISNULL(May, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newJun = (ISNULL(@newModeledCost,0) * (ISNULL(Jun, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newJul = (ISNULL(@newModeledCost,0) * (ISNULL(Jul, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newAug = (ISNULL(@newModeledCost,0) * (ISNULL(Aug, 0) /ISNULL(NULLIF(@oldModeledCost,0),1))),
@newSep = (ISNULL(@newModeledCost,0) * (ISNULL(Sep, 0) /ISNULL(NULLIF(@oldModeledCost,0),1)))
FROM dbo.Calculations
WHERE BudgetId = @currentId
UPDATE dbo.Calculations
SET ModeledCost = @newModeledCost,
Oct = @newOct,
Nov = @newNov,
Dec = @newDec,
Jan = @newJan,
Feb = @newFeb,
Mar = @newMar,
Apr = @newApr,
May = @newMay,
Jun = @newJun,
Jul = @newJul,
Aug = @newAug,
Sep = @newSep,
Username = 'Cascade',
lastmodified = getdate()
WHERE BudgetId = @currentId
AND @oldModeledCost <> 0
SET @rowCounter = @rowCounter + 1
END
March 8, 2013 at 8:56 am
Any thoughts people ?
March 8, 2013 at 9:49 am
The "hangs infinitely" could be abother process blocking your proc. We can't know from the information presented. Have you looked at what else is running on the system when it hangs?
The "is slow" is not surprising given the fact that it employs a loop and uses tons of function calls. Converting the proc to use set-based logic and cleaning up the data so you do not need all those cleansing functions every time you do an update would be a step in the right direction.
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
March 8, 2013 at 10:04 am
This looks an awful lot like a loop that is running inside either another loop or a cursor. I agree that the only way you are going to improve performance on this is to completely rewrite this as a set based operation. There is really no need to loop like this for updates.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 8, 2013 at 10:10 am
Thanks guys. I'll try out your sugesstions and reply back.. Thanks!!!
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply