[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2004-11-19
Medium Priority
Last Modified: 2012-05-05

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.
Question by:gilabean
LVL 48

Accepted Solution

hernst42 earned 500 total points
ID: 12632595
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.
LVL 12

Expert Comment

ID: 12633655
I agree with hernst42.  As a general rule, let the database do as much of the work as possible.


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question