Question Locked

Never ending UPDATE

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I ran this query in the Query Analyzer for 6 hours and then gave up. 29 million recs at 24GB.

Update A
Set Acctid = B.Acctid
From B
Where Substring(VendId,5,4) = B.VendId4

I did a link from Access and Access took 4 Hrs but it worked for 5,3.
Any suggestions will help me understand the SQL system.

3 replies

by tony hopkinson

That is doing a select from b for every record in A and the select is a serial read across B looking for the match

1000 records in A and 1000 records in B results in 1,000,000 read and compares.
If acctid is a key every update will also result in a change to the index and substring is necessarily inefficient.

Most of the wways to optimise this would generate a new table and possibly restructure the existing one

For instance add a column vendidsuffix allowed null to A
Then
Update A set Vendidsuffix = Substring(Vendid,5,4)

Then put indexes on vendid in B and vendidsuffix in A would speed things up some, then you could drop the added column.
Where clause is then A.Vendidsuffix = B.vendid

No substring function and the indexes will be
used instead of traversing the entire table

Some query optimisers would build temporay indexes for the lifetime of the update statement if that worked out 'best'

Another thing you could try is build a new version of the A table with

Select B.acctid, A.Vendid,......
Into NewA
From A,B
Where Substring(A.VendId,5,4) = B.VendId4

You could popin teh vendidsiffix column and indexes again to improve things.

Drop A rename A2 and add any indexes, constraints etc.
Or Truncate A and
Insert into A, select * From NewA

When looking at this sort of thing imagine you were doing it manually with print outs of the data. What would you have to do?

by jbny

I suspected the substring was a problem. I used it in Dbase many years ago but the data was much smaller. The last smaller database I had I created fields with the 4 5 and 6 digits so the match was the same with my B table fields. I had to search and update each sequence and tried the quick fix of substring. Tony, your solution help me understand the reads.
I think now I could just create fields acct4, acct5, acct6 and
update A
set acct4 = substring(acctid,1,4)
set acct5 = substring(acctid,1,5) etc

then
update A from B matching on the same length fields.

This discussion is closed to new replies.