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
 General SQL Server Forums
 New to SQL Server Programming
 help please

Author  Topic 

catbollu
Starting Member

10 Posts

Posted - 2011-10-29 : 16:00:05
I am trying to speed up a query. I am selecting the top 1 record each run and then deleting it so I can select top again. this is too slow deleting
What I think will speed it up is to just select the record number and incriminate the number each time eliminating the delete part.
I thought I could access the recordnumber like this but it wont work

select * from #tempourfilenum where recordnumber =1....



select top 1 @theourfile=rtrim(ltrim(ourfile)), @ourdate=rtrim(ltrim(editdate)) from #tempourfilenum

While ( @theourfile is not null)
begin
select top 1 @theourfile=rtrim(ltrim(ourfile)),@ourdate=rtrim(ltrim(editdate)) from #tempourfilenum
-- drop table ##temp2
drop table #temp2
--we now put the the ourfile and editdate in a table to use to get the right data
--for each ourfile
select
into #temp2 from alladdressqlaw
where ltrim(rtrim(ourfile))=ltrim(rtrim(@theourfile)) ORDER BY editdate DESC

-- get counts to make sure there are records to write to the addresses
select @mycount=count(ourfile) from #temp2
if @mycount >=1
begin
--address2
--get the ourfile and date
select top 1 @ourfile = rtrim(ltrim(ourfile)), @ourdate = ltrim(rtrim(editdate)) from #temp2


update a table

delete from #temp2 where ltrim(rtrim(ourfile))=ltrim(rtrim(@theourfile)) and ltrim(rtrim(editdate))=ltrim(rtrim(@ourdate))


delete from #tempourfilenum where ltrim(rtrim(ourfile)) = ltrim(rtrim(@theourfile))
select top 1 @theourfile=rtrim(ltrim(ourfile)) from #tempourfilenum

paultech
Yak Posting Veteran

79 Posts

Posted - 2011-10-29 : 22:17:30
Hello ,I surprised why you don't want to use a cursor to pass row by row ? and what you mean by saying
quote:
I am selecting the top 1 record each run and then deleting it so I can select top again. this is too slow deleting


paul Tech
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-29 : 23:38:11
you dont need cursor at all . you can do set based processing for all this.
Also all your select top should have order by on basis of which you want top. else it will retrieve random records

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

catbollu
Starting Member

10 Posts

Posted - 2011-10-30 : 12:54:12
quote:
Originally posted by paultech

Hello ,I surprised why you don't want to use a cursor to pass row by row ? and what you mean by saying
quote:
I am selecting the top 1 record each run and then deleting it so I can select top again. this is too slow deleting


paul Tech


I do select top 1 work with that information than delete based on that select and do it again
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-31 : 04:23:32
quote:
Originally posted by catbollu

quote:
Originally posted by paultech

Hello ,I surprised why you don't want to use a cursor to pass row by row ? and what you mean by saying
quote:
I am selecting the top 1 record each run and then deleting it so I can select top again. this is too slow deleting


paul Tech


I do select top 1 work with that information than delete based on that select and do it again


thats ok. but let us know on what basis you want to do this repeated deletion.
show some sample data and show how you want to delete and show what should be final output after deletion

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -