From f07b3e7df9cc07bcdf786802bc47f6f60d509673 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 17 May 2016 18:38:51 -0400 Subject: [PATCH] PERF: base importer updates user_stats with sql (mostly) --- script/import_scripts/base.rb | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/script/import_scripts/base.rb b/script/import_scripts/base.rb index 5c4cd5bc800..f4b378b4f3c 100644 --- a/script/import_scripts/base.rb +++ b/script/import_scripts/base.rb @@ -49,6 +49,7 @@ class ImportScripts::Base update_bumped_at update_last_posted_at update_last_seen_at + update_user_stats update_feature_topic_users update_category_featured_topics update_topic_count_replies @@ -568,6 +569,71 @@ class ImportScripts::Base User.exec_sql(sql) end + def update_user_stats + puts "", "Updating topic reply counts..." + User.find_each do |u| + u.create_user_stat if u.user_stat.nil? + us = u.user_stat + us.update_topic_reply_count + us.save + print "." + end + + puts "Updating first_post_created_at..." + + sql = <<-SQL + WITH sub AS ( + SELECT user_id, MIN(posts.created_at) AS first_post_created_at + FROM posts + GROUP BY user_id + ) + UPDATE user_stats + SET first_post_created_at = sub.first_post_created_at + FROM user_stats u1 + JOIN sub ON sub.user_id = u1.user_id + WHERE u1.user_id = user_stats.user_id + AND user_stats.first_post_created_at <> sub.first_post_created_at + SQL + + User.exec_sql(sql) + + puts "Updating user post_count..." + + sql = <<-SQL + WITH sub AS ( + SELECT user_id, COUNT(*) AS post_count + FROM posts + GROUP BY user_id + ) + UPDATE user_stats + SET post_count = sub.post_count + FROM user_stats u1 + JOIN sub ON sub.user_id = u1.user_id + WHERE u1.user_id = user_stats.user_id + AND user_stats.post_count <> sub.post_count + SQL + + User.exec_sql(sql) + + puts "Updating user topic_count..." + + sql = <<-SQL + WITH sub AS ( + SELECT user_id, COUNT(*) AS topic_count + FROM topics + GROUP BY user_id + ) + UPDATE user_stats + SET topic_count = sub.topic_count + FROM user_stats u1 + JOIN sub ON sub.user_id = u1.user_id + WHERE u1.user_id = user_stats.user_id + AND user_stats.topic_count <> sub.topic_count + SQL + + User.exec_sql(sql) + end + # scripts that are able to import last_seen_at from the source data should override this method def update_last_seen_at puts "", "updating last seen at on users"