Search code examples
performancesql-server-2005cursor

How slow are cursors really and what would be better alternatives?


I have been reading that cursors are pretty slow and one should unless out of options avoid them. I am trying to optimize my stored procedures and one of them uses a cursor. It frequently is being called by my application and with lot of users(20000) and rows to update. I was thinking maybe I should use something else as an alternative.

All I am trying to do or want is to get a list of records and then operate on depending on each row value. So for e.g we have say -

Employee - Id,Name,BenefitId,StartDate,EndDate

So based on benefitId I need to do different calculation using dates between StartDate and EndDate and update employee details. I am just making this contrived example to give a idea on my situation.

What are your thoughts on it ? Are there better alternatives for cursors like say using temp tables or user defined functions? When should you really opt for them or should we never be using cursors ? Thanks everyone for their help.


Solution

  • I've changed out cursors and moved from over 24 hours of processing time to less than a minute.

    TO help you see how to fix your proc with set-based logic, read this: http://wiki.lessthandot.com/index.php/Cursors_and_How_to_Avoid_Them