Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Unanswered: SQL Counts or Application Counts?

    Hi,

    Is it usually preferable to do several SQL counts on a related set of data or to pull all the data into the PHP app with 1 SQL statement and do the counts in php?

    I need to peform 4 counts on a similar set of data. I would probably have to read about 100,000 rows into memory to count the data in PHP.

    I'm guessing it's better to just do several SQL statements --all counts--, but maybe someone has some experience on the subject. Perhaps, it's possible to do all the counts in one statement, but I haven't figured out a way.

    -r23

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    assuming you are using a server SQL (eg MySQL) rather than a file base SQL (eg JET or Filemaker) then it should be far better to do the summation using SQL on the server, rather than doing it using PHP and iterating through a recordset.
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you need only one query to get your different counts

    i'd show you, but unfortunately my beta copy of Microsoft® CrystalBall© is down at the moment, and i can't really see what your 4 counts are supposed to be

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Apr 2009
    Posts
    4
    Haha, thanks for the offer. After some research, I think I can get the counts I need using CASE and SUM. Previously, I have only used count and group by. If I hit a brick wall, I'll post some more detailed information.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •