Commit 3fed90cd authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Improve performance of project issues API

Remove unneeded preloading of notes. This can be really expensive
because it is not unusual for issues to have lots of notes.

Changelog: performance
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/60981
parent f9e91f21
...@@ -123,7 +123,7 @@ class Issue < ApplicationRecord ...@@ -123,7 +123,7 @@ class Issue < ApplicationRecord
scope :with_prometheus_alert_events, -> { joins(:issues_prometheus_alert_events) } scope :with_prometheus_alert_events, -> { joins(:issues_prometheus_alert_events) }
scope :with_self_managed_prometheus_alert_events, -> { joins(:issues_self_managed_prometheus_alert_events) } scope :with_self_managed_prometheus_alert_events, -> { joins(:issues_self_managed_prometheus_alert_events) }
scope :with_api_entity_associations, -> { scope :with_api_entity_associations, -> {
preload(:timelogs, :closed_by, :assignees, :author, :notes, :labels, preload(:timelogs, :closed_by, :assignees, :author, :labels,
milestone: { project: [:route, { namespace: :route }] }, milestone: { project: [:route, { namespace: :route }] },
project: [:route, { namespace: :route }]) project: [:route, { namespace: :route }])
} }
......
---
title: Improve performance of project issues API
merge_request: 60981
author:
type: performance
...@@ -186,7 +186,7 @@ RSpec.describe API::Issues do ...@@ -186,7 +186,7 @@ RSpec.describe API::Issues do
it 'avoids N+1 queries' do it 'avoids N+1 queries' do
get api("/projects/#{project.id}/issues", user) get api("/projects/#{project.id}/issues", user)
create_list(:issue, 3, project: project, closed_by: user) issues = create_list(:issue, 3, project: project, closed_by: user)
control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do
get api("/projects/#{project.id}/issues", user) get api("/projects/#{project.id}/issues", user)
...@@ -195,6 +195,10 @@ RSpec.describe API::Issues do ...@@ -195,6 +195,10 @@ RSpec.describe API::Issues do
milestone = create(:milestone, project: project) milestone = create(:milestone, project: project)
create(:issue, project: project, milestone: milestone, closed_by: create(:user)) create(:issue, project: project, milestone: milestone, closed_by: create(:user))
create(:note_on_issue, project: project, noteable: issues[0])
create(:note_on_issue, project: project, noteable: issues[0])
create(:note_on_issue, project: project, noteable: issues[1])
expect do expect do
get api("/projects/#{project.id}/issues", user) get api("/projects/#{project.id}/issues", user)
end.not_to exceed_all_query_limit(control_count) end.not_to exceed_all_query_limit(control_count)
......
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