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

Use varchar_pattern_ops indexes for labels

This allows the indexes to be used when using LIKE queries and searching
for prefixes.

Changelog: other
parent b6538ccd
# frozen_string_literal: true
class IndexLabelsUsingVarcharPatternOps < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
NEW_TITLE_INDEX_NAME = 'index_labels_on_title_varchar'
NEW_PROJECT_ID_TITLE_INDEX_NAME = 'index_labels_on_project_id_and_title_varchar_unique'
NEW_GROUP_ID_TITLE_INDEX_NAME = 'index_labels_on_group_id_and_title_varchar_unique'
NEW_GROUP_ID_INDEX_NAME = 'index_labels_on_group_id'
OLD_TITLE_INDEX_NAME = 'index_labels_on_title'
OLD_PROJECT_ID_TITLE_INDEX_NAME = 'index_labels_on_project_id_and_title_unique'
OLD_GROUP_ID_TITLE_INDEX_NAME = 'index_labels_on_group_id_and_title_unique'
OLD_GROUP_ID_PROJECT_ID_TITLE_INDEX_NAME = 'index_labels_on_group_id_and_project_id_and_title'
def up
add_concurrent_index :labels, :title, order: { title: :varchar_pattern_ops }, name: NEW_TITLE_INDEX_NAME
add_concurrent_index :labels, [:project_id, :title], where: "labels.group_id IS NULL", unique: true, order: { title: :varchar_pattern_ops }, name: NEW_PROJECT_ID_TITLE_INDEX_NAME
add_concurrent_index :labels, [:group_id, :title], where: "labels.project_id IS NULL", unique: true, order: { title: :varchar_pattern_ops }, name: NEW_GROUP_ID_TITLE_INDEX_NAME
add_concurrent_index :labels, :group_id, name: NEW_GROUP_ID_INDEX_NAME
remove_concurrent_index_by_name :labels, OLD_TITLE_INDEX_NAME
remove_concurrent_index_by_name :labels, OLD_PROJECT_ID_TITLE_INDEX_NAME
remove_concurrent_index_by_name :labels, OLD_GROUP_ID_TITLE_INDEX_NAME
remove_concurrent_index_by_name :labels, OLD_GROUP_ID_PROJECT_ID_TITLE_INDEX_NAME
end
def down
add_concurrent_index :labels, :title, name: OLD_TITLE_INDEX_NAME
add_concurrent_index :labels, [:project_id, :title], where: "labels.group_id IS NULL", unique: true, name: OLD_PROJECT_ID_TITLE_INDEX_NAME
add_concurrent_index :labels, [:group_id, :title], where: "labels.project_id IS NULL", unique: true, name: OLD_GROUP_ID_TITLE_INDEX_NAME
add_concurrent_index :labels, [:group_id, :project_id, :title], unique: true, name: OLD_GROUP_ID_PROJECT_ID_TITLE_INDEX_NAME
remove_concurrent_index_by_name :labels, NEW_TITLE_INDEX_NAME
remove_concurrent_index_by_name :labels, NEW_PROJECT_ID_TITLE_INDEX_NAME
remove_concurrent_index_by_name :labels, NEW_GROUP_ID_TITLE_INDEX_NAME
remove_concurrent_index_by_name :labels, NEW_GROUP_ID_INDEX_NAME
end
end
4430d4e0d688c85768201ab09056d60151fdc949b4b5f4ebc5397a99b9ec5f83
\ No newline at end of file
......@@ -25451,17 +25451,17 @@ CREATE INDEX index_label_priorities_on_priority ON label_priorities USING btree
CREATE UNIQUE INDEX index_label_priorities_on_project_id_and_label_id ON label_priorities USING btree (project_id, label_id);
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 ON labels USING btree (group_id);
CREATE UNIQUE INDEX index_labels_on_group_id_and_title_unique ON labels USING btree (group_id, title) WHERE (project_id IS NULL);
CREATE UNIQUE INDEX index_labels_on_group_id_and_title_varchar_unique ON labels USING btree (group_id, title varchar_pattern_ops) WHERE (project_id IS NULL);
CREATE INDEX index_labels_on_project_id ON labels USING btree (project_id);
CREATE UNIQUE INDEX index_labels_on_project_id_and_title_unique ON labels USING btree (project_id, title) WHERE (group_id IS NULL);
CREATE UNIQUE INDEX index_labels_on_project_id_and_title_varchar_unique ON labels USING btree (project_id, title varchar_pattern_ops) WHERE (group_id IS NULL);
CREATE INDEX index_labels_on_template ON labels USING btree (template) WHERE template;
CREATE INDEX index_labels_on_title ON labels USING btree (title);
CREATE INDEX index_labels_on_title_varchar ON labels USING btree (title varchar_pattern_ops);
CREATE INDEX index_labels_on_type_and_project_id ON labels USING btree (type, 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