ActiveRecord: biting the hand that feeds you

Rails Post.png

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:

hn

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):

chart

Some points:

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.