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

@ -20,7 +20,7 @@ class AboutController < ApplicationController
unless current_user.staff?
RateLimiter.new(current_user, "live_post_counts", 1, 10.minutes).performed!
end
category_topic_ids = Category.pluck(:topic_id).compact!
category_topic_ids = Category.select(:topic_id).where.not(topic_id: nil)
public_topics =
Topic.listable_topics.visible.secured(Guardian.new(nil)).where.not(id: category_topic_ids)
stats = { public_topic_count: public_topics.count }

View File

@ -931,7 +931,7 @@ class ApplicationController < ActionController::Base
Discourse
.cache
.fetch(key, expires_in: 10.minutes) do
category_topic_ids = Category.pluck(:topic_id).compact
category_topic_ids = Category.select(:topic_id).where.not(topic_id: nil)
@top_viewed =
TopicQuery
.new(nil, except_topic_ids: category_topic_ids)

View File

@ -344,7 +344,7 @@ class TopicsController < ApplicationController
topic = Topic.find_by(id: params[:id])
guardian.ensure_can_edit!(topic)
category = Category.where(id: params[:category_id].to_i).first
category = Category.find_by(id: params[:category_id].to_i)
guardian.ensure_can_publish_topic!(topic, category)
row_count = SharedDraft.where(topic_id: topic.id).update_all(category_id: category.id)