[LRUG] N+1

Andrew Stewart boss at airbladesoftware.com
Tue Aug 4 04:46:15 PDT 2020

Hi Simon,

Rewriting the eager load to match the access pattern did the trick – thank you!

It's obvious in hindsight... :)

Thanks again,


> On 4 Aug 2020, at 12:28 pm, Simon Coffey <simon at urbanautomaton.com> wrote:
> Hey Andy,
> I think the problem might be that eager loading is applied to specific associations, and the ones you're applying eager loading to aren't the ones that you're using to perform your reads.
> Specifically, with `some_beta_scope.includes(gammas: :deltas)` you're eager loading the `Beta#gammas` and `Gamma#deltas` associations, but in your view you're using `Beta#deltas` and `Delta#gamma` to actually load the data. (possibly-unhelpful illustration <http://screenshots.urbanautomaton.com/20200804-p79pj.png>)
> You'd either need to rewrite the eager load to match your access pattern (`some_beta_scope.includes(deltas: :gamma)`, I think), or rewrite the loop to match the eager loading, e.g.
> - @betas.each do |beta|
>   = beta.name <http://beta.name/>
>   - beta.gammas.each do |gamma|
>     - gamma.deltas.each do |delta|
>       = delta.name <http://delta.name/>
>       = gamma.name <http://delta.gamma.name/>
> Does that work?
> Cheers,
> Simon

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20200804/b8e37beb/attachment.html>

More information about the Chat mailing list