【MySQL】计算磁盘空间使用

Posted by 西维蜀黍 on 2023-02-20, Last Modified on 2023-09-22

统计每个DB的磁盘空间使用

mysql>  select table_schema "DB name (table_schema)",
    -> sum((data_length+index_length)/1024/1024/1024) AS "DB size in GB" from
    -> information_schema.tables group by table_schema;
+-----------------------------+-------------------+
| DB name (table_schema)      | DB size in GB     |
+-----------------------------+-------------------+
| information_schema          |    0.000167846679 |
| xxyyzz_coins_v3_db_00000004 |  210.166442871299 |
| xxyyzz_coins_v3_db_00000004 | 1387.163970947461 |
+-----------------------------+-------------------+
3 rows in set (0.85 sec)
du -sch /location/of_Mysql/* | sort -hr | head -n20

统计每个table的磁盘空间使用

select table_schema,table_name,
truncate(data_length/1024/1024/1024, 2) as 'data_size_in_gb',
truncate(index_length/1024/1024/1024, 2) as 'index_size_in_gb',
truncate(DATA_FREE/1024/1024/1024, 2) as 'free_space_in_gb' 
from information_schema.tables where table_name = "<table_name>" and table_schema like '<db_nmae>';

Reference