Commit 6dd9d570 authored by Alishan Ladhani's avatar Alishan Ladhani

Eager load more issue associations to reduce N+1 queries

In an attempt to address performance issues outlined here: https://gitlab.com/gitlab-org/gitlab/-/issues/214482
parent 8e045622
---
title: 'Advanced Search API: Eager load more issue associations to reduce N+1 queries'
merge_request: 30233
author:
type: performance
......@@ -29,7 +29,7 @@ module Gitlab
when 'projects'
eager_load(projects, page, eager: [:route, :namespace, :compliance_framework_setting])
when 'issues'
eager_load(issues, page, eager: { project: [:route, :namespace] })
eager_load(issues, page, eager: { project: [:route, :namespace], labels: [], timelogs: [], assignees: [] })
when 'merge_requests'
eager_load(merge_requests, page, eager: { target_project: [:route, :namespace] })
when 'milestones'
......
......@@ -100,6 +100,24 @@ describe API::Search do
end
end
end
context 'for issues scope', :sidekiq_inline do
before do
create_list(:issue, 4, 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)
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)
end
end
end
describe 'GET /search' do
......
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