收缩表找出可用空间最多的表
- 删除数据或清空表之后,mysql的数据表不会自动变小,此时需要找出可用空间最多的几张表,然后执行 optimize 操作
select
table_schema,
table_name,
table_rows,
round(sum(data_length+index_length)/1024/1024,2) as total_size_mb,
round(sum(data_length)/1024/1024,2) as data_size_mb,
round(sum(index_length)/1024/1024,2) as index_size_mb,
round(sum(data_free)/1024/1024,2) as free_size_mb,
concat('optimize table ',table_name,';') as optimize_table
from information_schema.tables
where table_schema not in ('information_schema','mysql','performance_schema','sys','percona_schema')
group by table_schema,table_name
order by data_free desc
limit 30;
注:2025.08.05 实测,大表 file_info 共 78163437 行,总计 93.62 GB(数据 58.45 GB + 索引 35.16 GB),可用 47.19 GB,执行 optimize 耗时 74分钟,其中优化过程会额外占用 58 GB 磁盘空间(执行结束会一起释放)、12 GB 内存(不太准)
[root@localhost]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/vdb xfs 1.0T 561G 463G 55% /data #.收缩之前
/dev/vdb xfs 1.0T 619G 406G 61% /data #.收缩中
/dev/vdb xfs 1.0T 496G 529G 49% /data #.收缩完成后