mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +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:
parent
0398271f87
commit
9b75d95fc6
11 changed files with 326 additions and 6 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue