11 Replies - 12856 Views - Last Post: 08 November 2009 - 01:13 PM

#1 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 106
  • Joined: 27-February 09

MySQL COUNT vs PHP mysql_num_rows

Posted 25 October 2009 - 07:45 PM

I'm still learning PHP, obviously, and would consider myself around the intermediate level. I don't want to anger anyone by making them think that I think that I'm an expert in PHP programming. I just had a question about a couple debatable methods and wanted to get the options of others.

The question here is, which method do you find more efficient quick in the situation of needing to count the number of rows returned from a mysql_query. I was taught that mysql_num_rows() was a slow, inelegant, and overall bad method of doing this and that MySQL was just the opposite.

What are your opinions?

Example PHP:
$query = mysql_query("SELECT * FROM `users`");
$count = mysql_num_rows($query);
echo $count;



vs Example MySQL:
$query = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) FROM `users`"));
echo $query["COUNT(*)"];



Is This A Good Question/Topic? 0
  • +

Replies To: MySQL COUNT vs PHP mysql_num_rows

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 433
  • View blog
  • Posts: 789
  • Joined: 17-June 08

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 26 October 2009 - 08:06 AM

You learned right. Of course, it depends on the situation, but SELECT COUNT(*) is normally faster. The reason is simply that using mysql_num_rows(), the database server has to actually build the result set. With a COUNT(*), on the other hand, the DB may be able to determine the number of rows just from indexes, which is much faster. So if all you want is the count, then mysql_num_rows() ends up doing a lot more work for the same result.

Another down side is that mysql_num_rows doesn't work with unbuffered queries. So if you need to work with very large result sets, using mysql_num_rows might not be an option. However, COUNT(*) is always available.

On the other hand, if you need to buffer and actually use the results of the query anyway, then I imagine there isn't much difference. Though personally, I'm in the habit of just always using SELECT COUNT(*), since it always works and is normally pretty fast.
Was This Post Helpful? 0
  • +
  • -

#3 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 26 October 2009 - 01:05 PM

Thanks for the reply.

I was thinking: couldn't you do this if you wanted to select the count AND the actual values for use in one query?

SELECT *, COUNT(*) FROM `users`


Was This Post Helpful? 0
  • +
  • -

#4 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 433
  • View blog
  • Posts: 789
  • Joined: 17-June 08

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 26 October 2009 - 02:07 PM

View PostMaster Jake, on 26 Oct, 2009 - 02:05 PM, said:

I was thinking: couldn't you do this if you wanted to select the count AND the actual values for use in one query?

No, you couldn't. At least, not like that. You can't mix aggregate functions like COUNT() with non-aggregated columns. SQL doesn't work that way.

You could do something like you've posted in standard SQL by adding in a scalar subquery to do the count:
SELECT *, (SELECT COUNT(*) FROM `users`) AS cnt FROM `users`
However, that would be absolutely terrible idea. Not only is it ugly, but it would kill performance, as the count query would be run once for every row in the table.

However, MySQL does have an extension for that sort of thing. You can include SQL_CALC_FOUND_ROWS in the query to calculate the number, and then follow it up with a call to FOUND_ROWS() to return the number, like this:
SELECT SQL_CALC_FOUND_ROWS * FROM `users`;
SELECT FOUND_ROWS();

Of course, while it may seem better to do it in one query, that's not necessarily any faster. With proper indexing, the separate SELECT COUNT(*) can still beat it out.
Was This Post Helpful? 0
  • +
  • -

#5 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 26 October 2009 - 03:46 PM

Ooooh, I used SQL_CALC and FOUND_ROWS in my awesome paging system that displays the page the user is currently on and up to 3 adjacent pages with links, back and next buttons, etc.

=D I am proud of it.
Was This Post Helpful? 0
  • +
  • -

#6 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 05 November 2009 - 09:15 AM

I also have a question, I'm not that good in MySQL, I mean don't really know the advanced stuff like above, never heard of it but my question is: is there a difference between COUNT(*) AND COUNT(`id`), id is the primary key. I'm talking about speed btw
Was This Post Helpful? 0
  • +
  • -

#7 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 150
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 05 November 2009 - 10:59 AM

testing that in a table with 5000+ rows both were about the same, for me

Execution Time : 00:00:00:016 ( average )

but that's a VERY crude test at best through SQLyog ... and could probably be ruled null because of fluctuations in network traffic and so forth ... but thought i would post what i saw in that test
Was This Post Helpful? 0
  • +
  • -

#8 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 433
  • View blog
  • Posts: 789
  • Joined: 17-June 08

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 05 November 2009 - 12:18 PM

View Postnightscream, on 5 Nov, 2009 - 10:15 AM, said:

is there a difference between COUNT(*) AND COUNT(`id`), id is the primary key. I'm talking about speed btw

Yes there can be a speed difference. It depends on the column you're counting on. This is because COUNT(*) and COUNT(id) are not semantically equivalent - COUNT(*) will return the total number of rows in the table, while COUNT(id) will only count the number of rows where that column is not null. So if your id column is not nullable (which it probably isn't since 'id' is usually the primary key), then the performance should be about the same. However, if you use a nullable column for the count, it's going to be slower.
Was This Post Helpful? 0
  • +
  • -

#9 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 05 November 2009 - 02:52 PM

Actually the fastest method of getting a row count is to use SHOW TABLE STATUS LIKE `tablename` and take the Rows column.

For InnoDB this is only approximate, this will only affect you when there's ALOT (talking >15000) rows. But when it comes to counting that many rows with COUNT(*), you're going to take a huge performance hit.
Was This Post Helpful? 0
  • +
  • -

#10 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 433
  • View blog
  • Posts: 789
  • Joined: 17-June 08

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 05 November 2009 - 09:14 PM

View PostRudiVisser, on 5 Nov, 2009 - 03:52 PM, said:

For InnoDB this is only approximate, this will only affect you when there's ALOT (talking >15000) rows. But when it comes to counting that many rows with COUNT(*), you're going to take a huge performance hit.

Either that's a typo or you're setting your bar for "a lot of rows" a little low. Unless you're running your DB server on a pocket calculator, a COUNT(*) should never be slow on a table of only 15,000 rows. It's true that if the DB does the count by performing an index scan, then it will get slower as the table grows, but you'd need a lot more data. Maybe with 15 million rows and decent server load you'd see issues, but 15,000 rows is less than nothing. If you're having performance problems with a table that small, there's probably something wrong with your server.
Was This Post Helpful? 0
  • +
  • -

#11 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 06 November 2009 - 02:44 AM

Sorry I didn't mean a performance hit, I meant a performance difference (between COUNT(*) and SHOW TABLE STATUS).

But you're right, add a 0 or 2 and you'll start to see the difference.
Was This Post Helpful? 0
  • +
  • -

#12 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: MySQL COUNT vs PHP mysql_num_rows

Posted 08 November 2009 - 01:13 PM

Ok thank you, this was helpfull :)
btw how do you guys come up with this? just test the speed?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1