From 1fdeec564ba903571763b8372b97636e76c6bfb1 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Tue, 30 Apr 2019 00:34:46 -0300 Subject: [PATCH] PERF: Move `where` clause up to speed up CalculateAvgTime daily job (#7462) Cuts down affected posts earlier in the query, so the generated plan deals with less rows, and runs faster. https://meta.discourse.org/t/post-calculate-avg-time-taking-up-a-long-time/49750/13?u=falco --- app/models/post.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index 9f06916b109..5ab73200d75 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -688,6 +688,7 @@ class Post < ActiveRecord::Base ON p2.post_number = post_timings.post_number AND p2.topic_id = post_timings.topic_id AND p2.user_id <> post_timings.user_id + /*where2*/ GROUP BY post_timings.topic_id, post_timings.post_number) AS x /*where*/") @@ -696,7 +697,7 @@ class Post < ActiveRecord::Base AND (posts.avg_time <> (x.gmean / 1000)::int OR posts.avg_time IS NULL)") if min_topic_age - builder.where("posts.topic_id IN (SELECT id FROM topics where bumped_at > :bumped_at)", + builder.where2("p2.topic_id IN (SELECT id FROM topics where bumped_at > :bumped_at)", bumped_at: min_topic_age) end