I have a strong feeling that all mathematical operations unnecessary to the query itself ought to be preformed outside of the query. For example:
$result = mysql_query(SELECT a, a*b/c as score FROM table)
while ($row = mysql_fetch_assoc($result))
{
echo $row['a'].' score: '.$row['score'].<br>;
}
vs:
$result = mysql_query(SELECT a, b, c FROM table)
while ($row = mysql_fetch_assoc($result))
{
echo $row['a'].' score: '.$row['a']*$row['b']/$row['c'].<br>;
}
the second option would usually be better, especially with complex table joins & such. This is my suspicion, I only lack confirmation . . .
My feeling would be that doing the maths in the database would be slightly more efficient in the long run, given your query setup. With the
select a,b,cversion, PHP has to create 3 elements and populate them for each row fetched.With the in-database version, only 2 elements are created, so you’ve cut creation time by 33%. Either way, the calculation has to be done, so there’s not much in the way of savings there.
Now, if you actually needed the
bandcvalues to be exposed to your code, then there’d be no point in doing the calculation in the database, you’d be adding more fields to the result set with their attendant creation/processing/populating overhead.Regardless, though, you should benchmark both version. What works in one situation may be worse than useless in another, and only some testing will show which is better.