PERF: Keep track of first unread PM and first unread group PM for user.

This optimization helps to filter away topics so that the joins on
related tables when querying for unread messages is not expensive.
This commit is contained in:
Guo Xiang Tan
2020-09-03 14:02:15 +08:00
committed by Alan Guo Xiang Tan
parent 0398271f87
commit 9b75d95fc6
11 changed files with 326 additions and 6 deletions

View File

@@ -0,0 +1,16 @@
# frozen_string_literal: true
class AddFirstUnreadPmAToGroupUser < ActiveRecord::Migration[6.0]
def up
add_column :group_users, :first_unread_pm_at, :datetime, null: false, default: -> { 'CURRENT_TIMESTAMP' }
execute <<~SQL
UPDATE group_users gu
SET first_unread_pm_at = gu.created_at
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
class AddFirstUnreadPmAtToUserStats < ActiveRecord::Migration[6.0]
def up
add_column :user_stats, :first_unread_pm_at, :datetime, null: false, default: -> { 'CURRENT_TIMESTAMP' }
execute <<~SQL
UPDATE user_stats us
SET first_unread_pm_at = u.created_at
FROM users u
WHERE u.id = us.user_id
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
class AddIndexTopicsOnTimestampsPrivate < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def up
execute <<~SQL
CREATE INDEX CONCURRENTLY IF NOT EXISTS
index_topics_on_timestamps_private
ON topics (bumped_at, created_at, updated_at)
WHERE deleted_at IS NULL AND archetype = 'private_message'
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end