Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update query Help

Status
Not open for further replies.

oobern81

Programmer
Joined
Dec 2, 2010
Messages
1
Location
CA
Hi All,
I am having a difficult time optimizing a procedure to run more effeiently, and am hoping that someone may give some advise...
Basically, I have 2 tables:
-- Raw (staging table, max 50,000 records)
-- Lookup (over 200 million records and growing)

I have a package developed to import data from a text file into the Raw Table.. From there I have to perform updates on the Raw table to mark records as valid or not.
The raw table contains about 100 fields, of which 3 columns are used...
If data in any of these 3 columns matches any record from Lookup table, then the record in Raw Table must be updated...Lookup table is huge!

It is easy to write a simple update statement which will work, however I find the processing time is far too long!!

Below is the code from within procedure:

Update Raw
Set Col9=1000,
Col10='Invalid'

from Raw a, Lookup b
where (a.Col1 = b.Col2 or
a.Col2 = b.Col2 or
a.Col3 = b.Col2)


Does anyone have any tips on how to optimize?
Thanks in advance

oobern81
 
If you're using SQL Server 2005+, try
Code:
;with cte as (select Pk, Col9, Col10 from Raw R
where exists (select 1 from Lookup L
where L.Col2 IN (R.Col1, R.Col2, R.Col3))

update cte set Col9 = 1000, Col10 = 'Invalid'

In order to test first, change UPDATE to select * from cte. If the selected records are correct, try using an update.


PluralSight Learning Library
 
Try:
Code:
Update Raw Set Col9=1000,
               Col10='Invalid'
from Raw a
where EXISTS(SELECT * FROM lookup b
                WHERE a.Col1 = b.Col2 or
                      a.Col2 = b.Col2 or
                      a.Col3 = b.Col2)

or:
Code:
Update Raw
Set Col9=1000,
    Col10='Invalid'

from Raw a
INNER JOIN (SELECT DISTINCT Col2
                    FROM LookUp) b
where (a.Col1 = b.Col2 or
       a.Col2 = b.Col2 or
       a.Col3 = b.Col2)


Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
Yep :-)))

Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top