mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
PERF: Use a subquery when excluding a tag from topic query. (#14577)
When a tag with alot of topics is used, we end up allocating a Ruby
array of all the topic ids. Instead, we can just use a subquery here and
handle all of the exclusion logic in PG.
Follow-up to ae13839f98
This commit is contained in:
parent
5ffb810c68
commit
e3c724f79f
1 changed files with 9 additions and 2 deletions
|
@ -693,8 +693,15 @@ class TopicQuery
|
||||||
result = result.where.not(id: TopicTag.distinct.pluck(:topic_id))
|
result = result.where.not(id: TopicTag.distinct.pluck(:topic_id))
|
||||||
end
|
end
|
||||||
|
|
||||||
if @options[:exclude_tag] && tag = Tag.find_by(name: @options[:exclude_tag])
|
if @options[:exclude_tag].present?
|
||||||
result = result.where.not(id: TopicTag.distinct.where(tag_id: tag.id).pluck(:topic_id))
|
result = result.where(<<~SQL, name: @options[:exclude_tag])
|
||||||
|
topics.id NOT IN (
|
||||||
|
SELECT topic_tags.topic_id
|
||||||
|
FROM topic_tags
|
||||||
|
INNER JOIN tags ON tags.id = topic_tags.tag_id
|
||||||
|
WHERE tags.name = :name
|
||||||
|
)
|
||||||
|
SQL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue