Reputation: 78421
What is the way to most efficiently count the total number of rows in a large table? I have a table with 23 million rows and the following query takes 30+ seconds on production hardware:
select count(*) from tablename;
It seems that MySQL must be doing a table scan, but it doesn't seem like this should be necessary.
Upvotes: 15
Views: 6513
Reputation: 2041
If an approximation is enough, you can use:
show table status like 'tablename'
Upvotes: 21
Reputation: 91902
Row count of a table is slow in InnoDB.
MyISAM on the other hand have the row count as a property of the table, which makes your query really fast on MyISAM.
Upvotes: 3