<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Do you need all fields from all tables? Or just `id`s from `feed_details` to get further data? Maybe .select would help.<div class=""><br class=""><div class="">Also if all that data is necessary and it is huge maybe it is time to consider paginating it with .limit of .offset?<br class=""><div class=""><br class=""></div><div class="">Julius</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 14 Dec 2015, at 13:46, Marco Iannone <<a href="mailto:marco.iannone@gmail.com" class="">marco.iannone@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​Hello,<br class=""><br class="">​</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 class="">At least I can say we tried hard before going to <a href="http://stackoverflow.com/questions/34227034/manipulating-activerecord-objects-to-build-json" class="">StackOverflow</a></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">​, and now here.​</div><br class=""><br class="">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 class=""></div><br class="">If you look at the SQL they generate, the queries are as follows (all code a bit simplified):<br class=""><br class="">select * from feed_details where account_id = 5<br class="">select * from feeds where id in (VERY_LONG_LIST_OF_IDS_FROM_FIRST_QUERY)<br class="">select * from feeds_metadata where feed_id in (VERY_LONG_LIST_FROM_FIRST_QUERY)<br class="">select * from documents where feed_id in (VERY_LONG_LIST_FROM_FIRST_QUERY)<br class=""><br class="">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 class=""></div><br class="">select * from feeds, feed_details where id = feed_id and account_id = 5<br class="">select * from feeds_metadata fm, feed_details fd where fm.feed_id = fd.feed_id and account_id = 5<br class="">select * from documents where d, feed_details fd where d.feed_id = fd.feed_id and account_id = 5<br class=""><br class="">So far, so simple. <br class=""><br class="">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 class=""></div><br class="">class FeedDetail < ActiveRecord::Base<br class=""><br class="">  has_many :feeds<br class="">  has_many :feeds_metadata<br class="">  has_many :documents<br class=""><br class="">  scope :feeds_data_for_account, ->(current_account_id) do<br class="">    FeedDetail<br class="">              .includes(:feeds, :feeds_metadata, :documents)<br class="">              .where(<br class="">                  account_id: current_account_id<br class="">              )<br class="">  end<br class=""><br class="">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 class=""><br class="">  def index<br class="">    consumer          = ConsumerWithAccount.new(current_user.account)<br class="">    feed_builder      = FeedBuilder<br class="">    feeds_data        = FeedDetail.feeds_data_for_account(<a href="http://current_user.account.id/" class="">current_user.account.id</a>)<br class=""><br class="">    render json: ResponseBuilder.new(consumer, feed_builder, feeds_data)<br class="">  end<br class=""> <br class="">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 class=""></div><br class="">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 class=""></div>Feed.joins(:feed_details).where(feed_document_sets: {account_id: current_account_id})<br class=""><br class="">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 class="">Intense googling and referring to rails docs hasn't helped.​</div><br class=""><br class="">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 class=""></div><br class="">The person who wrote this is no longer with the company.<br class=""><br class="">Plan C is to shake very hard the computer and hope for the best.<br class=""><br class="">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 class=""><br class=""><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">​Thanks,<br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">Marco​</div><br class=""></div>
_______________________________________________<br class="">Chat mailing list<br class=""><a href="mailto:Chat@lists.lrug.org" class="">Chat@lists.lrug.org</a><br class="">Archives: http://lists.lrug.org/pipermail/chat-lrug.org<br class="">Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org<br class="">List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org<br class=""></div></blockquote></div><br class=""></div></div></div></body></html>