Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Missing foreign key constraintsCollation conflicts with temp tables and table variables. »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Looping in SQL code almost always causes performance problems. You can use a cursor or a while loop in SQL code. Both offer similarly bad performance. Not all code can be re-written in a set based manner, but it does make sense to revisit your looping code from time to time to see if it can be re-written.

There are rare times when a cursor will outperform set based code. Not all procedures returned by the query need (or should) be re-written. But it is a worth while endeavor to re-examine these procedures from time to time.

How to detect this problem:

  1. SELECT  OBJECT_NAME(OBJECT_ID) AS ProcedureName
  2. FROM    sys.sql_modules S
  3. WHERE   (Definition Like '%cursor%' or Definition Like '%while%')
  4.         And OBJECTPROPERTY(OBJECT_ID, N'IsMSShipped') = 0
  5. ORDER BY OBJECT_NAME(OBJECT_ID)

How to correct it: Examine each block of code that has a loop, and design an alternative method for accomplishing the same results. This is not always possible.

Level of severity: moderate

Level of difficulty: High

About the Author

George has been developing software professionally for 19 years, first for the department of defense, and then for various other companies. In 1998, George started his software company, Orbit Software, specializing in School Bus Transportation software. His specialty is refining SQL Server queries to deliver optimal performance.
Social SitingsTwitterLTD RSS Feed
311 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

1 comment

Comment from: thirster42 [Member] Email
i think it'd be better if you went into detail as to why set-based code performs better than a cursor or while statement.
11/20/09 @ 07:37

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)