Advanced Query Optimization

Slow and redundant database queries got you down? Well have no fear! On this week's episode, Chris and Joe dive deep into the strategies you can use to optimize your queries and get your app back to ludicrous speed ASAP.

This is a companion discussion topic for the original entry at

Great episode :+1: It would be helpful to know what the practical performance improvement is for people hitting this page. e.g. “They get a response 10% faster”.

Hi @equivalentideas, thanks for the question. Pretty much the cardinal rule on performance work is to always benchmark, and I failed to better highlight that in the video. Thanks for keeping me honest :smile:

Joe and I reviewed when he actually implemented the changes (the version used in the WI episode was slightly more complex) but did not capture the numbers. Looking back, it looks like we went from an average response time (from req received to response sent) went from 800ms down to 400ms. Fixed latency and rendering time added to that means an end user wouldn’t feel a full 2x improvement (likely more like 50% in end user experience) but pretty nice none the less.


@christoomey I finally understand eager loading. Thanks for the video again. Have you guys done any videos on bench marking? In my application, my queries are not optimized for sure, how would I go about getting a solid benchmark? When I look at the logs, the ActiveRecord load time varies anywhere from 500 ms to 600 ms, seemingly randomly. But I feel like that’s a pretty weak benchmark?


Hey @thedanotto, so glad we could help! We don’t have much on the topic of profiling, although we definitely should (and will somewhere down the line).

The best starting point I’ve found is rack-mini-profiler. I include it on all apps in dev mode at this point to gain some insight. Similarly, bullet, as mentioned in the episode, is not perfect, but can pick out some n+1s.

1 Like

Super useful episode! I knew about the includes style eager loading but hadn’t thought of refactoring by extracting a class or using preload to accomplish even faster queries.

1 Like

@ToniRib I found Bryan Helmkamp’s advice in his famous blog post about refactoring code out of ActiveRecord models (7 Patterns to Refactor Fat ActiveRecord Models | Code Climate) about Query Objects to be quite helpful. On a long-running project I worked on we had several very complicated queries (some using some hand-rolled SQL, others just using a lot of complicated AR logic) that we bundled up into query classes that performed the search for us.

Quiet assets, which is recommended in the beginning of the post, is now deprecated.

Ok, this is really really good. Seriously, very impressive.