Optimize API /groups/:id/projects by preloading associations
Closes #40308
This commit is contained in:
parent
c594659fea
commit
88e3ce30ae
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
title: Optimize API /groups/:id/projects by preloading fork_networks table
|
||||||
|
merge_request:
|
||||||
|
author:
|
||||||
|
type: performance
|
|
@ -172,6 +172,7 @@ module API
|
||||||
get ":id/projects" do
|
get ":id/projects" do
|
||||||
group = find_group!(params[:id])
|
group = find_group!(params[:id])
|
||||||
projects = GroupProjectsFinder.new(group: group, current_user: current_user, params: project_finder_params).execute
|
projects = GroupProjectsFinder.new(group: group, current_user: current_user, params: project_finder_params).execute
|
||||||
|
projects = projects.preload(:fork_network, :forked_project_link, :project_feature, :project_group_links, :tags, :taggings, :group, :namespace)
|
||||||
projects = reorder_projects(projects)
|
projects = reorder_projects(projects)
|
||||||
entity = params[:simple] ? Entities::BasicProjectDetails : Entities::Project
|
entity = params[:simple] ? Entities::BasicProjectDetails : Entities::Project
|
||||||
present paginate(projects), with: entity, current_user: current_user
|
present paginate(projects), with: entity, current_user: current_user
|
||||||
|
|
|
@ -401,6 +401,20 @@ describe API::Groups do
|
||||||
|
|
||||||
expect(response).to have_gitlab_http_status(404)
|
expect(response).to have_gitlab_http_status(404)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'avoids N+1 queries' do
|
||||||
|
get api("/groups/#{group1.id}/projects", admin)
|
||||||
|
|
||||||
|
control_count = ActiveRecord::QueryRecorder.new do
|
||||||
|
get api("/groups/#{group1.id}/projects", admin)
|
||||||
|
end.count
|
||||||
|
|
||||||
|
create(:project, namespace: group1)
|
||||||
|
|
||||||
|
expect do
|
||||||
|
get api("/groups/#{group1.id}/projects", admin)
|
||||||
|
end.not_to exceed_query_limit(control_count)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when using group path in URL' do
|
context 'when using group path in URL' do
|
||||||
|
|
Loading…
Reference in New Issue