mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 10:21:06 +08:00
PERF: Improve query performance all inbox private messages. (#14304)
First reported in https://meta.discourse.org/t/-/202482/19 There are two optimizations being applied here: 1. Fetch a user's group ids in a seperate query instead of including it as a sub-query. When I tried a subquery, the query plan becomes very inefficient. 1. Join against the `topic_allowed_users` and `topic_allowed_groups` table instead of doing an IN against a subquery where we UNION the `topic_id`s from the two tables. From my profiling, this enables PG to do a backwards index scan on the `index_topics_on_timestamps_private` index. This commit fixes a bug where listing all messages was incorrectly excluding topics if a topic has been archived by a group even if the user did not belong to the group. This commit also fixes another bug where dismissing private messages selectively was subjected to the default limit of 30.
This commit is contained in:

committed by
GitHub

parent
d1d2298a4c
commit
ddb458343d
@ -952,7 +952,7 @@ class TopicsController < ApplicationController
|
||||
end
|
||||
|
||||
def private_message_reset_new
|
||||
topic_query = TopicQuery.new(current_user)
|
||||
topic_query = TopicQuery.new(current_user, limit: false)
|
||||
|
||||
if params[:topic_ids].present?
|
||||
unless Array === params[:topic_ids]
|
||||
@ -968,13 +968,12 @@ class TopicsController < ApplicationController
|
||||
params.require(:inbox)
|
||||
inbox = params[:inbox].to_s
|
||||
filter = private_message_filter(topic_query, inbox)
|
||||
topic_query.options[:limit] = false
|
||||
topic_scope = topic_query.filter_private_message_new(current_user, filter)
|
||||
end
|
||||
|
||||
topic_ids = TopicsBulkAction.new(
|
||||
current_user,
|
||||
topic_scope.pluck(:id),
|
||||
topic_scope.distinct(false).pluck(:id),
|
||||
type: "dismiss_topics"
|
||||
).perform!
|
||||
|
||||
@ -1246,7 +1245,11 @@ class TopicsController < ApplicationController
|
||||
if inbox = params[:private_message_inbox]
|
||||
filter = private_message_filter(topic_query, inbox)
|
||||
topic_query.options[:limit] = false
|
||||
topics = topic_query.filter_private_messages_unread(current_user, filter)
|
||||
|
||||
topic_query
|
||||
.filter_private_messages_unread(current_user, filter)
|
||||
.distinct(false)
|
||||
.pluck(:id)
|
||||
else
|
||||
topics = TopicQuery.unread_filter(topic_query.joined_topic_user, staff: guardian.is_staff?).listable_topics
|
||||
topics = TopicQuery.tracked_filter(topics, current_user.id) if params[:tracked].to_s == "true"
|
||||
@ -1265,9 +1268,9 @@ class TopicsController < ApplicationController
|
||||
if params[:tag_name].present?
|
||||
topics = topics.joins(:tags).where("tags.name": params[:tag_name])
|
||||
end
|
||||
end
|
||||
|
||||
topics.pluck(:id)
|
||||
topics.pluck(:id)
|
||||
end
|
||||
end
|
||||
|
||||
def private_message_filter(topic_query, inbox)
|
||||
|
Reference in New Issue
Block a user