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
This commit is contained in:
Sam 2015-06-18 09:58:32 +10:00
parent 9cdfef2b27
commit 1343d40558
4 changed files with 20 additions and 1 deletions

View File

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

View File

@ -0,0 +1,5 @@
class AddIndexTargetPostIdOnUserActions < ActiveRecord::Migration
def change
add_index :user_actions, [:target_post_id]
end
end

View File

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

View File

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