ActiveRecord: biting the hand that feeds you
Updated version of an article first published on October 9th, 2017.
Richard Schneeman (better known as Schneems) recently wrote about how he reduced his database server load by 80%...with one simple trick. In the Hacker News discussion that followed, much of the debate was on the merits of using an ORM like ActiveRecord...or not:
In every case I can think of, writing raw SQL is likely to execute queries faster than using ActiveRecord. So, why don't I just write raw
conn.exec('SELECT * FROM users') everywhere?
Performance is an important thing, but it's not THE most important thing. In Richard's case, he had a 20 minute blip where response times were significantly higher for his app (a side project, btw):
- The impacted app, CodeTriage, is a side project for Richard. We'd all be happy to have a side project like this running in production. A 20 minute spike in response times isn't a deal-beaker. He has traffic on a side project - this is a great problem!
- Productivity and momentum trump execution in side projects almost every time. It would take a special developer to get a side project up and running faster without using an ORM.
- Rather than over-optimizing, Richard has a toolset he can use to optimize when there is a problem. I'd rather launch with some performance quirks that get exposed than over-optimize and waste time on an unused feature.
In our jobs as developers - and in life - our biggest challenge is choosing how to best spend our time. I'm not against raw SQL, but I rely on tools to tell me when I need to drop down to that level.
PS - our database monitoring is in early access. This would have gotten to the problem query quicker, I believe :).
Ready to optimize your site?
Adding servers can be a band-aid for slow code. Scout APM helps you find and fix your inefficient and costly code. We automatically identify N+1 SQL calls, memory bloat, and other code-related issues so you can spend less time debugging and more time programming.
Ready to optimize your site? Sign up for a free trial.