From 78bcea5247844c380c255b5642d6d8bf1dc3887f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 8 Sep 2014 22:08:41 +0200 Subject: [PATCH] PERF: improve perf on consistency query in importers --- script/import_scripts/base.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/script/import_scripts/base.rb b/script/import_scripts/base.rb index e1a2697b654..5741e2bcd8f 100644 --- a/script/import_scripts/base.rb +++ b/script/import_scripts/base.rb @@ -468,8 +468,23 @@ class ImportScripts::Base end def update_last_posted_at - puts "", "updaing last posted at on users" - User.exec_sql("UPDATE users SET last_posted_at = (SELECT MAX(posts.created_at) FROM posts WHERE posts.user_id = users.id)") + puts "", "updating last posted at on users" + + sql = <<-SQL + WITH lpa AS ( + SELECT user_id, MAX(posts.created_at) AS last_posted_at + FROM posts + GROUP BY user_id + ) + UPDATE users + SET last_posted_at = lpa.last_posted_at + FROM users u1 + JOIN lpa ON lpa.user_id = u1.id + WHERE u1.id = users.id + AND users.last_posted_at <> lpa.last_posted_at + SQL + + User.exec_sql(sql) end def update_feature_topic_users