Commit b539ba50 authored by Matthias Kaeppler's avatar Matthias Kaeppler

Fix duplicate query problem

We were loading board issues twice, because we invalidated the
issues scope after loading them by adding another `preload`,
so the query fired again during template rendering.
By eager-loading issues upfront, we can enforce this to always be
just one query.
parent 3af09062
......@@ -24,17 +24,14 @@ module Boards
push_frontend_feature_flag(:board_search_optimization, board.group)
end
# rubocop: disable CodeReuse/ActiveRecord
def index
list_service = Boards::Issues::ListService.new(board_parent, current_user, filter_params)
issues = list_service.execute
issues = issues.page(params[:page]).per(params[:per] || 20).without_count
issues = issues_from(list_service)
Issue.move_nulls_to_end(issues) if Gitlab::Database.read_write?
issues = issues.preload(associations_to_preload)
render_issues(issues, list_service.metadata)
end
# rubocop: enable CodeReuse/ActiveRecord
def create
service = Boards::Issues::CreateService.new(board_parent, project, current_user, issue_params)
......@@ -67,6 +64,14 @@ module Boards
private
def issues_from(list_service)
issues = list_service.execute
issues.page(params[:page]).per(params[:per] || 20)
.without_count
.preload(associations_to_preload) # rubocop: disable CodeReuse/ActiveRecord
.load
end
def associations_to_preload
[
:milestone,
......
---
title: Fix redundant query execution when loading board issues
merge_request: 27505
author:
type: performance
......@@ -112,6 +112,13 @@ describe Boards::IssuesController do
expect { list_issues(user: user, board: group_board, list: list3) }.not_to exceed_query_limit(control_count + (2 * 8 - 1))
end
it 'does not query issues table more than once' do
recorder = ActiveRecord::QueryRecorder.new { list_issues(user: user, board: board, list: list1) }
query_count = recorder.occurrences.select { |query,| query.start_with?('SELECT issues.*') }.each_value.first
expect(query_count).to eq(1)
end
end
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