Last Updated: February 25, 2016
·
558
· mohamedalaa

mysql list all tables with sizes

If you ever wants to who all your tables ordered by table size. use this snippet:


SET @DBNAME = "test_development"; #database name

SELECT CONCAT(table_name) AS "Tables",
       CONCAT(ROUND(table_rows / 1000000, 2), 'M')                                    rows,
       CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G')                    DATA,
       CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G')                   idx,
       CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
       ROUND(index_length / data_length, 2)                                           idxfrac
FROM   information_schema.TABLES
WHERE table_schema = @DBNAME
ORDER  BY data_length + index_length DESC;

Sample Results

+-------------------+-------+-------+-------+------------+---------+
| Tables            | rows  | DATA  | idx   | total_size | idxfrac |
+-------------------+-------+-------+-------+------------+---------+
| users             | 0.00M | 0.00G | 0.00G | 0.00G      |    2.00 |
| addresses         | 0.00M | 0.00G | 0.00G | 0.00G      |    0.00 |
| services          | 0.00M | 0.00G | 0.00G | 0.00G      |    0.00 |
| schema_migrations | 0.00M | 0.00G | 0.00G | 0.00G      |    0.00 |
| bookings          | 0.00M | 0.00G | 0.00G | 0.00G      |    0.00 |
+-------------------+-------+-------+-------+------------+---------+
5 rows in set (0.17 sec)