SQL CURSOR Performance – SQL For Programmers

Last week I showed how to use the SQL CURSOR to loop through records in a database.  In that article I mentioned that you want to avoid using the CURSOR if you can because it has performance problems.

The article was posted on DZone where a self-proclaimed SQL guru picked it up and left a comment basically stating an alternate way of looping through records that would do the same thing but didn’t require the CURSOR.  What he suggested was the common practice of doing something like this:

SELECT TOP 1 @field1=Field1,
   @field2=field2, @key=keyfield(s)
   FROM TABLE ORDER BY keyfield(s)
WHILE @key /*appropriate logic
         to verify there is a record */
BEGIN
  /* processing here */
  SET @lastKey=@key
  SELECT TOP 1 @field1=Field1,
   @field2=field2, @Key=keyfield(s)
   FROM TABLE WHERE @lastKey < keyfield(s)
END

Note: this is a rough outline of the code.  The key point is that you are doing a select for the next record up in order from the one you just retrieved and you are doing it with nothing but select statements.

Also, keep in mind that keyfield(s) could be one, or multiple fields and that the field(s) should be unique.  Otherwise this won’t work.

If you need to sort in an order other than the primary key, just make keyfield(s) a composite key with the sort order fields first and the primary key last.  Ie assuming all of the following fields are strings, keyfield(s) = field1 + field2 + primaryKeyField.

Instead of doing TOP 1, you could also do a select for the MINimum value of the remaining keys.

And it works for most cases.

However, in researching this post, I also ran across this article:

http://www.sqlteam.com/article/cursor-performance

The gist of the article is that while everyone knows you aren’t supposed to use CURSORS because they are so slow, you need to TEST your solution to make sure that in this particular case it really is faster.


But like I stated when I started this series, my job is to get you beyond SELECT, INSERT, UPDATE, and DELETE statements so that you can do more on the server side, not to turn you all into DBAs who know where all of these performance gotchas are.


Other Related Items:

3/4 hp 3450rpm 48Y Frame 115 volts 2 Speed Square Flange Pool Pump Replacement Motor AO Smith Electric Motor # SQL1072R3/4 hp 3450rpm 48Y Frame 115 volts 2 Speed Square Flange Pool Pump Replacement Motor AO Smith Electric Motor # SQL1072RSquare Flange replacement inground pool pump motor. This is a 2 speed motor. Motor is found on many in ground pool pumps such as PacFab Challenger, Wh... Read More >
E6-B Flight Computer (APR's Pocket Series E6-B4WHL)E6-B Flight Computer (APR's Pocket Series E6-B4WHL)APR's Pocket Series Wind Triangle & T/S/D E6B computer features patent pending Windspeed Cursor Arm. It calculates flight planning and en route naviga... Read More >
Battery Tender Junior ChargerBattery Tender Junior ChargerBattery Tender Junior Charger Lightweight, compact in size ideal for those hard to fit spots
The trickle charger with a brain will assure batteri... Read More >

Related Post

2 Responses to “SQL CURSOR Performance – SQL For Programmers”

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor