Need to speed up a query

M

mscertified

I have a table of around 50,000 rows (table A) and another table of around
5000 rows (table B).
There is a foreign key in table A pointing to table B.
Periodically, table A records are archived leaving 'orphan' keys in table B.
I need a query that deletes the orphan rows in table B.
I tried DELETE * FROM TableB WHERE ID NOT IN (SELECT ClientID FROM TableA)
however, it takes forever to run.
 
R

rox

mscertified said:
I have a table of around 50,000 rows (table A) and another table of around
5000 rows (table B).
There is a foreign key in table A pointing to table B.
Periodically, table A records are archived leaving 'orphan' keys in table
B.
I need a query that deletes the orphan rows in table B.
I tried DELETE * FROM TableB WHERE ID NOT IN (SELECT ClientID FROM TableA)
however, it takes forever to run.

Would SELECT DISTINCT speed things up any?
 
M

Michael Gramelspacher

I have a table of around 50,000 rows (table A) and another table of around
5000 rows (table B).
There is a foreign key in table A pointing to table B.
Periodically, table A records are archived leaving 'orphan' keys in table B.
I need a query that deletes the orphan rows in table B.
I tried DELETE * FROM TableB WHERE ID NOT IN (SELECT ClientID FROM TableA)
however, it takes forever to run.

maybe:

DELETE *
FROM TableB
WHERE TableB.ID IN (SELECT ID
FROM TableB LEFT JOIN TableA ON [TableB].[ID]=[TableA].[ClientID]
WHERE [TableA].[ClientID] Is Null);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top