Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen is Rails Eager loading by default for all association a good idea?
    primarykey
    data
    text
    <p>At which point is good to make all queries on a large project to eager load all of the objects associations?<br> Let me explain the background of this question.<br> Currently i am working on a project that has become quite complex in terms of models and associations. I started to check the server logs and saw that some views were taking some time to load because of the nested objects contained in the queries.<br> So i begin to refactor some of these queries to something like this: </p> <pre><code>@process = current_account.processes.query(params) </code></pre> <p>To this:</p> <pre><code>@process = current_account.processes.includes(:messages, :parts, :people).query(params) </code></pre> <p>The result was pretty good. In some views i was able to reduce the view render time and activerecord query time by 70%.<br> Man i was happy, so i decided to make more. But after this decision, i started to notice that not all places i refactored the code, the changes were good. In fact, there were queries that became slower.<br> To explain a little more about the problem i have a model like this: </p> <pre><code>class Process has_many :anotations has_many :publications has_many :movimentations has_many :reunions has_many :profiles ... </code></pre> <p>And each one of these nested models inside <code>process</code> <code>belongs_to :user</code> that created it like this:</p> <pre><code>class Anotation belongs_to :user class Reunion belongs_to :user class Profile belongs_to :user </code></pre> <p>And it go on.<br> In my <code>show view</code> of <code>process</code>, there is several tabs that display <strong>all</strong> of these nested objects and the name of the user that has added it to the current process.<br> With a query like this: </p> <pre><code>@process = current_account.processes.query(params) </code></pre> <p>It was performing kinda slow. So i tried something like this: </p> <pre><code>@process = current_account.process.includes(:anotations, :publications, :movimentations, :reunions, :profiles, :messages).query(params) </code></pre> <p>This has made me gain speed on the view rendering, but in activerecord, the the time to retrieve these objects have increased significantly. And it raised to the skies when i eager loaded the <code>user</code> object inside of all nested models of <code>process</code> like this: </p> <pre><code>@process = current_account.process.includes({:anotations =&gt; [:user]}, {:publications =&gt; [:user], etc...).query(params) </code></pre> <p>Well, refactoring the design of the app to behave differently with these associations is not going to happen, so i ask the question again.<br> At which point is good to make all queries on a large project to eager load all of the objects associations?<br> Any tips/best practices/experience on the subject would be gladly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload