mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
committed by
Alan Guo Xiang Tan
parent
0398271f87
commit
9b75d95fc6
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user