Skip to content

A platform for community discussion. Free, open, simple.

PERF: improve perf of topic suggested query with limit_suggested_to_category

Sam41a604a764c1
diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index 14aaaa8bbc7..572f19be085 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -820,7 +820,12 @@ class TopicQuery
def random_suggested(topic, count, excluded_topic_ids = [])
result = default_results(unordered: true, per_page: count).where(closed: false, archived: false)
- excluded_topic_ids += Category.topic_ids.to_a
+
+ if SiteSetting.limit_suggested_to_category
+ excluded_topic_ids += Category.where(id: topic.category_id).pluck(:id)
+ else
+ excluded_topic_ids += Category.topic_ids.to_a
+ end
result = result.where("topics.id NOT IN (?)", excluded_topic_ids) unless excluded_topic_ids.empty?
result = remove_muted_categories(result, @user)
@@ -837,7 +842,8 @@ class TopicQuery
#
# we over select in case cache is stale
max = (count * 1.3).to_i
- ids = RandomTopicSelector.next(max) + RandomTopicSelector.next(max, topic.category)
+ ids = SiteSetting.limit_suggested_to_category ? [] : RandomTopicSelector.next(max)
+ ids.concat(RandomTopicSelector.next(max, topic.category))
result.where(id: ids.uniq)
end