From 01075c5e7ac0c0edf96cdcad462d509699f0d455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 1 Oct 2013 17:11:13 +0200 Subject: [PATCH] =?UTF-8?q?FIX=20database=20growth=20when=20running=20the?= =?UTF-8?q?=20=EF=BB=BFPeriodicalUpdates=20job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/score_calculator.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/score_calculator.rb b/lib/score_calculator.rb index 489f6907918..8efedd91827 100644 --- a/lib/score_calculator.rb +++ b/lib/score_calculator.rb @@ -22,7 +22,6 @@ class ScoreCalculator exec_sql(post_score_sql, @weightings) # Update the percent rankings of the posts - exec_sql("UPDATE posts SET percent_rank = x.percent_rank FROM (SELECT id, percent_rank() OVER (PARTITION BY topic_id ORDER BY SCORE DESC) as percent_rank @@ -73,12 +72,12 @@ class ScoreCalculator # Generate a SQL statement to update the scores of all posts def post_score_sql - "UPDATE posts SET score = ".tap do |sql| - components = [] - @weightings.keys.each do |k| - components << "COALESCE(#{k.to_s}, 0) * :#{k.to_s}" - end - sql << components.join(" + ") - end + components = [] + @weightings.keys.each { |k| components << "COALESCE(#{k.to_s}, 0) * :#{k.to_s}" } + components = components.join(" + ") + + "UPDATE posts SET score = x.score + FROM (SELECT id, #{components} as score FROM posts) AS x + WHERE x.id = posts.id AND (posts.score IS NULL OR x.score <> posts.score)" end end