From 1343d40558cd1aeeb33599a41dd2a46743849443 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 18 Jun 2015 09:58:32 +1000 Subject: [PATCH] PERF: deleting a post in huge topics was timing out - add missing index to user actions for fast retrieval by post - add missing indexes to users for fast retrieval of staff - only refresh topic_users liked/bookmarked cache for affected users --- app/models/topic_user.rb | 8 ++++++++ ...0617233018_add_index_target_post_id_on_user_actions.rb | 5 +++++ db/migrate/20150617234511_add_staff_index_to_users.rb | 6 ++++++ lib/post_destroyer.rb | 2 +- 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20150617233018_add_index_target_post_id_on_user_actions.rb create mode 100644 db/migrate/20150617234511_add_staff_index_to_users.rb diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index 2611039f311..30fdf0be304 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -232,6 +232,7 @@ class TopicUser < ActiveRecord::Base def self.update_post_action_cache(opts={}) user_id = opts[:user_id] + post_id = opts[:post_id] topic_id = opts[:topic_id] action_type = opts[:post_action_type] @@ -277,6 +278,13 @@ SQL builder.where("tu2.topic_id = :topic_id", topic_id: topic_id) end + if post_id + builder.where("tu2.topic_id IN (SELECT topic_id FROM posts WHERE id = :post_id)", post_id: post_id) + builder.where("tu2.user_id IN (SELECT user_id FROM post_actions + WHERE post_id = :post_id AND + post_action_type_id = :action_type_id)") + end + builder.exec(action_type_id: PostActionType.types[action_type]) end diff --git a/db/migrate/20150617233018_add_index_target_post_id_on_user_actions.rb b/db/migrate/20150617233018_add_index_target_post_id_on_user_actions.rb new file mode 100644 index 00000000000..973bda76f04 --- /dev/null +++ b/db/migrate/20150617233018_add_index_target_post_id_on_user_actions.rb @@ -0,0 +1,5 @@ +class AddIndexTargetPostIdOnUserActions < ActiveRecord::Migration + def change + add_index :user_actions, [:target_post_id] + end +end diff --git a/db/migrate/20150617234511_add_staff_index_to_users.rb b/db/migrate/20150617234511_add_staff_index_to_users.rb new file mode 100644 index 00000000000..f060b9b1693 --- /dev/null +++ b/db/migrate/20150617234511_add_staff_index_to_users.rb @@ -0,0 +1,6 @@ +class AddStaffIndexToUsers < ActiveRecord::Migration + def change + add_index :users, [:id], name: 'idx_users_admin', where: 'admin' + add_index :users, [:id], name: 'idx_users_moderator', where: 'moderator' + end +end diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index fb6bd9079c9..818f3766356 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -90,7 +90,7 @@ class PostDestroyer end update_associated_category_latest_topic update_user_counts - TopicUser.update_post_action_cache(topic_id: @post.topic_id) + TopicUser.update_post_action_cache(post_id: @post.id) end feature_users_in_the_topic if @post.topic