Most efficient way to get table row count Most efficient way to get table row count sql sql

Most efficient way to get table row count


Following is the most performant way to find the next AUTO_INCREMENT value for a table. This is quick even on databases housing millions of tables, because it does not require querying the potentially large information_schema database.

mysql> SHOW TABLE STATUS LIKE 'table_name';// Look for the Auto_increment column

However, if you must retrieve this value in a query, then to the information_schema database you must go.

SELECT `AUTO_INCREMENT`FROM   INFORMATION_SCHEMA.TABLESWHERE  TABLE_SCHEMA = 'DatabaseName'AND    TABLE_NAME   = 'TableName';


If it's only about getting the number of records (rows) I'd suggest using:

SELECT TABLE_ROWSFROM information_schema.tables WHERE table_name='the_table_you_want' -- Can end here if only 1 DB   AND table_schema = DATABASE();      -- See comment below if > 1 DB

(at least for MySQL) instead.


try this

Execute this SQL:

SHOW TABLE STATUS LIKE '<tablename>'

and fetch the value of the field Auto_increment