From 4387e05162f3d53d76732ab4964ae721cb05d444 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 16 Apr 2015 17:29:18 +1000 Subject: [PATCH] PERF: speed up query --- app/models/post_action.rb | 26 +++++++++++++++++--------- spec/models/post_action_spec.rb | 12 ++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app/models/post_action.rb b/app/models/post_action.rb index dbd70dd961e..5fc13301268 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -82,18 +82,26 @@ class PostAction < ActiveRecord::Base def self.lookup_for(user, topics, post_action_type_id) return if topics.blank? - + # in critical path 2x faster than AR + # + topic_ids = topics.map(&:id) map = {} - PostAction.where(user_id: user.id, post_action_type_id: post_action_type_id, deleted_at: nil) - .references(:post) - .includes(:post) - .where('posts.topic_id in (?)', topics.map(&:id)) - .order('posts.topic_id, posts.post_number') - .pluck('posts.topic_id, posts.post_number') - .each do |topic_id, post_number| - (map[topic_id] ||= []) << post_number + builder = SqlBuilder.new < [post.post_number]}) + end + end + describe ".add_moderator_post_if_needed" do it "should not add a moderator post when it's disabled" do