At 10/7/09 08:45 AM, citricsquid wrote:
How fast is mysql_num_rows compared to count, is it a noticeable difference?
Yes. "SELECT COUNT(*) FROM table"; will always return ONE row with one cell (unless you're doing somethign like "SELECT COUNT(*), AVG(somefield) FROM table[ GROUP BY whatever]", which would give you two cells etc), even if the output is 0 (i.e. the dataset is empty.) "SELECT [whatever] FROM table [WHERE... GROUP BY.... ORDER BY....etc]"; and then analysing that dataset with mysql_num_rows() will force the database to retrieve every row in the database that matches the query. Doing this just to get a tally of the number of entries is assinine.
Imagine you have 1 million rows in your table and all you want is the total number of entries in that table. You really don't want to sit waiting around for a query to fetch every single row back from the table just to get that total, which is what you'd be doing by selecting a dataset and then running that through mysql_num_rows().
Here's an example of output from a COUNT():
+----------+
| COUNT(*) |
+----------+
| 1951870 |
+----------+
1 row in set (0.00 sec)