2018-08-03 17:22:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-05 22:20:55 +00:00
|
|
|
# == Sortable concern
|
|
|
|
#
|
|
|
|
# Set default scope for ordering objects
|
|
|
|
#
|
|
|
|
module Sortable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2020-02-07 12:09:13 +00:00
|
|
|
scope :with_order_id_desc, -> { order(self.arel_table['id'].desc) }
|
|
|
|
scope :order_id_desc, -> { reorder(self.arel_table['id'].desc) }
|
|
|
|
scope :order_id_asc, -> { reorder(self.arel_table['id'].asc) }
|
|
|
|
scope :order_created_desc, -> { reorder(self.arel_table['created_at'].desc) }
|
|
|
|
scope :order_created_asc, -> { reorder(self.arel_table['created_at'].asc) }
|
|
|
|
scope :order_updated_desc, -> { reorder(self.arel_table['updated_at'].desc) }
|
|
|
|
scope :order_updated_asc, -> { reorder(self.arel_table['updated_at'].asc) }
|
2018-06-25 19:46:45 +00:00
|
|
|
scope :order_name_asc, -> { reorder(Arel::Nodes::Ascending.new(arel_table[:name].lower)) }
|
|
|
|
scope :order_name_desc, -> { reorder(Arel::Nodes::Descending.new(arel_table[:name].lower)) }
|
2015-02-05 22:20:55 +00:00
|
|
|
end
|
|
|
|
|
2018-08-27 12:35:31 +00:00
|
|
|
class_methods do
|
2015-02-06 00:49:41 +00:00
|
|
|
def order_by(method)
|
Extend CTE search optimisation to projects
When we use the `search` param on an `IssuableFinder`, we can run into
issues. We have trigram indexes to support these searches. On
GitLab.com, we often see Postgres's optimiser prioritise the (global)
trigram indexes over the index on `project_id`. For group and project
searches, we know that it will be quicker to filter by `project_id`
first, as it returns fewer rows in most cases.
For group issues search, we ran into this issue previously, and went
through the following iterations:
1. Use a CTE on the project IDs as an optimisation fence. This prevents
the planner from disregarding the index on `project_id`.
Unfortunately it breaks some types of sorting, like priority and
popularity, as they sort on a joined table.
2. Use a subquery for listing issues, and a CTE for counts. The subquery
- in the case of group lists - didn't help as much as the CTE, but
was faster than not including it. We can safely use a CTE for counts
as they don't have sorting.
Now, however, we're seeing the same issue in a project context. The
subquery doesn't help at all there (it would only return one row, after
all). In an attempt to keep total code complexity under control, this
commit removes the subquery optimisation and applies the CTE
optimisation only for sorts we know that are safe.
This means that for more complicated sorts (like priority and
popularity), the search will continue to be very slow. If this is a
high-priority issue, we can consider introducing further optimisations,
but this finder is already very complicated and additional complexity
has a cost.
The group CTE optimisation is controlled by the same feature flag as
before, `attempt_group_search_optimizations`, which is enabled by
default. The new project CTE optimisation is controlled by a new feature
flag, `attempt_project_search_optimizations`, which is disabled by
default.
2019-04-03 09:46:13 +00:00
|
|
|
simple_sorts.fetch(method.to_s, -> { all }).call
|
|
|
|
end
|
|
|
|
|
|
|
|
def simple_sorts
|
|
|
|
{
|
|
|
|
'created_asc' => -> { order_created_asc },
|
2019-08-15 10:56:33 +00:00
|
|
|
'created_at_asc' => -> { order_created_asc },
|
Extend CTE search optimisation to projects
When we use the `search` param on an `IssuableFinder`, we can run into
issues. We have trigram indexes to support these searches. On
GitLab.com, we often see Postgres's optimiser prioritise the (global)
trigram indexes over the index on `project_id`. For group and project
searches, we know that it will be quicker to filter by `project_id`
first, as it returns fewer rows in most cases.
For group issues search, we ran into this issue previously, and went
through the following iterations:
1. Use a CTE on the project IDs as an optimisation fence. This prevents
the planner from disregarding the index on `project_id`.
Unfortunately it breaks some types of sorting, like priority and
popularity, as they sort on a joined table.
2. Use a subquery for listing issues, and a CTE for counts. The subquery
- in the case of group lists - didn't help as much as the CTE, but
was faster than not including it. We can safely use a CTE for counts
as they don't have sorting.
Now, however, we're seeing the same issue in a project context. The
subquery doesn't help at all there (it would only return one row, after
all). In an attempt to keep total code complexity under control, this
commit removes the subquery optimisation and applies the CTE
optimisation only for sorts we know that are safe.
This means that for more complicated sorts (like priority and
popularity), the search will continue to be very slow. If this is a
high-priority issue, we can consider introducing further optimisations,
but this finder is already very complicated and additional complexity
has a cost.
The group CTE optimisation is controlled by the same feature flag as
before, `attempt_group_search_optimizations`, which is enabled by
default. The new project CTE optimisation is controlled by a new feature
flag, `attempt_project_search_optimizations`, which is disabled by
default.
2019-04-03 09:46:13 +00:00
|
|
|
'created_date' => -> { order_created_desc },
|
|
|
|
'created_desc' => -> { order_created_desc },
|
2019-08-15 10:56:33 +00:00
|
|
|
'created_at_desc' => -> { order_created_desc },
|
Extend CTE search optimisation to projects
When we use the `search` param on an `IssuableFinder`, we can run into
issues. We have trigram indexes to support these searches. On
GitLab.com, we often see Postgres's optimiser prioritise the (global)
trigram indexes over the index on `project_id`. For group and project
searches, we know that it will be quicker to filter by `project_id`
first, as it returns fewer rows in most cases.
For group issues search, we ran into this issue previously, and went
through the following iterations:
1. Use a CTE on the project IDs as an optimisation fence. This prevents
the planner from disregarding the index on `project_id`.
Unfortunately it breaks some types of sorting, like priority and
popularity, as they sort on a joined table.
2. Use a subquery for listing issues, and a CTE for counts. The subquery
- in the case of group lists - didn't help as much as the CTE, but
was faster than not including it. We can safely use a CTE for counts
as they don't have sorting.
Now, however, we're seeing the same issue in a project context. The
subquery doesn't help at all there (it would only return one row, after
all). In an attempt to keep total code complexity under control, this
commit removes the subquery optimisation and applies the CTE
optimisation only for sorts we know that are safe.
This means that for more complicated sorts (like priority and
popularity), the search will continue to be very slow. If this is a
high-priority issue, we can consider introducing further optimisations,
but this finder is already very complicated and additional complexity
has a cost.
The group CTE optimisation is controlled by the same feature flag as
before, `attempt_group_search_optimizations`, which is enabled by
default. The new project CTE optimisation is controlled by a new feature
flag, `attempt_project_search_optimizations`, which is disabled by
default.
2019-04-03 09:46:13 +00:00
|
|
|
'id_asc' => -> { order_id_asc },
|
|
|
|
'id_desc' => -> { order_id_desc },
|
|
|
|
'name_asc' => -> { order_name_asc },
|
|
|
|
'name_desc' => -> { order_name_desc },
|
|
|
|
'updated_asc' => -> { order_updated_asc },
|
2019-08-15 10:56:33 +00:00
|
|
|
'updated_at_asc' => -> { order_updated_asc },
|
|
|
|
'updated_desc' => -> { order_updated_desc },
|
|
|
|
'updated_at_desc' => -> { order_updated_desc }
|
Extend CTE search optimisation to projects
When we use the `search` param on an `IssuableFinder`, we can run into
issues. We have trigram indexes to support these searches. On
GitLab.com, we often see Postgres's optimiser prioritise the (global)
trigram indexes over the index on `project_id`. For group and project
searches, we know that it will be quicker to filter by `project_id`
first, as it returns fewer rows in most cases.
For group issues search, we ran into this issue previously, and went
through the following iterations:
1. Use a CTE on the project IDs as an optimisation fence. This prevents
the planner from disregarding the index on `project_id`.
Unfortunately it breaks some types of sorting, like priority and
popularity, as they sort on a joined table.
2. Use a subquery for listing issues, and a CTE for counts. The subquery
- in the case of group lists - didn't help as much as the CTE, but
was faster than not including it. We can safely use a CTE for counts
as they don't have sorting.
Now, however, we're seeing the same issue in a project context. The
subquery doesn't help at all there (it would only return one row, after
all). In an attempt to keep total code complexity under control, this
commit removes the subquery optimisation and applies the CTE
optimisation only for sorts we know that are safe.
This means that for more complicated sorts (like priority and
popularity), the search will continue to be very slow. If this is a
high-priority issue, we can consider introducing further optimisations,
but this finder is already very complicated and additional complexity
has a cost.
The group CTE optimisation is controlled by the same feature flag as
before, `attempt_group_search_optimizations`, which is enabled by
default. The new project CTE optimisation is controlled by a new feature
flag, `attempt_project_search_optimizations`, which is disabled by
default.
2019-04-03 09:46:13 +00:00
|
|
|
}
|
2015-02-05 22:20:55 +00:00
|
|
|
end
|
2016-07-26 21:21:20 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-28 00:26:56 +00:00
|
|
|
def highest_label_priority(target_type_column: nil, target_type: nil, target_column:, project_column:, excluded_labels: [])
|
2017-06-21 13:48:12 +00:00
|
|
|
query = Label.select(LabelPriority.arel_table[:priority].minimum)
|
|
|
|
.left_join_priorities
|
|
|
|
.joins(:label_links)
|
|
|
|
.where("label_priorities.project_id = #{project_column}")
|
|
|
|
.where("label_links.target_id = #{target_column}")
|
|
|
|
.reorder(nil)
|
2016-07-26 21:21:20 +00:00
|
|
|
|
2017-02-22 15:10:32 +00:00
|
|
|
query =
|
|
|
|
if target_type_column
|
|
|
|
query.where("label_links.target_type = #{target_type_column}")
|
|
|
|
else
|
|
|
|
query.where(label_links: { target_type: target_type })
|
|
|
|
end
|
2016-10-28 00:26:56 +00:00
|
|
|
|
|
|
|
query = query.where.not(title: excluded_labels) if excluded_labels.present?
|
2016-07-26 21:21:20 +00:00
|
|
|
|
|
|
|
query
|
|
|
|
end
|
2015-02-05 22:20:55 +00:00
|
|
|
end
|
|
|
|
end
|