SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Cursors and high gas prices

Using cursors is a controversal topic. As a former developer, that is the way I thought about processing data at one time. I gave a talk to a VB group a couple of years ago where I compared using cursors, correlated subqueries, etc., to other techniques to show the performance differences. A lively discussion ensued as the audience members were shocked that the ways they were updating and retrieving data might not be the best. 

I recently came up with an analogy to explain why processing one row at a time is generally a bad idea. Suppose you have a 10 item grocery list. What are the steps to get the groceries to your pantry? If we process one item at a time, it will look something like this:


for each item in list
    walk to the car
    get in the car
    start the car
    drive to the store
    stop the car
    walk into store
    locate food package that matches item
    retrieve food package from store shelf
    cross off list
    pay for package
    leave store
    walk to the car
    get in car
    start car
    drive home
    stop car
    get out of the car
    walk in house with package
    put package away
next item

With the high gasoline prices, you can see why this might not be a good idea.  


Advice from Aunt Kathi

Kathi Kellenberger is a Sr. Consultant with Pragmatic Works. She is an author, speaker and trainer.


No comments.

Leave a Comment

Please register or log in to leave a comment.