Re: Speed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux