# How fast is Model.average compared to a find all and loop?

**URL:** <https://www.ruby-forum.com/t/how-fast-is-model-average-compared-to-a-find-all-and-loop/160984>\
**Category:** Rails\
**Created:** [February 21, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/how-fast-is-model-average-compared-to-a-find-all-and-loop/160984 "2009-02-21T20:13:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [February 21, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/how-fast-is-model-average-compared-to-a-find-all-and-loop/160984/1 "2009-02-21T20:13:11Z")

</div>

I have a set of results that I want to find some average values, divided  
into group\_1, \_2, and \_3. I have two options.

1. Find all and then loop through all and in each loop calculate how  
many there are and the summery for each and get the average for each.

2. Use the calculation method Model.average() 3 times.

Which one is fastest? And what happens when there are more groups?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [February 21, 2009, 9:43pm UTC](https://www.ruby-forum.com/t/how-fast-is-model-average-compared-to-a-find-all-and-loop/160984/2 "2009-02-21T21:43:10Z")

</div>

You should try both and report back. 🙂

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [February 21, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/how-fast-is-model-average-compared-to-a-find-all-and-loop/160984/3 "2009-02-21T21:57:22Z")

</div>

Craig D. wrote:

> You should try both and report back. 🙂
> 
> Regards,  
> Craig

I don’t have so many post in my db and I thought someone might have the  
answer. I’ll will add some and try it. 🙂

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [February 22, 2009, 7:55am UTC](https://www.ruby-forum.com/t/how-fast-is-model-average-compared-to-a-find-all-and-loop/160984/4 "2009-02-22T07:55:25Z")

</div>

Frederick C. wrote:

> On Feb 21, 8:57ï¿½pm, Pï¿½l Bergstrï¿½m [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:

> I would expect Model.average to be much faster - aggregating lots of  
> data is something databases are good at, and something for which  
> instantiating 10s of thousands of active record objects is  
> unnecessary.
> 
> Fred

Thanks. That was what I suspected. 🙂

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [February 22, 2009, 12:15am UTC](https://www.ruby-forum.com/t/how-fast-is-model-average-compared-to-a-find-all-and-loop/160984/5 "2009-02-22T00:15:35Z")

</div>

On Feb 21, 8:57 pm, Pål Bergström [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Craig D. wrote:
> 
> > You should try both and report back. 🙂
> 
> > Regards,  
> > Craig
> 
> I don’t have so many post in my db and I thought someone might have the  
> answer. I’ll will add some and try it. 🙂

I would expect Model.average to be much faster - aggregating lots of  
data is something databases are good at, and something for which  
instantiating 10s of thousands of active record objects is  
unnecessary.

Fred
