SELECT count(*)
FROM contacts_lists
JOIN plain_contacts
ON contacts_lists.contact_id = plain_contacts.contact_id
JOIN contacts
ON contacts.id = plain_contacts.contact_id
WHERE plain_contacts.has_email
AND NOT contacts.email_bad
AND NOT contacts.email_unsub
AND contacts_lists.list_id =67339
how can i optimize this query.. could you please explain…
Reformatting your query plan for clarity:
Two partial indexes might eliminate seq scans depending on your data distribution:
You might be missing an index on a foreign key:
Last but not least if you’ve never analyzed your data, you need to run:
If it’s still slow once all that is done, there isn’t much you can do short of merging your plain_contacts and your contacts tables: getting the above query plan in spite of the above indexes means most/all of your subscribers are subscribed to that particular list — in which case the above query plan is the fastest you’ll get.