Commit 6d3ffe67 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Fix N+1 in issues API when loading parent epics

We preload the epic's group and route to prevent an N+1 when presenting
the issues' epics.

Changelog: performance
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/61852
parent a7608d29
......@@ -78,7 +78,7 @@ module EE
class_methods do
def with_api_entity_associations
super.preload(:epic)
super.preload(epic: { group: :route })
end
# override
......
---
title: Fix N+1 in issues API when loading parent epics
merge_request: 61852
author:
type: performance
......@@ -7,7 +7,7 @@ module EE
extend ActiveSupport::Concern
prepended do
with_options if: -> (issue, _) { issue.project.group&.feature_available?(:epics) } do
with_options if: -> (issue, _) { issue.project.namespace.group? && issue.project.namespace.feature_available?(:epics) } do
expose :epic_iid do |issue|
authorized_epic_for(issue)&.iid
end
......
......@@ -269,6 +269,28 @@ RSpec.describe API::Issues, :mailer do
expect_response_contain_exactly(iteration_1_issue.id)
end
end
it 'avoids N+1 queries' do
stub_licensed_features(epics: true)
group.add_developer(user)
subgroup_1 = create(:group, parent: group)
subgroup_1_project = create(:project, group: subgroup_1)
create(:issue, project: subgroup_1_project, epic: create(:epic, group: subgroup_1))
get api("/groups/#{group.id}/issues", user)
control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { get api("/groups/#{group.id}/issues", user) }
subgroup_2 = create(:group, parent: group)
subgroup_2_project = create(:project, group: subgroup_2)
create(:issue, project: subgroup_2_project, epic: create(:epic, group: subgroup_2))
expect { get api("/groups/#{group.id}/issues", user) }.not_to exceed_query_limit(control_count)
end
end
describe "GET /projects/:id/issues" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment