I am trying to do a simple sql query:
SELECT DISTINCT id
FROM marketing
WHERE type = 'email'
AND id NOT IN (
SELECT id
FROM marketing
WHERE type = 'letter'
)
ORDER BY id;
It takes a really long time to run, and I assume it has to do with the select in the where statement (There are a large number of ids), but I can't come up with a way to improve it.
First can this be the reason the query is so slow, and second any suggestion on how to improve it?
Edit:
Database System: MySql
Id is indexed but but is not a primary key in this table; it is a foreign key.