My goal is to select articles where the primary_category_id (articles table) or any of the
secondary categories (articles_secondary_categories join table) are a given value. In this example query, category 1. I tried using other types of joins but the caveat here is that an article might not have any secondary categories.
SELECT DISTINCT articles.*
FROM articles
LEFT JOIN articles_secondary_categories AS categories
ON categories.article_id = articles.id
WHERE
(
primary_category_id = 1
OR
categories.category_id = 1
)
AND articles.state = "published"
AND edition_id = 1
ORDER BY publish_at DESC
LIMIT 10;
Any help optimizing this or alternatives are welcome. In a DB with 4k articles and 7k articles_secondary_categories (not categories) it takes 5 seconds to run this query.
You can reverse the query on the secondary categories:
It should give you a decent speed boost – just make sure you index categories.articles_id