PERF: Keep track of when a users first unread is

This optimisation avoids large scans joining the topics table with the
topic_users table.

Previously when a user carried a lot of read state we would have to join
the entire read state with the topics table. This operation would slow down
home page and every topic page. The more read state you accumulated the
larger the impact.

The optimisation helps people who clean up unread, however if you carry
unread from years ago it will only have minimal impact.
This commit is contained in:
Sam Saffron
2019-04-05 12:44:36 +11:00
parent d299197392
commit 5f896ae8f7
7 changed files with 157 additions and 1 deletions
+4
View File
@@ -469,6 +469,10 @@ class TopicQuery
staff: @user&.staff?)
.order('CASE WHEN topics.user_id = tu.user_id THEN 1 ELSE 2 END')
if @user
result = result.where("topics.updated_at >= (SELECT first_unread_at FROM user_stats WHERE user_id = ?)", @user.id)
end
self.class.results_filter_callbacks.each do |filter_callback|
result = filter_callback.call(:unread, result, @user, options)
end