SQL Server Performance

Why cursor is slow?

Discussion in 'SQL Server 2005 General DBA Questions' started by ashis.deb, Dec 9, 2008.

  1. ashis.deb New Member

    Hi All,
    we all know when use cursor the performance become slow,so my question is why cursor is slow.
    Thanks is advance.
  2. Adriaan New Member

    Not all cursor based processing is slow. And not all cursor-based processing can be replaced with set-based processing.
    There are situations where cursor-based is your only option. There are even situations where you have the choice between cursor-based and set-based, and cursor-based performs better.
    "Programmers" often don't know any better than to loop through a record-set, so when they start creating SQL procedures, they copy that logic. "Database developers" will know about set-based operations, and know that the database engine is their friend, not the enemy. You just need to speak the language (T-SQL).
  3. ranjitjain New Member

    I have found below lines from BOL which could help.
    If the query uses cursors, determine if the cursor query could be written using either a more efficient cursor type (such as fast forward-only) or a single query. Single queries typically outperform cursor operations.
    Because a set of cursor statements is typically an outer loop operation, in which each row in the outer loop is processed once using an inner statement, consider using either a GROUP BY or CASE statement or a subquery instead

Share This Page