PERF: slow user pages in admin. add an index for trust level 3 calculations, and memoize query results

This commit is contained in:
Neil Lalonde 2015-01-19 15:30:16 -05:00
parent 7412ff4da7
commit 4c0129ccdd
2 changed files with 15 additions and 10 deletions

View File

@ -134,12 +134,12 @@ class TrustLevel3Requirements
end
def num_flagged_by_users
PostAction.with_deleted
.where(post_id: flagged_post_ids)
.where.not(user_id: @user.id)
.where.not(agreed_at: nil)
.pluck(:user_id)
.uniq.count
@_num_flagged_by_users ||= PostAction.with_deleted
.where(post_id: flagged_post_ids)
.where.not(user_id: @user.id)
.where.not(agreed_at: nil)
.pluck(:user_id)
.uniq.count
end
def max_flagged_by_users
@ -212,9 +212,9 @@ class TrustLevel3Requirements
end
def flagged_post_ids
@user.posts
.with_deleted
.where('created_at > ? AND (spam_count > 0 OR inappropriate_count > 0)', TIME_PERIOD.days.ago)
.pluck(:id)
@_flagged_post_ids ||= @user.posts
.with_deleted
.where('created_at > ? AND (spam_count > 0 OR inappropriate_count > 0)', TIME_PERIOD.days.ago)
.pluck(:id)
end
end

View File

@ -0,0 +1,5 @@
class AddPostsIndexIncludingDeleted < ActiveRecord::Migration
def change
add_index :posts, [:user_id, :created_at]
end
end