Commit 46b33ef8 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'ee-34510-board-issues-sql-speedup' into 'master'

Port of !14198 to gitlab-ee

See merge request gitlab-org/gitlab-ee!2905
parents 8b1f28ac 1bbfd61e
...@@ -13,9 +13,15 @@ module Boards ...@@ -13,9 +13,15 @@ module Boards
issues = Boards::Issues::ListService.new(board_parent, current_user, filter_params).execute issues = Boards::Issues::ListService.new(board_parent, current_user, filter_params).execute
issues = issues.page(params[:page]).per(params[:per] || 20) issues = issues.page(params[:page]).per(params[:per] || 20)
make_sure_position_is_set(issues) unless Gitlab::Geo.secondary? make_sure_position_is_set(issues) unless Gitlab::Geo.secondary?
issues = issues.preload(:project,
:milestone,
:assignees,
labels: [:priorities],
notes: [:award_emoji, :author]
)
render json: { render json: {
issues: serialize_as_json(issues.preload(:project)), issues: serialize_as_json(issues),
size: issues.total_count size: issues.total_count
} }
end end
...@@ -78,14 +84,13 @@ module Boards ...@@ -78,14 +84,13 @@ module Boards
def serialize_as_json(resource) def serialize_as_json(resource)
resource.as_json( resource.as_json(
labels: true,
only: [:id, :iid, :project_id, :title, :confidential, :due_date, :relative_position], only: [:id, :iid, :project_id, :title, :confidential, :due_date, :relative_position],
labels: true,
include: { include: {
project: { only: [:id, :path] }, project: { only: [:id, :path] },
assignees: { only: [:id, :name, :username], methods: [:avatar_url] }, assignees: { only: [:id, :name, :username], methods: [:avatar_url] },
milestone: { only: [:id, :title] } milestone: { only: [:id, :title] }
}, }
user: current_user
) )
end end
end end
......
...@@ -130,7 +130,12 @@ class Label < ActiveRecord::Base ...@@ -130,7 +130,12 @@ class Label < ActiveRecord::Base
end end
def priority(project) def priority(project)
priorities.find_by(project: project).try(:priority) priority = if priorities.loaded?
priorities.first { |p| p.project == project }
else
priorities.find_by(project: project)
end
priority.try(:priority)
end end
def template? def template?
......
---
title: Optimize the boards' issues fetching.
merge_request: 14198
author:
type: other
...@@ -45,6 +45,17 @@ describe Boards::IssuesController do ...@@ -45,6 +45,17 @@ describe Boards::IssuesController do
expect(parsed_response.length).to eq 2 expect(parsed_response.length).to eq 2
expect(development.issues.map(&:relative_position)).not_to include(nil) expect(development.issues.map(&:relative_position)).not_to include(nil)
end end
it 'avoids N+1 database queries' do
create(:labeled_issue, project: project, labels: [development])
control_count = ActiveRecord::QueryRecorder.new { list_issues(user: user, board: board, list: list2) }.count
# 25 issues is bigger than the page size
# the relative position will ignore the `#make_sure_position_set` queries
create_list(:labeled_issue, 25, project: project, labels: [development], assignees: [johndoe], relative_position: 1)
expect { list_issues(user: user, board: board, list: list2) }.not_to exceed_query_limit(control_count)
end
end end
context 'with invalid list id' do context 'with invalid list id' 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