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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Optimise SQL Upate Query

Author  Topic 

mtlondon
Starting Member

2 Posts

Posted - 2010-11-04 : 08:02:36
Hi

I have 2 tables, one is a wide version with name and address info and the other is a deep version with other profile info.

I have deduped the name and address table, and now have a contact urn, i need to update the deep table with this.

The link between 2 tables at present is an SRN field and source name, I need to update the contact urn, but would need to run in batches as there are millions of records to update. I have indexes on both tables, but the query seems to take too long to run and uses up all the space.

my code is

declare @i int,
@stop int

select @i = MIN(SRN),
@stop = MAX(SRN)
from URNs


while @i < @stop

begin

update a
set a.PURN = b.PURN,
a.AURN = b.AURN,
a.EURN = b.EURN
from other a, URNs b
where a.Source = b.Source
and a.SRN = b.SRN
and a.PURN is null
and b.SRN in (select top 5000 SRN
from URNs
where SRN >= @i
order by SRN )

set @i = @i + 5000


delete from URNs where SRN < @i

end

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-04 : 09:47:00
1. what are deep and wide?
2. You should use ANSI syntax
3. if you do it that way us WHERE BETWEEN, not in and use a loop
4. millions is not alot...how big does the log get?
5. If it is a problem, try creating a view and do an unload/ load

MOO

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -