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

Posted on 2004-11-19
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

    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

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


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API ( …
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now