I'm using Rails and MySQL, and have an efficiency question based on row counting.
I have a Project
model that has_many :donations
.
I want to count the number of unique donors for a project.
Is having a field in the projects
table called num_donors
, and incrementing it when a new donor is created a good idea?
Or is something like @num_donors = Donor.count(:select => 'DISTINCT user_id')
going to be similar or the same in terms of efficiency thanks to database optimization? Will this require me to create indexes for user_id
and any other fields I want to count?
Does the same answer hold for summing the total amount donated?