Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql list tables and sizes - order by size

Tags:

mysql

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

like image 225
Marty Wallace Avatar asked Jan 28 '13 19:01

Marty Wallace


People also ask

How do I find the size of a specific table in MySQL?

To check the sizes of all of the tables in a specific database, at the mysql> prompt, type the following command. Replace database_name with the name of the database that you want to check: Copy SELECT table_name AS "Table", ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)" FROM information_schema.

What is Information_schema tables in MySQL?

INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictionary and system catalog.

How do I find the index and table size in MySQL?

I think this is what you're looking for. Avg_row_length, Data_length, Max_data_length, Index_length, Data_free are all reported in bytes, yes. This gives the total index size, not a size per index (assuming a table has multiple indices).


2 Answers

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

like image 82
MG_Bautista Avatar answered Oct 05 '22 15:10

MG_Bautista


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; 
like image 40
minhas23 Avatar answered Oct 05 '22 15:10

minhas23