Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Interview question: Cursors

Author  Topic 

ravilobo
Master Smack Fu Yak Hacker

1184 Posts

Posted - 2009-11-06 : 16:05:32
I told in the recent interview that I avoid cursors because they cause a network round trip. And I was challenged.

Now after the interview, I think the n/w round trip is True for the app cursors, not the SQL server cursor, which resides on the database layer.

What are the down sides of using a SQL cursor, compared with a temp table & while loop?

------------------------
I think, therefore I am - Rene Descartes

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-06 : 17:03:17
dude....cursors in sql server, reside in sql server

I imagine, and I could be wrong, that "Cursors", i.e. looping through a result set is purely done on the app server

No round trips (unless your app side "Cursor" is executing a sql server statement for each row, in which case you should be shot)

No the downside of Cursors in SQLServer is that they incur a lot of overhead and must access data row by row (does a page of data get loaded after n fetches? Gotta look up Delaneys book).

You are much better served to use a set based approach to your DML and be appropriately indexed.

Now, performance issues on the app side with your "Cursor" would be bad if you decide to select 50,000 rows...that would be a problem

Understand also, this is NOT True for DB2 and Oracle which are optimized for cursors (they use pointers to data and don't actually move anything)

HTH

or

MOO

or

As Much as I can correctly remember being old and decrepit



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-11-06 : 20:05:21
I seem to post this a lot, sorry if it's getting old:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=118835
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-06 : 22:28:43
isn't that what I said?

I do feel like an espresso however...and a shot of Anise~



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-11-06 : 22:45:03
Yeah, you did, I'm just being a shameless self-promoter.
quote:
Understand also, this is NOT True for DB2 and Oracle which are optimized for cursors (they use pointers to data and don't actually move anything)
If they don't actually move anything, how do the rows get sent to the client? I'm not sure this would be the reason those cursors are faster, since they'd have to access the data pages on each fetch, whereas SQL Server could fetch on buffered pages.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-07 : 09:18:35
kinda my point

They don't move them to buffer


the use reference pointers



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

ravilobo
Master Smack Fu Yak Hacker

1184 Posts

Posted - 2009-11-09 : 10:16:37
quote:
Originally posted by robvolk

I seem to post this a lot, sorry if it's getting old:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=118835



Rob,

Thanks a lot for the, Coffee-Sugar example. I am very clear now! I am going to use your analogy in my next interview, however I will portray as if it is my own. Hope that is fine with you!


------------------------
I think, therefore I am - Rene Descartes
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-11-09 : 17:34:43
Oh yeah, take all the credit you want. Like I said though, please laugh at them before you tell the story.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-09 : 18:11:11
you're going to use that in an interview?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

ravilobo
Master Smack Fu Yak Hacker

1184 Posts

Posted - 2009-11-10 : 09:42:01
quote:
Originally posted by robvolk

Oh yeah, take all the credit you want. Like I said though, please laugh at them before you tell the story.



The one who took my interview was a Geek, but also a nice guy. I guess he just wanted to know, how much clear I am with my concepts. End of the day, I am clear about the curors. That's a good news!

Thank you guys. (I just got the offer! )

------------------------
I think, therefore I am - Rene Descartes
Go to Top of Page
   

- Advertisement -