Basically I would have done it directly from MySQL, but I am working with someone else's code and I am bound to his mistakes.
Just out of curiosity, let's say I have 20 million rows in my database, and I sum them all in a PHP loop (doesn't matter names of columns).
How consuming is that?
For now I bypassed the issue using:
ini_set('memory_limit','2000M');
Let's try be more specific with an example:
Assume I have a table called deposits
that has the columns user_id
, deposit_amount
.
There are two ways to sum the amount of all the users.
1) Using MySQL:
SELECT SUM(`deposit_amount`) FROM `deposits`)
2) Using PHP:
$deposits = getAllDeposits();
foreach ($deposits as $deposit)
{
$sum += $deposit;
}