Commit ee8f6bd9 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'sh-fix-nplus-one-groups-api' into 'master'

Fix N+1 queries with /api/v4/groups endpoint

See merge request gitlab-org/gitlab-ee!4371
parents 9850b74e b7c2e24c
---
title: FIx N+1 queries with /api/v4/groups endpoint
merge_request:
author:
type: performance
......@@ -54,6 +54,8 @@ module API
find_params[:parent] = find_group!(params[:id]) if params[:id]
groups = GroupsFinder.new(current_user, find_params).execute
# EE-only
groups = groups.preload(:ldap_group_links)
groups = groups.search(params[:search]) if params[:search].present?
groups = groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present?
groups = groups.reorder(params[:order_by] => params[:sort])
......
......@@ -31,6 +31,21 @@ describe API::Groups do
expect(json_response)
.to satisfy_one { |group| group['name'] == group1.name }
end
it 'avoids N+1 queries' do
# Establish baseline
get api("/groups", admin)
control = ActiveRecord::QueryRecorder.new do
get api("/groups", admin)
end
create(:group)
expect do
get api("/groups", admin)
end.not_to exceed_query_limit(control)
end
end
context "when authenticated as user" 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