From f9e217872d26c161bc64dc7bb9456b1b22bc3259 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sat, 19 Jan 2019 22:10:20 -0800 Subject: Eliminate N+1 queries in /api/groups/:id In https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/15475/diffs, a significant amount of work went into eliminating N+1 queries in the /api/groups/:id/projects endpoint. We can reuse the `Entities::Project.prepare_relation` call on the projects. In a group with 2,573 projects on GitLab.com, this change significantly improves performance: * 18019 SQL queries down to 21 * Time spent in DB: 70 s down to 384 ms Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/49845 --- spec/requests/api/groups_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'spec/requests/api/groups_spec.rb') diff --git a/spec/requests/api/groups_spec.rb b/spec/requests/api/groups_spec.rb index c9dfc5c4a7e..7176bc23e34 100644 --- a/spec/requests/api/groups_spec.rb +++ b/spec/requests/api/groups_spec.rb @@ -382,6 +382,20 @@ describe API::Groups do expect(response_project_ids(json_response, 'shared_projects')) .to contain_exactly(projects[:public].id, projects[:internal].id) end + + it 'avoids N+1 queries' do + get api("/groups/#{group1.id}", admin) + + control_count = ActiveRecord::QueryRecorder.new do + get api("/groups/#{group1.id}", admin) + end.count + + create(:project, namespace: group1) + + expect do + get api("/groups/#{group1.id}", admin) + end.not_to exceed_query_limit(control_count) + end end context "when authenticated as admin" do -- cgit v1.2.1