Commit e85e1011 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '227383-fix-search-n-plus-1' into 'master'

Fix N+1 queries for milestone search [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!57715
parents 5affad35 5d488073
...@@ -89,6 +89,10 @@ class Milestone < ApplicationRecord ...@@ -89,6 +89,10 @@ class Milestone < ApplicationRecord
.order(:project_id, :group_id, :due_date).select('DISTINCT ON (project_id, group_id) id') .order(:project_id, :group_id, :due_date).select('DISTINCT ON (project_id, group_id) id')
end end
def self.with_web_entity_associations
preload(:group, project: [:project_feature, group: [:parent], namespace: :route])
end
def participants def participants
User.joins(assigned_issues: :milestone).where("milestones.id = ?", id).distinct User.joins(assigned_issues: :milestone).where("milestones.id = ?", id).distinct
end end
......
...@@ -10,7 +10,8 @@ class SearchServicePresenter < Gitlab::View::Presenter::Delegated ...@@ -10,7 +10,8 @@ class SearchServicePresenter < Gitlab::View::Presenter::Delegated
issues: :with_web_entity_associations, issues: :with_web_entity_associations,
merge_requests: :with_web_entity_associations, merge_requests: :with_web_entity_associations,
epics: :with_web_entity_associations, epics: :with_web_entity_associations,
notes: :with_web_entity_associations notes: :with_web_entity_associations,
milestones: :with_web_entity_associations
}.freeze }.freeze
SORT_ENABLED_SCOPES = %w(issues merge_requests).freeze SORT_ENABLED_SCOPES = %w(issues merge_requests).freeze
......
---
title: Fix N+1 for searching milestone scope
merge_request: 57715
author:
type: performance
...@@ -10,6 +10,15 @@ module EE ...@@ -10,6 +10,15 @@ module EE
has_many :boards has_many :boards
end end
class_methods do
extend ::Gitlab::Utils::Override
override :with_web_entity_associations
def with_web_entity_associations
super.preload(project: [group: [:saml_provider]])
end
end
def supports_milestone_charts? def supports_milestone_charts?
resource_parent&.feature_available?(:milestone_charts) && weight_available? resource_parent&.feature_available?(:milestone_charts) && weight_available?
end end
......
...@@ -78,9 +78,7 @@ RSpec.describe 'Global elastic search', :elastic, :sidekiq_inline do ...@@ -78,9 +78,7 @@ RSpec.describe 'Global elastic search', :elastic, :sidekiq_inline do
let(:object) { :milestone } let(:object) { :milestone }
let(:creation_args) { { project: project } } let(:creation_args) { { project: project } }
let(:path) { search_path(search: '*', scope: 'milestones') } let(:path) { search_path(search: '*', scope: 'milestones') }
# N+1 queries still exist and will be fixed per let(:query_count_multiplier) { 0 }
# https://gitlab.com/gitlab-org/gitlab/-/issues/325887
let(:query_count_multiplier) { 1 }
it_behaves_like 'an efficient database result' it_behaves_like 'an efficient database result'
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