mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: N+1 queries when viewing tags (#19891)
When the `tags_listed_by_group` site setting is enabled, we were seeing the N+1 queries problem when multiple `TagGroup` records are listed. This commit fixes that by ensuring that we are not filtering through the `tags` association after the association has been eager loaded.
This commit is contained in:
committed by
GitHub
parent
14983c5b8e
commit
1e8a1a0d24
@@ -80,9 +80,10 @@ RSpec.describe TagsController do
|
||||
|
||||
it "works for tags in groups" do
|
||||
tag_group = Fabricate(:tag_group, tags: [test_tag, topic_tag, synonym])
|
||||
get "/tags.json"
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
get "/tags.json"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
tags = response.parsed_body["tags"]
|
||||
expect(tags.length).to eq(0)
|
||||
group = response.parsed_body.dig("extras", "tag_groups")&.first
|
||||
@@ -90,6 +91,47 @@ RSpec.describe TagsController do
|
||||
expect(group["tags"].length).to eq(2)
|
||||
expect(group["tags"].map { |t| t["id"] }).to contain_exactly(test_tag.name, topic_tag.name)
|
||||
end
|
||||
|
||||
it "does not result in N+1 queries with multiple tag_groups" do
|
||||
tag_group = Fabricate(:tag_group, tags: [test_tag, topic_tag, synonym])
|
||||
|
||||
# warm up
|
||||
get "/tags.json"
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
initial_sql_queries_count =
|
||||
track_sql_queries do
|
||||
get "/tags.json"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
tag_groups = response.parsed_body.dig("extras", "tag_groups")
|
||||
|
||||
expect(tag_groups.length).to eq(1)
|
||||
expect(tag_groups.map { |tag_group| tag_group["name"] }).to contain_exactly(
|
||||
tag_group.name,
|
||||
)
|
||||
end.length
|
||||
|
||||
tag_group2 = Fabricate(:tag_group, tags: [topic_tag])
|
||||
|
||||
new_sql_queries_count =
|
||||
track_sql_queries do
|
||||
get "/tags.json"
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
tag_groups = response.parsed_body.dig("extras", "tag_groups")
|
||||
|
||||
expect(tag_groups.length).to eq(2)
|
||||
expect(tag_groups.map { |tag_group| tag_group["name"] }).to contain_exactly(
|
||||
tag_group.name,
|
||||
tag_group2.name,
|
||||
)
|
||||
end.length
|
||||
|
||||
expect(new_sql_queries_count).to be <= initial_sql_queries_count
|
||||
end
|
||||
end
|
||||
|
||||
context "with tags_listed_by_group disabled" do
|
||||
|
||||
Reference in New Issue
Block a user