0

I have to count the total numbers of records from my database. I want to know the efficient/faster way for MySQL to do this. I can use:

(1)

$rslt=mysql_query("select * from table");
$count = mysql_num_rows($rslt);

or (2) by using the following query:

select count(*) from table;

Which QUERY to the database is efficient?

Parveez Ahmed
  • 1,325
  • 4
  • 17
  • 28

2 Answers2

3

The first (select * from ...) will have to actually read the rows, while the second will return an int from the database. The second will be much faster with large tables.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

If your table is really large I would expect the 2nd query to outperform the first.

  1. It does not need to actually fetch a ton of data from DB to client. SELECT * is a horrible idea in such case. SELECT 1 would not have to load every field.

  2. If your table is indexed on its primary key, RDBMS might be able to get the result excursively from index.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111