Fix n+1 queries on Rails

An ebook to help you go from "Please help me with my n+1 problem" to be one of those that can help the team.

By Benito Serna

Get a free sample Buy the ebook

Do you feel bad for asking it again?... "Please help me with my n+1 problem..."

Making things work isn't enough for you any more? Now you need to consider performance and scalability?

... But you are still having troubles fixing n+1 queries and trying to find why active record is ignoring your "includes"?

Are you are worried because you feel unqualified to tackle tasks with complex data models?

What if you could...

Ship code that fix user problems taking performance in consideration.

Be confident that you can handle tasks on complex data models and fix n+1 queries if they appear.

Be one of those who help the team... next week

Learn how to fix n+1 queries on rails

Start your road to fix n+1 queries like a pro, with my ebook Fix n+1 queries on rails:

Table of contents

Here is the full table of contents of the book.

No more… “Why active record is ignoring my includes?”

  1. Explain what is an n+1 queries problem
  2. Identify when ActiveRecord will execute a query
  3. Solve the latest comment example
  4. Detect n+1 queries by watching the logs
  5. Tools to detect n+1 queries

Get this first part of the ebook for free

Use preloading to avoid n+1 queries

  1. Joins does not preload
  2. Preload direct associations
  3. Preload nested associations
  4. Difference between preload, eager load or includes
  5. I prefer to use preload by default
  6. Preload scopes with scoped associations
  7. Simplify preloading with has many through associations
  8. Use a “preload object” for complex preloading
  9. Sometimes preloading can hurt the performance

Count without n+1 queries

  1. What is the difference between count, size and length
  2. Common mistakes with counts
  3. How to preload counts in a list

Save counts with counter caches

  1. Add counter cache to a new association
  2. Custom counter cache columns names
  3. Reset the counters for and existent association with just a few records
  4. Reset the counters with SQL, for associations with many records
  5. Using counter caches in your views

Caching custom counts and other computed values

  1. Fix n+1 queries by caching computed values
  2. Be aware that you can save incorrect values due to race conditions
  3. Tips to avoid race conditions saving computed values