Commit a23ede4b authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'speed-up-label-query-for-optimized-issuable-finders' into 'master'

Improve label search with issuable optimization

See merge request gitlab-org/gitlab!48238
parents d266b8e5 06a0bbba
# frozen_string_literal: true # frozen_string_literal: true
module OptimizedIssuableLabelFilter module OptimizedIssuableLabelFilter
extend ActiveSupport::Concern
prepended do
extend Gitlab::Cache::RequestCache
# Avoid repeating label queries times when the finder is instantiated multiple times during the request.
request_cache(:find_label_ids) { [root_namespace.id, params.label_names] }
end
def by_label(items) def by_label(items)
return items unless params.labels? return items unless params.labels?
...@@ -41,7 +50,7 @@ module OptimizedIssuableLabelFilter ...@@ -41,7 +50,7 @@ module OptimizedIssuableLabelFilter
def issuables_with_selected_labels(items, target_model) def issuables_with_selected_labels(items, target_model)
if root_namespace if root_namespace
all_label_ids = find_label_ids(root_namespace) all_label_ids = find_label_ids
# Found less labels in the DB than we were searching for. Return nothing. # Found less labels in the DB than we were searching for. Return nothing.
return items.none if all_label_ids.size != params.label_names.size return items.none if all_label_ids.size != params.label_names.size
...@@ -57,18 +66,20 @@ module OptimizedIssuableLabelFilter ...@@ -57,18 +66,20 @@ module OptimizedIssuableLabelFilter
items items
end end
def find_label_ids(root_namespace) def find_label_ids
finder_params = { group_labels = Label
include_subgroups: true, .where(project_id: nil)
include_ancestor_groups: true, .where(title: params.label_names)
include_descendant_groups: true, .where(group_id: root_namespace.self_and_descendants.select(:id))
group: root_namespace,
title: params.label_names project_labels = Label
} .where(group_id: nil)
.where(title: params.label_names)
LabelsFinder .where(project_id: Project.select(:id).where(namespace_id: root_namespace.self_and_descendants.select(:id)))
.new(nil, finder_params)
.execute(skip_authorization: true) Label
.from_union([group_labels, project_labels], remove_duplicates: false)
.reorder(nil)
.pluck(:title, :id) .pluck(:title, :id)
.group_by(&:first) .group_by(&:first)
.values .values
......
---
title: Replace wrong index definition on labels (project_id, title)
merge_request: 48238
author:
type: other
# frozen_string_literal: true
class ReplaceUnusedLabelsIndex < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
NEW_INDEX_NAME = 'index_labels_on_group_id_and_title_with_null_project_id'
OLD_INDEX_NAME = 'index_labels_on_group_id_and_title'
def up
add_concurrent_index :labels, [:group_id, :title], where: 'project_id IS NULL', name: NEW_INDEX_NAME
remove_concurrent_index_by_name :labels, OLD_INDEX_NAME
end
def down
add_concurrent_index :labels, [:group_id, :title], where: 'project_id = NULL::integer', name: OLD_INDEX_NAME
remove_concurrent_index_by_name :labels, NEW_INDEX_NAME
end
end
8b60a6bc892f9700df81de9909595544f9f820621a210906a249428ddec9eefa
\ No newline at end of file
...@@ -21202,7 +21202,7 @@ CREATE UNIQUE INDEX index_label_priorities_on_project_id_and_label_id ON label_p ...@@ -21202,7 +21202,7 @@ CREATE UNIQUE INDEX index_label_priorities_on_project_id_and_label_id ON label_p
CREATE UNIQUE INDEX index_labels_on_group_id_and_project_id_and_title ON labels USING btree (group_id, project_id, title); CREATE UNIQUE INDEX index_labels_on_group_id_and_project_id_and_title ON labels USING btree (group_id, project_id, title);
CREATE INDEX index_labels_on_group_id_and_title ON labels USING btree (group_id, title) WHERE (project_id = NULL::integer); CREATE INDEX index_labels_on_group_id_and_title_with_null_project_id ON labels USING btree (group_id, title) WHERE (project_id IS NULL);
CREATE INDEX index_labels_on_project_id ON labels USING btree (project_id); CREATE INDEX index_labels_on_project_id ON labels USING btree (project_id);
......
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