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


Cursor taking long time


Cursor taking long time

Author
Message
PJ_SQL
PJ_SQL
Mr or Mrs. 500
Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)

Group: General Forum Members
Points: 568 Visits: 652
Hi All,

This is the code I have which users cursors, I have about 2 million records and this script is reading each row and updating it. Is there any suggestions to this?

Declare @A [varchar](9),
@B [integer],
@SQLexec [varchar](500)

Set @B = 0
Declare Cur1 Cursor
For Select A From dbo.tblname

Open Cur1
Fetch from Cur1 into @A
WHILE @@FETCH_STATUS = 0

Begin
Set @B = @B + 1
Set @SQLexec = 'Update dbo.tblname Set rec_num = '+Cast(@B as varchar)+' from dbo.tblname where A = '+@A
Exec(@SQLexec)

Fetch Next From Cur1 into @A
End

Close Cur1
DEALLOCATE Cur

GO
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16563 Visits: 19098
You posted in 2 different forums for 2 different versions, please define which one would you like to consider. The other post is this one: http://www.sqlservercentral.com/Forums/Topic1551281-391-1.aspx

Your code seems to do something as simple as this:

Update dbo.tblname Set
rec_num = 0



If it doesn't satisfy your requirements, check the code you posted.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39374 Visits: 38548
Before doing anything I just want to verify that you are actually using SQL Server 2000 (or older).

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
PJ_SQL
PJ_SQL
Mr or Mrs. 500
Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)

Group: General Forum Members
Points: 568 Visits: 652
It's 2005.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16563 Visits: 19098
I suggest that all answers should be posted on the other thread.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39374 Visits: 38548
Agreed. That way we can use the newer features that are not available in SQL Server 2000. Makes things easier.

By the way, to the OP, please don't post to multiple forums as it just fragments any responses you may get in response to your question.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5900 Visits: 5080
Do you know that cursors' performance is nearly the same to the set-based statements when they are run on up to about 1000 rows. In your case 2 million rows for a cursor will burn the cpu.
You should find a set-based solution, which is not complex for your case. You can use temp tables or CTEs...

Igor Micev,
SQL Server developer at Seavus
My blog: www.igormicev.com
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16563 Visits: 19098
Igor Micev (3/14/2014)
Do you know that cursors' performance is nearly the same to the set-based statements when they are run on up to about 1000 rows.

Not really, it might look that way with small sets of data, but it's mostly perception. A set based query will run several times faster than a cursor in most cases.

You should find a set-based solution, which is not complex for your case. You can use temp tables or CTEs...

Like the ones posted in this thread or the other?


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5900 Visits: 5080
Luis Cazares (3/14/2014)
Igor Micev (3/14/2014)
Do you know that cursors' performance is nearly the same to the set-based statements when they are run on up to about 1000 rows.

Not really, it might look that way with small sets of data, but it's mostly perception. A set based query will run several times faster than a cursor in most cases.

[/quote]
It depends how many columns you're fetching into the cursor and what operations are then perform. However, for one or two columns and small number of rows I haven't seen significant differences. Once I was making some measures, and found out that number (1000) empiricallyl; but as I say, it depends.
Yes, set-based is faster in any case.

Igor Micev,
SQL Server developer at Seavus
My blog: www.igormicev.com
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26121 Visits: 17539
Igor Micev (3/14/2014)
Luis Cazares (3/14/2014)
Igor Micev (3/14/2014)
Do you know that cursors' performance is nearly the same to the set-based statements when they are run on up to about 1000 rows.

Not really, it might look that way with small sets of data, but it's mostly perception. A set based query will run several times faster than a cursor in most cases.


It depends how many columns you're fetching into the cursor and what operations are then perform. However, for one or two columns and small number of rows I haven't seen significant differences. Once I was making some measures, and found out that number (1000) empiricallyl; but as I say, it depends.
Yes, set-based is faster in any case.
[/quote]

It is the small number of rows that concerns me here. We don't generally design new processes against tables with millions of rows but we intend to use them there. And even if the table has only a couple hundred rows today, in the future it can grow to millions. Why write code that is just a time bomb? Your argument sounds like the pleas of somebody who was just asked why they wrote a cursor instead of set based solution. Just do it right the first time and then there is no need to worry about it.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search