[MySQL](EN) Get table's row count and size

Get table’s row count and size in MySQL


Environment and Prerequisite

  • MySQL


Row Count

  • Use COUNT(*) in query
select count(*) from '{table_name}';
  • Get estimated value using information_schema.TABLES
select TABLE_SCHEMA, TABLE_NAME, TABLE_ROWS from information_schema.TABLES where TABLE_SCHEMA='{database_name}' and TABLE_NAME='{table_name}';


Size

  • Use information_schema.TABLES
select TABLE_SCHEMA, TABLE_NAME, ((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) as 'Size in MB' from information_schema.TABLES where TABLE_SCHEMA='{database_name}' and TABLE_NAME='{table_name}';


Get row count and size using one query

  • Use information_schema.TABLES
select TABLE_SCHEMA, TABLE_NAME, TABLE_ROWS, ((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) as 'Size in MB' from information_schema.TABLES where TABLE_SCHEMA='{database_name}' and TABLE_NAME='{table_name}';


Reference