Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Performance comparison of using WHILE and CURSOR
Message
From
26/06/2009 07:39:48
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2005
Application:
Web
Miscellaneous
Thread ID:
01408571
Message ID:
01408595
Views:
50
Right. Then the tow results are close and while is faster. On my machine w/o PK:
While:9184
Cursor: 186

with PK:
While: 126
Cursor:170

What I believe he needed neither while nor cursor:) but who knows.
Cetin

>Jokes aside, OP forgot to make pk column a PRIMARY KEY
>
>
>DECLARE @MyData TABLE(pk INT IDENTITY(1,1) PRIMARY KEY, id INT)
>
>
>>Are you sure??? I think he meant not to start with @index = 10000.
>>Cetin
>>
>>>My results are totally different
>>>
>>>Execution Time of WHILE Loop (ms)
>>>0
>>>
>>>Execution Time of CURSOR (ms)
>>>516
>>>
>>>
>>>I prefer to use WHILE loop (if I have to) because it's more flexible and faster
>>>
>>>>
>>>>Recently I learned about CURSOR and I found most experts advise not to use it due to performance issue. However, I found using CURSOR is much faster than using a WHILE for looping. Thus, I would like to seek for some valuable advices.
>>>>
>>>>Basically I would like to loop each record in my record sets and perform some actions based on the selected field from each row.
>>>>
>>>>In order to simplify my scenario, I have created a sample here for comparison.
>>>>
>>>>1. Create a table variable with 10k records.
>>>>
>>><snip>>
>>>>
>>>>And finally I got this result
>>>>
>>>>Execution Time of WHILE Loop (ms)
>>>>11063
>>>>Execution Time of CURSOR (ms)
>>>>296
>>>>
>>>>
>>>>Kindly advise, thanks.
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform