What is the MySQL command to retrieve the count of records in a table?
12 Answers
SELECT COUNT(*) FROM fooTable;
will count the number of rows in the table.
See the reference manual.

- 69,011
- 20
- 139
- 164
-
8Is it any faster when I use name of indexed column instead of * ? Like this: SELECT COUNT(id) FROM `tablename` – Nov 08 '12 at 21:01
-
1Only slightly. I got a 21961904 row table which his COUNT queried in 115 sec, while using the ID took 107 sec. – Bondolin Jun 06 '13 at 11:44
-
2COUNT(\*) is a strict language definition. You will get a parse error if you try COUNT (\*) note the space – ppostma1 Jun 23 '14 at 18:30
-
9You can do `COUNT(1)`, this will be the fastest way. – Shota Papiashvili Sep 14 '16 at 17:05
-
What is the best way for using COUNT() to write a variable in PHP? Do I do "...COUNT(*) AS rowCount..." in the SQL, does it use $results->num_rows, or is there a way to call this result directly? – Nosajimiki Apr 24 '17 at 19:26
-
wouldn't it be more efficient or less computationally intensive to count a single column instead of ALL columns??? – oldboy Mar 30 '18 at 22:22
-
@Nosajimiki once you execute the prepared statement and fetch the results... `$result = $statement->fetch(PDO::FETCH_ASSOC);`... you can then call it by `$result["COUNT(*)"];` – oldboy Mar 30 '18 at 22:26
-
COUNT(*) function without a WHERE clause or additional columns, will perform very fast on MyISAM tables because the number of rows is stored in the table_rows column in the tables table of the information_schema database. For transactional storage engines such as InnoDB, storing an exact row count is problematic because InnoDB does not keep an internal count of rows in a table. Source: https://www.navicat.com/en/company/aboutus/blog/695-getting-row-counts-in-mysql-part-1 – Diana Oct 27 '19 at 18:21
-
InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) operations in the same way. There is no performance difference. Source: https://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html – Yusuf Çağlar Jan 26 '20 at 01:38
Because nobody mentioned it:
show table status;
lists all tables along with some additional information, including estimated rows for each table. This is what phpMyAdmin is using for its database page.
This information is available in MySQL 4, probably in MySQL 3.23 too - long time prior information schema database.
The number shown is estimated for InnoDB and TokuDB but it is absolutely correct for MyISAM and Aria (Maria) storage engines.
Per the documentation:
The number of rows. Some storage engines, such as MyISAM, store the exact count. For other storage engines, such as InnoDB, this value is an approximation, and may vary from the actual value by as much as 40% to 50%. In such cases, use SELECT COUNT(*) to obtain an accurate count.
This also is fastest way to see the row count on MySQL, because query like:
select count(*) from table;
Doing full table scan what could be very expensive operation that might take hours on large high load server. It also increase disk I/O.
The same operation might block the table for inserts and updates - this happen only on exotic storage engines.
InnoDB and TokuDB are OK with table lock, but need full table scan.
-
3This looks like the most efficient way to count rows. I wonder why it is not the answer? I'm using InnoDB. With table lock, the number of rows should be exact right? – Chung Lun Yuen Oct 27 '16 at 14:07
-
for innodb it is **estimated**. but you can use it in some cases "Our website have xxx members", "We detected xxx results similar to yours" and so on. – Nick Oct 27 '16 at 14:13
-
I am not sure. but for Innodb is not real count. But is quite useful for million row tables. – Nick Oct 28 '16 at 01:01
-
4Going through tables to count each row to get a row count is rather ridiculous. So I prefer this answer as well. – th3penguinwhisperer Jun 18 '17 at 21:05
We have another way to find out the number of rows in a table without running a select
query on that table.
Every MySQL instance has information_schema database. If you run the following query, it will give complete details about the table including the approximate number of rows in that table.
select * from information_schema.TABLES where table_name = 'table_name'\G

- 2,870
- 24
- 39
- 44

- 759
- 9
- 19
-
Is this faster as well on a MyISAM table since MyISAM is storing the number of rows? – NaturalBornCamper Mar 08 '17 at 11:06
-
If you have several fields in your table and your table is huge, it's better DO NOT USE *
because of it load all fields to memory and using the following will have better performance
SELECT COUNT(1) FROM fooTable;

- 7,931
- 11
- 67
- 103
-
1This is incorrect. See https://stackoverflow.com/questions/5179969/what-is-better-in-mysql-count-or-count1 COUNT(1) may actually be much slower for MyISAM. – jcoffland Oct 25 '18 at 15:43
-
@jcoffland you can try t by myself, especially when you have a join or where conditions is will be much much faster than count(*). – Yuseferi Oct 26 '18 at 04:36
Just do a
SELECT COUNT(*) FROM table;
You can specify conditions with a Where after that
SELECT COUNT(*) FROM table WHERE eye_color='brown';

- 471
- 1
- 4
- 12
As mentioned by Santosh, I think this query is suitably fast, while not querying all the table.
To return integer result of number of data records, for a specific tablename in a particular database:
select TABLE_ROWS from information_schema.TABLES where TABLE_SCHEMA = 'database'
AND table_name='tablename';

- 4,452
- 10
- 23
- 47
-
1This may return an estimated number of rows. In one example, I got 55,940,343 rows using COUNT(*) but 56,163,339 using this method so it was off by more than 200k. – jcoffland Oct 25 '18 at 15:46
-
@jcoffland, I mentioned the answer by _ Santosh_ above which states the result is **approximate**. My answer is a more detailed practical query. Its quite clear across answers if you want precise count use `count(*)` with performance awareness – Mohammad Kanan Oct 25 '18 at 21:54
If you have a primary key or a unique key/index, the faster method possible (Tested with 4 millions row tables)
SHOW INDEXES FROM "database.tablename" WHERE Key_Name=\"PRIMARY\"
and then get cardinality field (it is close to instant)
Times where from 0.4s to 0.0001ms

- 938
- 1
- 11
- 22
$sql="SELECT count(*) as toplam FROM wp_postmeta WHERE meta_key='ICERIK' AND post_id=".$id;
$total = 0;
$sqls = mysql_query($sql,$conn);
if ( $sqls ) {
$total = mysql_result($sqls, 0);
};
echo "Total:".$total;`
-
This answer is outdated, mysql_result is deprecated in PHP 5.5.0 and removed in PHP 7.0.0 – luke77 Jun 01 '20 at 13:20
You have to use count() returns the number of rows that matches a specified criteria
select count(*) from table_name;

- 3,588
- 2
- 39
- 40
It can be convenient to select count with filter by indexed field. Try this
EXPLAIN SELECT * FROM table_name WHERE key < anything;

- 18,910
- 11
- 45
- 55