PERF: base importer updates user_stats with sql (mostly)

This commit is contained in:
Neil Lalonde 2016-05-17 18:38:51 -04:00
parent d96ca0a985
commit f07b3e7df9

View File

@ -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"