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.
Great episode 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
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.
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.
@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.