Commit 976293a6 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'ali/refactor-api-search-issue-preloads' into 'master'

Advanced Search API (Issues scope): Preload more associations to reduce N+1 queries

See merge request gitlab-org/gitlab!30778
parents 00ebc97a ab8cbdc1
---
title: Improve performance of Advanced Search API (Issues scope) by preloading more associations
merge_request: 30778
author:
type: performance
......@@ -160,19 +160,21 @@ describe API::Search do
context 'for issues scope', :sidekiq_inline do
before do
create_list(:issue, 4, project: project)
create_list(:issue, 2, project: project)
ensure_elasticsearch_index!
end
it 'avoids N+1 queries' do
control = ActiveRecord::QueryRecorder.new { get api(endpoint, user), params: { scope: 'issues', search: '*' } }
new_issues = create_list(:issue, 4, project: project)
create_list(:issue, 2, project: project)
create_list(:issue, 2, project: create(:project, group: group))
create_list(:issue, 2)
ensure_elasticsearch_index!
# Some N+1 queries still exist
expect { get api(endpoint, user), params: { scope: 'issues', search: '*' } }.not_to exceed_query_limit(control.count + new_issues.count * 4)
expect { get api(endpoint, user), params: { scope: 'issues', search: '*' } }.not_to exceed_query_limit(control.count + 2)
end
it_behaves_like 'pagination', scope: 'issues'
......
......@@ -22,7 +22,8 @@ module API
SCOPE_PRELOAD_METHOD = {
merge_requests: :with_api_entity_associations,
projects: :with_api_entity_associations
projects: :with_api_entity_associations,
issues: :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