0

I need to identify which records are missing in a table taking another table for comparison.

I tried:

SELECT * FROM tableA WHERE tableA.id NOT IN (SELECT tableB.id FROM tableB)

It normally works when the data is small of course but tableA and tableB both have over 400k records and the process gets stuck, is there a more optimal way to do this?

1 Answer 1

3

Joins are usually faster than IN and NOT IN clause. Try this:

SELECT tableA.*
FROM tableA left join tableB on tableA.id = tableB.id
WHERE tableB.id is null
1
  • You'll need two queries if you want missing rows in either table. Nov 22, 2012 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.