Fix N+1 in /projects REST endpoint

This commits fixes two N+1 in the /projeccts REST endpoint.

The related fields are `saml_providers` and `namespace_Settings`.
parent a569f9c0
---
title: Fix N+1 in /projects endpoint
merge_request: 57746
author:
type: performance
......@@ -11,7 +11,7 @@ module EE
override :preload_relation
def preload_relation(projects_relation, options = {})
super(projects_relation).with_compliance_framework_settings
super(projects_relation).with_compliance_framework_settings.with_group_saml_provider
end
end
......
......@@ -90,6 +90,27 @@ RSpec.describe API::Projects do
expect(json_response.first['id']).to eq project.id
end
end
context 'when there are several projects owned by groups' do
let_it_be(:admin) { create(:admin) }
it 'avoids N+1 queries' do
create(:project, :public, namespace: create(:group))
# Warming up context
get api('/projects', admin)
control = ActiveRecord::QueryRecorder.new do
get api('/projects', admin)
end
create_list(:project, 2, :public, namespace: create(:group))
expect do
get api('/projects', admin)
end.not_to exceed_query_limit(control.count)
end
end
end
describe 'GET /projects/:id' do
......
......@@ -127,7 +127,7 @@ module API
# as `:tags` are defined as: `has_many :tags, through: :taggings`
# N+1 is solved then by using `subject.tags.map(&:name)`
# MR describing the solution: https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/20555
super(projects_relation).preload(:group)
super(projects_relation).preload(group: :namespace_settings)
.preload(:ci_cd_settings)
.preload(:project_setting)
.preload(:container_expiration_policy)
......
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