Topic: Performance of ActiveRecord

I'm fetching data using a quite simple query with many outer joins.

The query itself executes in about 16ms on the db, which is faster than I need.

However, the resultset contains about 1200 rows (due to the many outer joins) with about 100 columns in each row and instantiating the according objects by ActiveRecord takes forever (about 5 seconds). Furthermore my impression is that ActiveRecord instantiates the same objects over and over again (without using an efficient first level cache, indexed by the object's class and id), so that fetching the data without the outer joins and linking the puzzle manually is actually faster. But of course this is a non-solution.

Is there any tutorial on optimizing ActiveRecord's performance available ?

Re: Performance of ActiveRecord

Out of curiosity, why are you fetching so many records? Surely you aren't displaying all of those records on one page?

One option is to bypass ActiveRecord entirely and interact directly with the database, but that can lead to some very messy code depending on what you're doing.

Railscasts - Free Ruby on Rails Screencasts

Re: Performance of ActiveRecord

In fact I need all this data.

It's a rather complicated report page (a user's complete skill profile with many associated objects). The high number of records is a result of the outer joins. The root record is singular...

With Hibernate this would work like a charm (due to it's first level cache and the fact that there's only one instance of a record witha given id in this cache). It think that this is just a limitation of the DataObject pattern that is implemented by ActiveRecord. Maybe I'll split the queries.

Re: Performance of ActiveRecord

You could also manually manage a cache with Rails using callbacks. I can go into detail if you need me to.

Railscasts - Free Ruby on Rails Screencasts

Re: Performance of ActiveRecord

Thx for your reply, Ryan. A manual cache sounds interesting to me.

But I would need to plug this into the ActiveRecord loading process. Hmm, interesting anyway.