Commit 631ee2a5 authored by Luke Duncalfe's avatar Luke Duncalfe

Further reduce N+1 on Jira pull endpoints

This builds on the optimisations in
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/57482 to further
reduce N+1s during loading source and target project associations.

The N+1 issues happen when a collection of merge requests are serialised
as `API::Github::Entities::PullRequest` entities.

This change also removes duplicate `preload` logic which was made
redundant after 7856804b.
parent 82a7f898
...@@ -316,7 +316,7 @@ class MergeRequest < ApplicationRecord ...@@ -316,7 +316,7 @@ class MergeRequest < ApplicationRecord
} }
scope :with_csv_entity_associations, -> { preload(:assignees, :approved_by_users, :author, :milestone, metrics: [:merged_by]) } scope :with_csv_entity_associations, -> { preload(:assignees, :approved_by_users, :author, :milestone, metrics: [:merged_by]) }
scope :with_jira_integration_associations, -> { preload(:metrics, :assignees, :author, :target_project, :source_project) } scope :with_jira_integration_associations, -> { preload_routables.preload(:metrics, :assignees, :author) }
scope :by_target_branch_wildcard, ->(wildcard_branch_name) do scope :by_target_branch_wildcard, ->(wildcard_branch_name) do
where("target_branch LIKE ?", ApplicationRecord.sanitize_sql_like(wildcard_branch_name).tr('*', '%')) where("target_branch LIKE ?", ApplicationRecord.sanitize_sql_like(wildcard_branch_name).tr('*', '%'))
......
---
title: Resolve more N+1 issues in Jira pulls API
merge_request: 57658
author:
type: performance
...@@ -197,16 +197,13 @@ module API ...@@ -197,16 +197,13 @@ module API
# Self-hosted Jira (tested on 7.11.1) requests this endpoint right # Self-hosted Jira (tested on 7.11.1) requests this endpoint right
# after fetching branches. # after fetching branches.
# rubocop: disable CodeReuse/ActiveRecord
get ':namespace/:project/events' do get ':namespace/:project/events' do
user_project = find_project_with_access(params) user_project = find_project_with_access(params)
merge_requests = authorized_merge_requests_for_project(user_project) merge_requests = authorized_merge_requests_for_project(user_project)
merge_requests = merge_requests.preload(:author, :assignees, :metrics, source_project: :namespace, target_project: :namespace)
present paginate(merge_requests), with: ::API::Github::Entities::PullRequestEvent present paginate(merge_requests), with: ::API::Github::Entities::PullRequestEvent
end end
# rubocop: enable CodeReuse/ActiveRecord
params do params do
use :project_full_path use :project_full_path
......
...@@ -246,7 +246,10 @@ RSpec.describe API::V3::Github do ...@@ -246,7 +246,10 @@ RSpec.describe API::V3::Github do
control_count = ActiveRecord::QueryRecorder.new { perform_request }.count control_count = ActiveRecord::QueryRecorder.new { perform_request }.count
create(:merge_request, source_project: project, source_branch: 'fix') project3 = create(:project, :repository, creator: user)
project3.add_maintainer(user)
assignee3 = create(:user)
create(:merge_request, source_project: project3, target_project: project3, author: user, assignees: [assignee3])
expect { perform_request }.not_to exceed_query_limit(control_count) expect { perform_request }.not_to exceed_query_limit(control_count)
end end
......
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