|
Posted by Mark on 01/11/06 16:17
Peter Lauri wrote:
> Best group member,
>
>
>
> Assume that I save data about an object and it has 10.000 observations of
> the object stored in a MySQL database. I want calculate the average value
> of a column, is it faster done by using PHP on the result array or using
> the MySQL function to do that?
>
>
>
> /Peter
Hands down it will be the SQL database. To do an average, you must retrieve
all the data from disk anyway, that's your big hit, the summing all the
columns and dividing by the count is trivial compared to sending all that
data to a PHP box to do the average.
You would be better off all together figuring out how to not hit all those
records each time and keep a running average.
Something like:
$total_sum += $new_entry;
$count ++;
$avg = $total_sum/$count;
You will have to keep track of count and total_sum in some mutex locked type
of methodology, but any lock contention you get from it will almost
certainly out perform getting 10,000 records from a database.
Navigation:
[Reply to this message]
|