mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 08:43:25 +08:00
PERF: Fix N+1 queries problem when listing topics list (#20971)
This performance regression was introduced in
7c6a8f1c74
where the preloading of tags in
`TopicQuery` was accidentally removed.
This commit is contained in:
parent
e586f6052f
commit
5bec894a8c
|
@ -721,7 +721,11 @@ class TopicQuery
|
|||
end
|
||||
end
|
||||
|
||||
result = filter_by_tags(result)
|
||||
if SiteSetting.tagging_enabled
|
||||
result = result.includes(:tags)
|
||||
result = filter_by_tags(result)
|
||||
end
|
||||
|
||||
result = apply_ordering(result, options)
|
||||
|
||||
all_listable_topics =
|
||||
|
@ -1151,8 +1155,6 @@ class TopicQuery
|
|||
end
|
||||
|
||||
def filter_by_tags(result)
|
||||
return result if !SiteSetting.tagging_enabled
|
||||
|
||||
tags_arg = @options[:tags]
|
||||
|
||||
if tags_arg && tags_arg.size > 0
|
||||
|
|
|
@ -106,6 +106,50 @@ RSpec.describe ListController do
|
|||
expect(first_item.css('[itemprop="url"]')[0]["href"]).to eq(topic.url)
|
||||
end
|
||||
|
||||
it "does not result in N+1 queries when topics have tags and tagging_enabled site setting is enabled" do
|
||||
SiteSetting.tagging_enabled = true
|
||||
tag = Fabricate(:tag)
|
||||
topic.tags << tag
|
||||
|
||||
# warm up
|
||||
get "/latest.json"
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
initial_sql_queries_count =
|
||||
track_sql_queries do
|
||||
get "/latest.json"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
body = response.parsed_body
|
||||
|
||||
expect(body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(topic.id)
|
||||
expect(body["topic_list"]["topics"][0]["tags"]).to contain_exactly(tag.name)
|
||||
end.count
|
||||
|
||||
tag2 = Fabricate(:tag)
|
||||
topic2 = Fabricate(:topic, tags: [tag2])
|
||||
|
||||
new_sql_queries_count =
|
||||
track_sql_queries do
|
||||
get "/latest.json"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
body = response.parsed_body
|
||||
|
||||
expect(body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(
|
||||
topic.id,
|
||||
topic2.id,
|
||||
)
|
||||
|
||||
expect(body["topic_list"]["topics"][0]["tags"]).to contain_exactly(tag2.name)
|
||||
expect(body["topic_list"]["topics"][1]["tags"]).to contain_exactly(tag.name)
|
||||
end.count
|
||||
|
||||
expect(new_sql_queries_count).to eq(initial_sql_queries_count)
|
||||
end
|
||||
|
||||
it "does not N+1 queries when topic featured users have different primary groups" do
|
||||
user.update!(primary_group: group)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user