how to write cursor alternative query in sql server -
i need implement without using cursor. below script using cursor , it's taking 5 hours 140k records. how improve performance in sql server?
in orginal table have on 100k records.
set nocount on create table #temp ( recordid int identity, address varchar(50), city varchar(30), state varchar(5), gpslat numeric(9,6), gpslong numeric(9,6), mapurl varchar(255)) insert #temp (address, city, state) values ('1033 southwest 152nd street', 'burien', 'wa') insert #temp (address, city, state) values ('11910 northeast 154th street', 'brush prairie', 'wa') insert #temp (address, city, state) values ('500 seaworld drive', 'san diego', 'ca') insert #temp (address, city, state) values ('1 legoland drive', 'carlsbad', 'ca') declare curgeo cursor local static select recordid, address, city, state #temp declare @recordid int declare @address varchar(50) declare @city varchar(30) declare @state varchar(5) declare @gpslatitude numeric(9, 6) declare @gpslongitude numeric(9, 6) declare @mapurl varchar(255) open curgeo fetch curgeo @recordid, @address, @city, @state while @@fetch_status = 0 begin begin try exec opsstream.sputilgeocode @address = @address output, @city = @city output, @state = @state output, @gpslatitude = @gpslatitude output, @gpslongitude = @gpslongitude output, @mapurl = @mapurl output update #temp set gpslat = @gpslatitude, gpslong = @gpslongitude, mapurl = @mapurl recordid = @recordid end try begin catch print 'warning: recordid ' + cast(@recordid varchar(100)) + ' not geocoded.' end catch fetch curgeo @recordid, @address, @city, @state end select * #temp
you have procedure call in code, i'm quite sure problem not cursor, row-by-row logic done procedure. might improve performance of cursor defining fast_forward
, might not noticeable.
you have procedure call, either need change procedure accept table valued parameter, , of course change procedure it's not row-by-row operation or change table valued function, if use multi statement one, it's not going improve performance.
Comments
Post a Comment