Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cursor to Set Based Operation Conversion

Author  Topic 

vraju
Starting Member

1 Post

Posted - 2013-03-14 : 15:13:42
http://tinypic.com/r/1075g6v/6

So I have this query which searches id by id and calculates the cost accordingly. But is very slow and I would like to understand how I could convert it into a set based operation.

So depending on our condition we calculate our modeled costs differently.

When user updates a driver we can run a update on the entire driver column based on where it has changed.

But when it comes to calculating the modeled cost. We do it row by row as the fixed cost differs and then divide by months. I have pasted the code below. Is there still a way to this by set based operation ?

First we update the drivers in the same table depending on what value has changed using an update and then update the modeled cost row by row (which is really slow)

Code :

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.DIMSTD_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)
-- normal allocation for all scenarios
else
(ISNULL(unita,0) * (ISNULL(DriverValue1,0)/ISNULL(NULLIF(DriverValue2,0),1))* ISNULL(UnitB,0))+ISNULL(FixedCost,0)
--(ISNULL(unita,0) * (ISNULL(DriverValue1,0)/ISNULL(DriverValue2,0))*ISNULL(UnitB,0))+ISNULL(FixedCost,0)
-- allocation for model scenarios
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.TBF_BUDGETExpenses
WHERE BudgetId = @currentId
--and not exists (select 1 from dbo.DIMSTD_SCENARIO where SCENARIO0_Name = SCENARIO and SCENARIO2_Name = 'Model')

UPDATE dbo.TBF_BUDGETExpenses
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

Print 'Record Update ' + CAST(@currentId AS VARCHAR(15))

SET @rowCounter = @rowCounter + 1

END

VR

backend
Starting Member

8 Posts

Posted - 2013-03-14 : 17:48:52
Take a look at your table and see if it has indexes, if you do take a look at the execution plan to find out if if it has any table scan, bookmark up (RID) or hash joins and try to eliminate them.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-03-14 : 18:25:59
backend,
I think they are trying to improve performance by changing from the loop to a set based solution (which is a good thing)

vraju,
this looks like a good candidate for a set based query. please post the code to populate @temp.

Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-15 : 11:36:31
looks like a pivot scenario to me

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -