Managing disk space on MySQL

Below are a bunch of helpful SQL queries to find tables and DB’s using up disk space;

This finds the largest 10 tables across your MySQL server;

SELECT CONCAT(table_schema, '.', table_name),
       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
ORDER  BY data_length + index_length DESC
LIMIT  10;

This gets the size of each of your databases, ordered smallest to largest;

SELECT table_schema, CONCAT(ROUND( sum((data_length+index_length)/1024/1024)/1024, 2), 'G') AS MB from information_schema.tables group by 1 order by sum(data_length+index_length) asc;

Ref; https://serverfault.com/questions/676669/amazon-rds-instance-slowly-losing-free-diskspace

Leave a Reply