I have two tables in MySQL that are related. I would like to find the rows in table A that do not have a corresponding row table B. The manual and other threads here on SO recommend doing this:
SELECT a.id
FROM a LEFT JOIN b ON a.id = b.a_id
WHERE b.id IS NULL;
However, this is very slow. In my case, table A has less than 5000 rows, and table B is at around 40000, but this query is taking up to 8 minutes.
Does anybody know how to achieve this faster?
Thank you very much, matt
EDIT: The index was the problem. After creating one, the query runs in 10 microseconds.
a_id
field with index in b
tableWHERE b.id IS NULL
with WHERE b.a_id IS NULL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With