Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query improvement - MSAccess 2007

I have a need to Select all records based on the following SQL:

Select ID, ID2 From Table1 Where ID2 NOT IN (Select ID2 From Table2 Where ID3 IN (151,157))

There are 171k records in Table1 and 70k records in Table2 where 'ID3 IN (151,157)'.

Unfortunately, that query takes forever; in fact, I have never seen it complete on a system with 32GB memory and quad I7 processors. I give up and cancel after 30 minutes.

I figure there is an SQL guru or two here that can tell me how to improve this query and get it to complete in under a minute.

like image 858
JJJones_3860 Avatar asked Mar 03 '26 02:03

JJJones_3860


1 Answers

You could try joining the subquery:

Select ID, ID2
From Table1
LEFT JOIN 
(Select ID2
 From Table2
 Where ID3 IN (151,157))
WHERE ID2 IS NULL

If that doesn't work, I would consider creating the subquery as temp table and referencing that.

like image 135
kjmerf Avatar answered Mar 04 '26 17:03

kjmerf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!