discourse/db/migrate/20190514055014_add_read_notification_index.rb
Sam Saffron 624184560e PERF: improve performance of publish_notifications_state
User.publish_notifications_state is called every time a notification is
created, this can become a very critical code path.

On some heavy notification related sites this can be a major CPU user on PG

This index makes it much cheaper to publish notification state, cause a
simple index lookup does the trick.
2019-05-14 16:02:55 +10:00

24 lines
617 B
Ruby

# frozen_string_literal: true
class AddReadNotificationIndex < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
def up
# doing this by hand cause I am ordering id DESC
execute <<~SQL
CREATE UNIQUE INDEX CONCURRENTLY index_notifications_on_read_or_n_type
ON notifications(user_id, id DESC, read, topic_id)
WHERE read or notification_type <> 6
SQL
# we need to do this to ensure this index hits
# on some sites this was missing prior
execute <<~SQL
VACUUM ANALYZE notifications
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end