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


#1

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?


#2

You should try both and report back. :slight_smile:

Regards,
Craig


#3

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:


#4

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


#5

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