Commit 90a01bbd authored by Stan Hu's avatar Stan Hu

Optimize issue search when sorting by weight

When searching for issues within a project or group, `IssuableFinder`
has an optimization that uses a CTE to filter the list of authorized
issues before searching within the list for text. This CTE optimization
fence is currently only used when the user specifies a simple sorting
parameter, such as `created_at`, and not popularity. These attributes
are generally stored directly with the model, so the CTE can easily do
an `ORDER BY` without joining another table (e.g. `award_emoji` for
popularity).

In EE, `weight` is another simple sorting parameter, and we should take
advantage of the CTE for that. This commit adds `weight` to
`Issue.simple_sorts`. When a user sorts by weight, this should help
speed up the search query and avoid a database statement timeout.

Improves https://gitlab.com/gitlab-org/gitlab/issues/34661
parent dc28f770
---
title: Optimize issue search when sorting by weight
merge_request: 24208
author:
type: performance
......@@ -145,7 +145,20 @@ module EE
end
class_methods do
# override
extend ::Gitlab::Utils::Override
override :simple_sorts
def simple_sorts
super.merge(
{
'weight' => -> { order_weight_asc.with_order_id_desc },
'weight_asc' => -> { order_weight_asc.with_order_id_desc },
'weight_desc' => -> { order_weight_desc.with_order_id_desc }
}
)
end
override :sort_by_attribute
def sort_by_attribute(method, excluded_labels: [])
case method.to_s
when 'weight', 'weight_asc' then order_weight_asc.with_order_id_desc
......
......@@ -210,6 +210,15 @@ describe Issue do
end
end
describe '.simple_sorts' do
it 'includes weight with other base keys' do
expect(Issue.simple_sorts.keys).to match_array(
%w(created_asc created_at_asc created_date created_desc created_at_desc
id_asc id_desc updated_desc updated_asc updated_at_asc updated_at_desc
weight weight_asc weight_desc))
end
end
describe '#sort' do
let(:project) { create(:project) }
......
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