mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 19:01:52 +08:00
624184560e
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.
24 lines
617 B
Ruby
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
|