mirror of
https://github.com/discourse/discourse.git
synced 2025-03-01 23:06:42 +08:00
PERF: stop counting participants on very large topics
This query gets very expensive and can be bypassed on large topics
This commit is contained in:
parent
35e0ccfc84
commit
cbdab71179
@ -296,10 +296,18 @@ class TopicView
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# if a topic has more that N posts no longer attempt to
|
||||||
|
# get accurate participant count, instead grab cached count
|
||||||
|
# from topic
|
||||||
|
MAX_POSTS_COUNT_PARTICIPANTS = 500
|
||||||
|
|
||||||
def participant_count
|
def participant_count
|
||||||
@participant_count ||=
|
@participant_count ||=
|
||||||
begin
|
begin
|
||||||
if participants.size == MAX_PARTICIPANTS
|
if participants.size == MAX_PARTICIPANTS
|
||||||
|
if unfiltered_post_ids.length > MAX_POSTS_COUNT_PARTICIPANTS
|
||||||
|
@topic.participant_count
|
||||||
|
else
|
||||||
sql = <<~SQL
|
sql = <<~SQL
|
||||||
SELECT COUNT(DISTINCT user_id)
|
SELECT COUNT(DISTINCT user_id)
|
||||||
FROM posts
|
FROM posts
|
||||||
@ -307,6 +315,7 @@ class TopicView
|
|||||||
AND user_id IS NOT NULL
|
AND user_id IS NOT NULL
|
||||||
SQL
|
SQL
|
||||||
DB.query_single(sql, post_ids: unfiltered_post_ids).first.to_i
|
DB.query_single(sql, post_ids: unfiltered_post_ids).first.to_i
|
||||||
|
end
|
||||||
else
|
else
|
||||||
participants.size
|
participants.size
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user