-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
dafc941
commit 6241845
Showing
3 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 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 |