# QueryCache and AR objects

**URL:** <https://www.ruby-forum.com/t/querycache-and-ar-objects/184014>\
**Category:** Rails\
**Created:** [February 23, 2010, 9:31pm UTC](https://www.ruby-forum.com/t/querycache-and-ar-objects/184014 "2010-02-23T21:31:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [February 23, 2010, 9:31pm UTC](https://www.ruby-forum.com/t/querycache-and-ar-objects/184014/1 "2010-02-23T21:31:13Z")

</div>

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](http://github.com/dcadenas/)  
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

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [February 24, 2010, 12:07pm UTC](https://www.ruby-forum.com/t/querycache-and-ar-objects/184014/2 "2010-02-24T12:07:02Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [February 24, 2010, 4:42pm UTC](https://www.ruby-forum.com/t/querycache-and-ar-objects/184014/3 "2010-02-24T16:42:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [February 25, 2010, 12:05pm UTC](https://www.ruby-forum.com/t/querycache-and-ar-objects/184014/4 "2010-02-25T12:05:01Z")

</div>

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 😉

Cheers,

Andy

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