QueryCache and AR objects

I was hoping that the Rails’ QueryCache might cache the ActiveRecord
objects it creates and not just the SQL result set, but some
experimentation shows that this is not the case. For example:

ActiveRecord::Base.cache do
?> u = User.find(:first)
u.name=“z”
u = User.find(:first)
end

returns a clean copy of the object.

My question is, is there a way to cache the ActiveRecord objects as
well? I have read that their construction is time consuming. I found
the plugin ActiveRecordContext (dcadenas (Daniel Cadenas) · GitHub
active_record_context) but it was last updated way back in 2007, so
I wasn’t sure it is the best thing to use (or if it even works with
the current Rails).

Thanks,
–Paul

My question is, is there a way to cache the ActiveRecord objects as
well? I have read that their construction is time consuming.

I think that used to be the case, but I don’t think it is any more
(which
may be why the Query cache doesn’t cache them). Also, how many objects
are
you constructing? It should really only be a page worth, otherwise I’d
use
aggregate functions (if you’re just iterating anyway).

Cheers,

Andy

Thanks for the reply. I’m not sure about how many objects, but for
page (on which we are showing way, way too much information) we do
about 5000 SQL queries, a few of which might pull back 100 records or
so. I think a lot of those calls could be collapsed with eager
loading. But, lets say there are 6000 objects. On my system, I
calculated the object construction time (not including SQL time) as
being .000031s, which for 6000 objects adds up to 0.186s-- which is
not insignificant, though probably not our biggest performance issue.

I took a look at aggregate functions, and I don’t think they would be
of much use in this case, but thanks for mentioning them because I
wasn’t aware you could do that.

–Paul

Wow!!! 5000 SQL queries!

I don’t know what you’re doing, but I hope you’re not allowing lots of
people to view that page at the same time, MySQL meltdown :wink:

Cheers,

Andy


Andy J.
http://andyjeffries.co.uk/ #rubyonrails #mysql #jquery
Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS
Company number: 5452840