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.
To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.
The SQL COUNT( ) function is used to return the number of rows in a table. It is used with the Select( ) statement.
The best way to get the record count is to use the sys. dm_db_partition_stats or sys. partitions system views (there is also sysindexes, but it has been left for the backward compatibility with SQL Server 2000).
If an approximation is enough, you can use:
show table status like 'tablename'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With