July 7, 2005 at 6:29 am
Sub : I would like to have a replacement for a Cursor.
It should be other than the use of Table variables or
Temporary Tables.
Is there any other methods available.
regards
shown
July 7, 2005 at 6:47 am
Post the table definition, some sample data and the expected output from the query. We can't help you without that.
July 7, 2005 at 6:56 am
If there is a unique id, then it is easy:
while (@id is not null)
select @id = min(id) from table where id > @id
and continue until you get null.
July 7, 2005 at 6:58 am
That's nice but how about we show him how to use the set based approach that he obviously doesn't know??
July 7, 2005 at 7:16 am
Not sure I know that either, so I look forward to your reply...
July 7, 2005 at 7:22 am
Will have to look for his reply first .
July 7, 2005 at 7:34 am
Mr Jesper,
I think this is the technique what U have meant.
****************************************************
eg:-
SELECT @id=MIN(id) FROM TABLE1
SET @minid=@id
WHILE @minid IS NOT NULL
BEGIN
------
-------
-------
SELECT @minid=MIN(id) FROM TABLE1 WHERE id > @minid
END
***********************************************************
If this is the technique if a table is very big that
doesn't help i feel. Every time U will have to check
the minimum of that table for manipulations.
Actually I know 3 techniques which are replacement for the
Cursors.
1) Using Temp tables
2) Using Table Variables
3) Finding the MIN value and manipulating row by row
I wanted some other technique which can be actually used on
big tables so that the manipulations does work faster.
regards
shown
July 7, 2005 at 8:03 am
Thanx Shown for this input, but I'd like to stress out that these methods are only LAST RESORTS, you must first try to find the SET BASED approach that will run faster 99.999% of the time compared to these work-arounds.
July 7, 2005 at 8:08 am
You assumption is correct
I added a simple "select @minid" to the code above and constructed a similar cursor example. Then I ran the two algorithms on a table with 77000 rows. They both completed in 48 secs (you need an index on id, of course).
Are you looking for something even faster, or is there a different reason for not using cursors?
July 7, 2005 at 8:19 am
Here's my thinking : Don't use cursors, ever. Then if you find a situation where a cursor is faster than a set based approach or there's no set based approach (some rare admin task), then go for it. But I assure you that it's not gonna happen often.
July 7, 2005 at 8:27 am
Please give me some examples regarding
"What did u mean by this SET BASED APPROACH"
regards
shown
July 7, 2005 at 8:33 am
while (@id is not null)
begin
select @id = min(id) from table where id > @id
Update Table set SomeField = (Select Value from Table2 where id = @Id) where id = @Id
end
Now run this on a table with 100k rows.
Should take 2-20+ minutes.
Now run this :
Update T1 SET T1.SomeField = T2.Value from dbo.Table T1 inner join dbo.Table2 T2 on T1.id = T2.id
Should take between 1 and 20 seconds.
July 7, 2005 at 8:42 am
Ok, Does it mean that it's better to prefer
SET BASED APPROACH above all other methods.
shown
July 7, 2005 at 8:46 am
As I said, research only for procedural approach when no set based approach exists (or works at reasonable speed). Sometimes it's faster to use a cursor, but it's just 0.001% of the time.
You have to switch your thinking when programming in sql. Don't do things one step at the time, but do all steps in one pass .
July 7, 2005 at 8:50 am
Nice way to finish.
Anyway thank U very much for the help and to everyone involved.
regards
shown
Viewing 15 posts - 1 through 15 (of 70 total)
You must be logged in to reply to this topic. Login to reply