mirror of
https://github.com/discourse/discourse.git
synced 2025-06-24 04:03:34 +08: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
@ -960,9 +960,19 @@ class TopicQuery
|
||||
def unread_messages(params)
|
||||
query = TopicQuery.unread_filter(
|
||||
messages_for_groups_or_user(params[:my_group_ids]),
|
||||
@user&.id,
|
||||
staff: @user&.staff?)
|
||||
.limit(params[:count])
|
||||
@user.id,
|
||||
staff: @user.staff?
|
||||
)
|
||||
|
||||
first_unread_pm_at =
|
||||
if params[:my_group_ids].present?
|
||||
GroupUser.where(user_id: @user.id, group_id: params[:my_group_ids]).minimum(:first_unread_pm_at)
|
||||
else
|
||||
UserStat.where(user_id: @user.id).pluck(:first_unread_pm_at).first
|
||||
end
|
||||
|
||||
query = query.where("topics.updated_at >= ?", first_unread_pm_at) if first_unread_pm_at
|
||||
query = query.limit(params[:count]) if params[:count]
|
||||
query
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user