2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-06 10:50:21 +08:00

PERF: avoid expensive order by random for suggested topics

This commit is contained in:
Sam 2015-02-25 17:19:12 +11:00
parent 7b6e85cb6c
commit b760d22460
3 changed files with 112 additions and 1 deletions

View file

@ -414,7 +414,16 @@ class TopicQuery
result = result.order("CASE WHEN topics.category_id = #{topic.category_id.to_i} THEN 0 ELSE 1 END")
end
result.order("RANDOM()")
# Best effort, it over selects, however if you have a high number
# of muted categories there is tiny chance we will not select enough
# in particular this can happen if current category is empty and tons
# of muted, big edge case
#
# we over select in case cache is stale
max = (count*1.3).to_i
ids = RandomTopicSelector.next(max) + RandomTopicSelector.next(max, topic.category)
result.where(id: ids)
end
def suggested_ordering(result, options)