Benito Serna Tips and tools for Ruby on Rails developers

Cheatsheet to help you decide which fix for the last-comment n+1 problem is the best for you

November 13, 2020

Do you have to fetch the latest “x” of each record? Not sure how to avoid the n+1 queries?

Hopefully you will find different ways to solve it. But what is the one that you should use?

You can search the internet, but opinions can be overwhelming and confusing, and most of the time not specific to your specific usecase.

I have already shared some fixes that to solve the “latest comment” n+1 queries problem and some benchmarks for this 5 fixes

Now I want to share with you a little cheatsheet to help you decide which fix to pick in different contexts, based mostly on my interpretation of the benchmarks and the complexity of each solution.

I hope it can help you =)

Related articles