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

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?

You should try both and report back. :slight_smile:

Regards,
Craig

Craig D. wrote:

You should try both and report back. :slight_smile:

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. :slight_smile:

Frederick C. wrote:

On Feb 21, 8:57�pm, P�l Bergstr�m [email protected]
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. :slight_smile:

On Feb 21, 8:57 pm, Pål Bergström [email protected]
wrote:

Craig D. wrote:

You should try both and report back. :slight_smile:

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. :slight_smile:

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