Eliminate N+1 queries in Dashboard::TodosController

This appears to bring down the number of SQL queries on GitLab.com for
my Todos page from 672 to 100.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/43042
This commit is contained in:
Stan Hu 2019-06-23 07:54:08 -07:00
parent 7f6de7fe29
commit 211a00473b
4 changed files with 35 additions and 1 deletions

View file

@ -10,6 +10,7 @@ class Dashboard::TodosController < Dashboard::ApplicationController
def index def index
@sort = params[:sort] @sort = params[:sort]
@todos = @todos.page(params[:page]) @todos = @todos.page(params[:page])
@todos = @todos.with_api_entity_associations
return if redirect_out_of_range(@todos) return if redirect_out_of_range(@todos)
end end

View file

@ -170,7 +170,7 @@ module TodosHelper
end end
def todo_group_options def todo_group_options
groups = current_user.authorized_groups.map do |group| groups = current_user.authorized_groups.with_route.map do |group|
{ id: group.id, text: group.full_name } { id: group.id, text: group.full_name }
end end

View file

@ -0,0 +1,5 @@
---
title: Eliminate N+1 queries in Dashboard::TodosController
merge_request: 29954
author:
type: performance

View file

@ -44,6 +44,34 @@ describe Dashboard::TodosController do
end end
end end
context "with render_views" do
render_views
it 'avoids N+1 queries', :request_store do
merge_request = create(:merge_request, source_project: project)
create(:todo, project: project, author: author, user: user, target: merge_request)
create(:issue, project: project, assignees: [user])
group = create(:group)
group.add_owner(user)
get :index
control = ActiveRecord::QueryRecorder.new { get :index }
create(:issue, project: project, assignees: [user])
group_2 = create(:group)
group_2.add_owner(user)
project_2 = create(:project)
project_2.add_developer(user)
merge_request_2 = create(:merge_request, source_project: project_2)
create(:todo, project: project, author: author, user: user, target: merge_request_2)
expect { get :index }.not_to exceed_query_limit(control)
expect(response.status).to eq(200)
end
end
context 'group authorization' do context 'group authorization' do
it 'renders 404 when user does not have read access on given group' do it 'renders 404 when user does not have read access on given group' do
unauthorized_group = create(:group, :private) unauthorized_group = create(:group, :private)