Why does this delete query run forever?

sumdumgai

Registered User.
Local time
Today, 02:49
Joined
Jul 19, 2007
Messages
453
This delete query runs forever and I have to kill it.
Code:
DELETE * FROM Table1
WHERE Table 1.Field1 NOT IN (SELECT DISTINCT Field1 FROM Table 2 where Field1 is not null);
Table1 has about 5,000 records and Table2 has about 300,000 records.
If I first create Table3 of distinct Field1's and then do:
Code:
DELETE * FROM Table1
WHERE Field1 not in (Select Field1 From Table3);
it runs quickly. But I would rather not create the Table 3.
Thanks for any help.
 
Try this:
Code:
DELETE * FROM Table1
WHERE NOT EXISTS (
    SELECT Table2.Field1 FROM Table2
    WHERE Table2.Field1 = Table1.Field1);
 
Blazingly fast! Thanks a heap.
 

Users who are viewing this thread

Back
Top Bottom