Forum: Ruby on Rails How fast is Model.average compared to a find all and loop?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul B. (Guest)
on 2009-02-21 21:13
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?
Craig D. (Guest)
on 2009-02-21 22:43
(Received via mailing list)
You should try both and report back. :-)

Regards,
Craig
Paul B. (Guest)
on 2009-02-21 22:57
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. :-)
Frederick C. (Guest)
on 2009-02-22 01:15
(Received via mailing list)
On Feb 21, 8:57 pm, Pål Bergström <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
Paul B. (Guest)
on 2009-02-22 08:55
Frederick C. wrote:
> On Feb 21, 8:57�pm, P�l Bergstr�m <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. :)
This topic is locked and can not be replied to.