Commit f57750a8 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch '215710-improve-performance-of-search-api-advanced-milestones-scope' into 'master'

Fix N+1 queries for Elastic Search milestones scope

See merge request gitlab-org/gitlab!33327
parents 0901d4b2 6940968e
......@@ -28,6 +28,7 @@ class Milestone < ApplicationRecord
scope :order_by_name_asc, -> { order(Arel::Nodes::Ascending.new(arel_table[:title].lower)) }
scope :reorder_by_due_date_asc, -> { reorder(Gitlab::Database.nulls_last_order('due_date', 'ASC')) }
scope :with_api_entity_associations, -> { preload(project: [:project_feature, :route, namespace: :route]) }
validates_associated :milestone_releases, message: -> (_, obj) { obj[:value].map(&:errors).map(&:full_messages).join(",") }
......
---
title: Fix N+1 queries for Elastic Search milestones scope.
merge_request: 33327
author:
type: performance
......@@ -212,6 +212,16 @@ RSpec.describe API::Search do
end
it_behaves_like 'pagination', scope: 'milestones'
it 'avoids N+1 queries' do
control = ActiveRecord::QueryRecorder.new { get api(endpoint, user), params: { scope: 'milestones', search: '*' } }
create_list(:milestone, 3, project: project)
create_list(:milestone, 2, project: create(:project, :public))
ensure_elasticsearch_index!
expect { get api(endpoint, user), params: { scope: 'milestones', search: '*' } }.not_to exceed_query_limit(control.count)
end
end
context 'for users scope', :sidekiq_inline do
......
......@@ -23,7 +23,8 @@ module API
SCOPE_PRELOAD_METHOD = {
merge_requests: :with_api_entity_associations,
projects: :with_api_entity_associations,
issues: :with_api_entity_associations
issues: :with_api_entity_associations,
milestones: :with_api_entity_associations
}.freeze
def search(additional_params = {})
......
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