2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-10-03 17:21:20 +08:00
discourse/db/migrate/20250619140858_add_jsonb_indexes_to_notifications.rb
Amanda Alves Branquinho ca36d98d55
PERF: Add indexes to improve user merger performance (#33271)
- Add indexes in some places where we were seeing bottlenecks
- Remove redundant ILIKE query

---------

Co-authored-by: Gerhard Schlager <gerhard.schlager@discourse.org>
2025-07-10 18:18:40 -03:00

55 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class AddJsonbIndexesToNotifications < ActiveRecord::Migration[7.2]
disable_ddl_transaction!
def up
execute <<~SQL
DROP INDEX CONCURRENTLY IF EXISTS index_notifications_on_data_original_username
SQL
execute <<~SQL
CREATE INDEX CONCURRENTLY index_notifications_on_data_original_username
ON notifications ((data :: JSONB ->> 'original_username'))
WHERE (data :: JSONB ->> 'original_username') IS NOT NULL;
SQL
execute <<~SQL
DROP INDEX CONCURRENTLY IF EXISTS index_notifications_on_data_display_username
SQL
execute <<~SQL
CREATE INDEX CONCURRENTLY index_notifications_on_data_display_username
ON notifications ((data :: JSONB ->> 'display_username'))
WHERE (data :: JSONB ->> 'display_username') IS NOT NULL;
SQL
execute <<~SQL
DROP INDEX CONCURRENTLY IF EXISTS index_notifications_on_data_username
SQL
execute <<~SQL
CREATE INDEX CONCURRENTLY index_notifications_on_data_username
ON notifications ((data :: JSONB ->> 'username'))
WHERE (data :: JSONB ->> 'username') IS NOT NULL;
SQL
execute <<~SQL
DROP INDEX CONCURRENTLY IF EXISTS index_notifications_on_data_username2
SQL
execute <<~SQL
CREATE INDEX CONCURRENTLY index_notifications_on_data_username2
ON notifications ((data :: JSONB ->> 'username2'))
WHERE (data :: JSONB ->> 'username2') IS NOT NULL;
SQL
end
def down
execute <<~SQL
DROP INDEX index_notifications_on_data_original_username;
DROP INDEX index_notifications_on_data_display_username;
DROP INDEX index_notifications_on_data_username;
DROP INDEX index_notifications_on_data_username2;
SQL
end
end