45

What would be the query to list all tables in a database order by their size in mysql?

Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • 1
    [What have you tried?](http://www.whathaveyoutried.com/) See [ask advice](http://stackoverflow.com/questions/ask-advice), please. – John Conde Jan 28 '13 at 19:49
  • possible duplicate of [How to get the sizes of the tables of a mysql database?](http://stackoverflow.com/questions/9620198/how-to-get-the-sizes-of-the-tables-of-a-mysql-database) – Kermit Jan 28 '13 at 19:49
  • Check http://www.mysqlperformanceblog.com/2008/02/04/finding-out-largest-tables-on-mysql-server/ – Ravindra Gullapalli Jan 28 '13 at 19:51
  • Related, if it's of interest, I wrote a *Describe All Tables* in [this Answer](http://stackoverflow.com/a/38693721). – Drew Aug 01 '16 at 15:37

3 Answers3

105

Try this...

SELECT TABLE_NAME, table_rows, data_length, index_length, 
round(((data_length + index_length) / 1024 / 1024),2) "Size in MB"
FROM information_schema.TABLES WHERE table_schema = "schema_name"
ORDER BY (data_length + index_length) DESC;

Note: Replace schema_name above with the name of your database

Cave Johnson
  • 6,499
  • 5
  • 38
  • 57
MG_Bautista
  • 2,593
  • 2
  • 18
  • 33
6

Run the following query in the mysql client

SELECT table_name AS "Tables", 
round(((data_length + index_length) / 1024 / 1024), 2) "Size in MB" 
FROM information_schema.TABLES 
WHERE table_schema = "$DB_NAME"
ORDER BY (data_length + index_length) DESC;
minhas23
  • 9,291
  • 3
  • 58
  • 40
-1

Execute following query in information_schema database:

SELECT table_schema AS "Database name",
SUM(data_length + index_length) / 1024 / 1024 AS "Size (MB)" 
FROM information_schema.TABLES 
GROUP BY table_schema 
ORDER BY (SUM(data_length + index_length) / 1024 / 1024) DESC;
David Motilla
  • 775
  • 1
  • 4
  • 5