DEV: Prefer nested queries (#23464)

Some sites have a large number of categories and fetching the category
IDs or category topic IDs just to build another query can take a long
time or resources (i.e. memory).
This commit is contained in:
Bianca Nenciu
2023-09-25 19:38:54 +03:00
committed by GitHub
parent 1cc2c8fcb6
commit 3700514819
5 changed files with 5 additions and 7 deletions

View File

@ -569,8 +569,7 @@ class Topic < ActiveRecord::Base
topics = topics.limit(opts[:limit]) if opts[:limit]
# Remove category topics
category_topic_ids = Category.pluck(:topic_id).compact!
topics = topics.where("topics.id NOT IN (?)", category_topic_ids) if category_topic_ids.present?
topics = topics.where.not(id: Category.select(:topic_id).where.not(topic_id: nil))
# Remove muted and shared draft categories
remove_category_ids =