MySQL SUM() vs PHP LOOP speed ....


I have a total of 500,000 records inside a table and I need to sum some fields up from this records and group it by customer.

I am using MySQL as my db and PHP as my web language. I am wondering whether a MySQL SUM() AND GROUP BY statement is faster or a PHP LOOP of all the 500K of records to sum it up is faster?

Let's assume that my table have all the indexes that the SQL statment needs. It would be great to share this with everyone !

Thanks in advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

the mysql-db is faster than php (I assume 10 to 100 time or even more with that amount of records), beacuse mysql can read the data directly and does not need to transfer them, does not need to put them into php-structures and does not need to convert the strings to ints and sum those values.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I agree with hernst42.  As a general rule, let the database do as much of the work as possible.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.