Avoid n+1 queries on rails


Do you feel bad for asking again... “Why active record is ignoring my includes?”

Once you understand that n+1 queries can be a problem, it can be frustrating that is very easy to end with n+1 queries in rails, sometimes as an accident and sometimes just because you don’t know why.

When n+1 queries happen a lot to you, and you are not the one that find them, It can make you feel that you are not “good enough”.

And when you try to fix the n+1 queries, and it results in rails “ignoring your includes” it can make you even more insecure or unqualified to handle more complex tasks or even try to fix other performance problems.

You know that includes can sometimes fix the problem... You are just not sure why sometimes it doesn't.

The common advice to fix n+1 queries is “have you tried includes?”… But it don’t solve all the problems, and maybe you have other questions…

  • Should I use includes, preload or eager load?
  • Should I use size, count or length?
  • How can I fetch just the counts instead of the whole records?
  • What is that “counter cache” thing? Should I use it?
  • Why do we have so many n+1 queries? What tool can I use to detect them?

What if you were fluent with Active Record preloading and you knew how to fix n+1 queries taking performance and scalability in account?

You wont be taking time from your team asking for help for you n+1 queries problems.

You could feel secure and “qualified” to take complex tasks, with complex associations, because you will feel secure that you can fetch the needed data efficiently. You know how to preload direct associations but also, complex nested associations.

You can decide when is better to preload just the counts, instead of the whole association. Or you can decide if it is a good time to introduce a counter cache.

You can help your team leaders to decide which tool to use to detect n+1 queries proactively.

You know went to use includes, when to use preload and when to use eager_load, and can help your team when someone don’t know when is better to use one over the other.

You could be the one that help your team to fix the problems - next week

It is true, is very easy to produce n+1 queries working with ActiveRecord associations. Fixing them alone can be frustrating. And some “fixes” can hurt performance even more... but it doesn't have to.

Learn to fix n+1 queries like a pro, with my ebook "Avoid n+1 queries on rails"

Master n+1 queries identification, preloading, counts and counter caches, and eliminate n+1 queries from your code.

You will learn:

  1. What is an n+1 queries problem
  2. When a query will be executed
  3. How to preload direct associations
  4. How to preload nested associations
  5. When should you use the different preloading methods
  6. What is the difference between joins and preload
  7. What is the difference between count, size and length
  8. How to preload just the counts in a list
  9. How to solve n+1 queries and slow counts with counter caches
  10. What tools can help you detect n+1 queries

Avoid and fix n+1 queries with total confidence.

You'll learn, how you can use active record features and other tools to avoid n+1 queries and start shipping performant code.

Just pick the story/task/feature that you want, buy my ebook "Avoid n+1 queries basics", and you'll be shipping beautiful and efficient code in no time.

I want this!

An ebook with the knowledge and tools to help Ruby on Rails developers detect and fix n+1 queries

Book formats

Avoid n+1 queries on rails

I want this!