Commit aa85d7bc authored by David Kim's avatar David Kim

Add a new index to improve query performance

parent cba2df9e
---
title: Adjust indices to improve query performance for notification_settings
merge_request: 58895
author:
type: performance
# frozen_string_literal: true
class AddSourceAndLevelIndexOnNotificationSettings < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_WITH_SOURCE_LEVEL_USER_NAME = 'index_notification_settings_on_source_and_level_and_user'
INDEX_WITH_SOURCE_NAME = 'index_notification_settings_on_source_id_and_source_type'
INDEX_WITH_USER_NAME = 'index_notification_settings_on_user_id'
disable_ddl_transaction!
def up
add_concurrent_index :notification_settings, [:source_id, :source_type, :level, :user_id], name: INDEX_WITH_SOURCE_LEVEL_USER_NAME
remove_concurrent_index_by_name :notification_settings, INDEX_WITH_SOURCE_NAME # Above index expands this index
remove_concurrent_index_by_name :notification_settings, INDEX_WITH_USER_NAME # It is redundant as we already have unique index on (user_id, source_id, source_type)
end
def down
add_concurrent_index :notification_settings, [:source_id, :source_type], name: INDEX_WITH_SOURCE_NAME
add_concurrent_index :notification_settings, [:user_id], name: INDEX_WITH_USER_NAME
remove_concurrent_index_by_name :notification_settings, INDEX_WITH_SOURCE_LEVEL_USER_NAME
end
end
6c44623655732e9c2916f7a71d51f53e819a216b8936d20d28d6acf37cc94fdf
\ No newline at end of file
......@@ -23220,9 +23220,7 @@ CREATE INDEX index_notes_on_project_id_and_noteable_type ON notes USING btree (p
CREATE INDEX index_notes_on_review_id ON notes USING btree (review_id);
CREATE INDEX index_notification_settings_on_source_id_and_source_type ON notification_settings USING btree (source_id, source_type);
CREATE INDEX index_notification_settings_on_user_id ON notification_settings USING btree (user_id);
CREATE INDEX index_notification_settings_on_source_and_level_and_user ON notification_settings USING btree (source_id, source_type, level, user_id);
CREATE UNIQUE INDEX index_notifications_on_user_id_and_source_id_and_source_type ON notification_settings USING btree (user_id, source_id, source_type);
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