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


Not sure you're faster without cursors


Not sure you're faster without cursors

Author
Message
psimler
psimler
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 17
Hello,

I did some performance tests on the following table


CREATE TABLE [dbo].[tblPeople](
[Id] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[FirstName] [varchar](100) NULL,
[LastName] [varchar](100) NULL,
[BirthDate] [datetime] NULL,
CONSTRAINT [PK_tblPeople] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]



I've created 10.000 rows


declare @i int

set @i=1
while @i<10000
begin
insert tblPeople ( FirstName, LastName, BirthDate )
values ( 'FirstName ' + str(@i), 'LastName ' + str(@i), getdate() )
set @i=@i+1
end



With the cursor, it takes 343 ms


declare @id int
declare @firstname varchar(100)
declare @lastname varchar(100)
declare @birthdate datetime
declare @s datetime, @e datetime

--declare c cursor for
declare c cursor local forward_only for
select Id, FirstName, LastName, BirthDate
from tblPeople
order by Id

set @s = getdate()

open c
while 1=1
begin
fetch next from c into
@Id, @FirstName, @LastName, @BirthDate
if @@fetch_status != 0 break
end

close c
deallocate c

set @e =getdate()

print datediff( ms, @s, @e )



With Gaby's approach, it takes 36690 ms !!


declare @id int
declare @firstname varchar(100)
declare @lastname varchar(100)
declare @birthdate datetime
declare @s datetime, @e datetime

select Id, FirstName, LastName, BirthDate into #t
from tblPeople
order by Id

set @s = getdate()

while 1=1
begin
select top 1
@Id=Id, @FirstName=FirstName, @LastName=LastName, @BirthDate=BirthDate
from #t
order by Id
if @@rowcount = 0
break
delete #t where Id=@Id
end

drop table #t

set @e =getdate()

print datediff( ms, @s, @e )



I think, i will continue to use cursors, it's just 107 times faster

/Pascal
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