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
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:
- Forget about… “Why active record is ignoring my includes?”
- Learn different ways to solve n+1 queries beyond includes
- Learn different preloading techniques to avoid n+1 queries
- Learn how to count records without n+1 queries
- Learn to use counter caches
- Learn how to cash custom counts and other computed values with concurrency in mind
Table of contents
Here is the full table of contents of the book.
No more… “Why active record is ignoring my includes?”
- Explain what is an n+1 queries problem
- Identify when ActiveRecord will execute a query
- Solve the latest comment example
- Detect n+1 queries by watching the logs
- Tools to detect n+1 queries
- 4 ways to fix n+1 queries
Use preloading to avoid n+1 queries
- Joins does not preload
- Preload direct associations
- Preload nested associations
- Difference between preload, eager load or includes
- I prefer to use preload by default
- Preload scopes with scoped associations
- Simplify preloading with has many through associations
- Use a “preload object” for complex preloading
- Sometimes preloading can hurt the performance
Count without n+1 queries
- What is the difference between count, size and length
- Common mistakes with counts
- How to preload counts in a list
Save counts with counter caches
- Add counter cache to a new association
- Custom counter cache columns names
- Reset the counters for and existent association with just a few records
- Reset the counters with SQL, for associations with many records
- Using counter caches in your views
Caching custom counts and other computed values
- Fix n+1 queries by caching computed values
- Be aware that you can save incorrect values due to race conditions
- Tips to avoid race conditions saving computed values