<div dir="ltr">Is it just a case of putting the `where` before the `includes`?<div><br></div><div>i.e. is it potentially loading all of the included models first and THEN filtering them by account ID?</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 14 December 2015 at 12:46, Marco Iannone <span dir="ltr"><<a href="mailto:marco.iannone@gmail.com" target="_blank">marco.iannone@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​Hello,<br><br>​</div>Ruby newbie question alert<div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​: many apologies if it's too basic, or not the type of questions for this forum. <br>At least I can say we tried hard before going to <a href="http://stackoverflow.com/questions/34227034/manipulating-activerecord-objects-to-build-json" target="_blank">StackOverflow</a></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​, and now here.​</div><br><br>We're trying to <div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​ensure a webpage loads faster. The main issue </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​relates to a few activerecord-generated queries on that page.<br></div><br>If you look at the SQL they generate, the queries are as follows (all code a bit simplified):<br><br>select * from feed_details where account_id = 5<br>select * from feeds where id in (VERY_LONG_LIST_OF_IDS_FROM_FIRST_QUERY)<br>select * from feeds_metadata where feed_id in (VERY_LONG_LIST_FROM_FIRST_QUERY)<br>select * from documents where feed_id in (VERY_LONG_LIST_FROM_FIRST_QUERY)<br><br>All the indexes are in place, but we verified that they'd take less than half the time by changing <div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​each of them to:<br></div><br>select * from feeds, feed_details where id = feed_id and account_id = 5<br>select * from feeds_metadata fm, feed_details fd where fm.feed_id = fd.feed_id and account_id = 5<br>select * from documents where d, feed_details fd where d.feed_id = fd.feed_id and account_id = 5<br><br>So far, so simple. <br><br>Here is the <div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​Rails issue: these queries have been generated by this code in the FeedDetail model:<br></div><br>class FeedDetail < ActiveRecord::Base<br><br>  has_many :feeds<br>  has_many :feeds_metadata<br>  has_many :documents<br><br>  scope :feeds_data_for_account, ->(current_account_id) do<br>    FeedDetail<br>              .includes(:feeds, :feeds_metadata, :documents)<br>              .where(<br>                  account_id: current_account_id<br>              )<br>  end<br><br>This generates an active record object with the data from the above-mentioned queries, that is used in the controller to build the JSON that is sent to the webpage as in below (see feeds_data line):<br><br>  def index<br>    consumer          = ConsumerWithAccount.new(current_user.account)<br>    feed_builder      = FeedBuilder<br>    feeds_data        = FeedDetail.feeds_data_for_account(<a href="http://current_user.account.id" target="_blank">current_user.account.id</a>)<br><br>    render json: ResponseBuilder.new(consumer, feed_builder, feeds_data)<br>  end<br> <br>To use the faster queries, we were hoping to build the feeds_data activerecord object <div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​​by using the faster queries; this gets passed to the JSON builder​. That would </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​seem to require getting rid of the "involves", which (seemingly) uses the IN ().<br></div><br>If useful, we have the <div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​more effective activerecord for the 3 queries, they look like:<br></div>Feed.joins(:feed_details).where(feed_document_sets: {account_id: current_account_id})<br><br>But we couldn't figure out how to build that<div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​ ​</div>​activerecord object<div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​, or one that can be parsed in a similar way by the responsebuilder​</div>: therein lies the proverbial rub.<div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline"><br>Intense googling and referring to rails docs hasn't helped.​</div><br><br>The plan B <div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​would be to re-write the responsebuilder to take a different type of object, but that looks more complicated.<br></div><br>The person who wrote this is no longer with the company.<br><br>Plan C is to shake very hard the computer and hope for the best.<br><br>Any suggestions would be much appreciated<div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​, off/on list​</div>.<br><br><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">​Thanks,<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">Marco​</div><br></div>
<br>_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>
Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>
List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
<br></blockquote></div><br></div>