A platform for community discussion. Free, open, simple.
PERF: Faster TL3 promotion replies needed calculation (#10416)
Rafael dos Santos Silva28669dfeb2dc
diff --git a/app/models/trust_level3_requirements.rb b/app/models/trust_level3_requirements.rbindex 3e35497d197..61970b2b9eb 100644--- a/app/models/trust_level3_requirements.rb+++ b/app/models/trust_level3_requirements.rb@@ -143,7 +143,7 @@ class TrustLevel3Requirementsenddef num_topics_replied_to- @user.user_stat.calc_topic_reply_count!(min_topics_replied_to + 1, time_period.days.ago)+ @user.user_stat.calc_topic_reply_count!(time_period.days.ago)enddef min_topics_replied_todiff --git a/app/models/user_stat.rb b/app/models/user_stat.rbindex 1eb168bc9be..881b14ae69a 100644--- a/app/models/user_stat.rb+++ b/app/models/user_stat.rb@@ -154,11 +154,9 @@ class UserStat < ActiveRecord::Baseend# topic_reply_count is a count of posts in other users' topics- def calc_topic_reply_count!(max, start_time = nil)+ def calc_topic_reply_count!(start_time = nil)sql = <<~SQL- SELECT COUNT(*) count- FROM (- SELECT DISTINCT posts.topic_id+ SELECT COUNT(DISTINCT posts.topic_id) AS countFROM postsINNER JOIN topics ON topics.id = posts.topic_idWHERE posts.user_id = ?@@ -166,13 +164,11 @@ class UserStat < ActiveRecord::BaseAND posts.deleted_at IS NULL AND topics.deleted_at IS NULLAND topics.archetype <> 'private_message'#{start_time.nil? ? '' : 'AND posts.created_at > ?'}- LIMIT ?- ) as user_topic_repliesSQLif start_time.nil?- DB.query_single(sql, self.user_id, max).first+ DB.query_single(sql, self.user_id).firstelse- DB.query_single(sql, self.user_id, start_time, max).first+ DB.query_single(sql, self.user_id, start_time).firstendenddiff --git a/lib/promotion.rb b/lib/promotion.rbindex c39d13f44f7..d768d226ed2 100644--- a/lib/promotion.rb+++ b/lib/promotion.rb@@ -91,7 +91,7 @@ class Promotionreturn false if stat.days_visited < SiteSetting.tl2_requires_days_visitedreturn false if stat.likes_received < SiteSetting.tl2_requires_likes_receivedreturn false if stat.likes_given < SiteSetting.tl2_requires_likes_given- return false if stat.calc_topic_reply_count!(SiteSetting.tl2_requires_topic_reply_count) < SiteSetting.tl2_requires_topic_reply_count+ return false if stat.calc_topic_reply_count! < SiteSetting.tl2_requires_topic_reply_counttrueend