Optimizing ActiveRecord :includes with :select

I have just read two posts (here and here), both of them discussing the sometimes formidable challenges faced when attempting to optimize ActiveRecord’s performance.

For me, ActiveRecord is easily Rails’ weakest link. Arguably, part of the problem is its ease of use. The object-oriented syntax means many developers find it far too easy to avoid opening up the hood and get their hands dirty with raw SQL, something which - in my opinion - every web developer should be able to do. But this is hardly AR’s fault. If that was the only problem, then ActiveRecord could stroll away unscathed while the workmen stood around blaming their tools. The real problems lie in other areas, and for me the most common I encounter is its inability to join two tables without then selecting every column in each table in the query.

For example:

User.find :all, :include => [ :posts, :comments ], :select => 'users.id, users.name, comments.content, posts.title'

will ignore the :include entirely, and go ahead and try to select from tables which weren’t included in the query. (This seems to have changed from earlier versions of AR, which ignored the :select options instead). Either way, this behaviour means it is difficult or impossible to sculpt elegant, effecient queries in many cases, and seriously erodes AR’s performance on complex queries.

I’ve seen a few patches published to solve this problem - and the highly useful select_with_include gem packaged up one of these patches, but sadly seems to be broken with the more recent builds of ActiveRecord (probably since this change, which I’m not sure fixes the same problem). Finding an alternative is high on my list of priorities, but surely something should have been baked into AR itself by now?

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Reddit
  • StumbleUpon
  • Technorati
  • Facebook

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*