2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-05 17:20:55 -05:00
|
|
|
# == Sortable concern
|
|
|
|
#
|
|
|
|
# Set default scope for ordering objects
|
|
|
|
#
|
|
|
|
module Sortable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2018-08-06 15:38:37 -04:00
|
|
|
scope :with_order_id_desc, -> { order(id: :desc) }
|
2015-11-12 10:30:39 -05:00
|
|
|
scope :order_id_desc, -> { reorder(id: :desc) }
|
2016-01-06 11:29:44 -05:00
|
|
|
scope :order_id_asc, -> { reorder(id: :asc) }
|
2015-11-03 05:54:43 -05:00
|
|
|
scope :order_created_desc, -> { reorder(created_at: :desc) }
|
|
|
|
scope :order_created_asc, -> { reorder(created_at: :asc) }
|
|
|
|
scope :order_updated_desc, -> { reorder(updated_at: :desc) }
|
|
|
|
scope :order_updated_asc, -> { reorder(updated_at: :asc) }
|
2018-06-25 15:46:45 -04: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 17:20:55 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 08:35:31 -04:00
|
|
|
class_methods do
|
2015-02-05 19:49:41 -05: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 05:46:13 -04:00
|
|
|
simple_sorts.fetch(method.to_s, -> { all }).call
|
|
|
|
end
|
|
|
|
|
|
|
|
def simple_sorts
|
|
|
|
{
|
|
|
|
'created_asc' => -> { order_created_asc },
|
2019-08-15 06:56:33 -04: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 05:46:13 -04:00
|
|
|
'created_date' => -> { order_created_desc },
|
|
|
|
'created_desc' => -> { order_created_desc },
|
2019-08-15 06:56:33 -04: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 05:46:13 -04: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 06:56:33 -04: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 05:46:13 -04:00
|
|
|
}
|
2015-02-05 17:20:55 -05:00
|
|
|
end
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-27 20:26:56 -04:00
|
|
|
def highest_label_priority(target_type_column: nil, target_type: nil, target_column:, project_column:, excluded_labels: [])
|
2017-06-21 09:48:12 -04: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 17:21:20 -04:00
|
|
|
|
2017-02-22 10:10:32 -05: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-27 20:26:56 -04:00
|
|
|
|
|
|
|
query = query.where.not(title: excluded_labels) if excluded_labels.present?
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
query
|
|
|
|
end
|
2015-02-05 17:20:55 -05:00
|
|
|
end
|
|
|
|
end
|