MySQL中InnoDB引擎的表存储容量我们有什么方法可以计算出来?MOS的这篇文章《How can I determine the total size of my InnoDB tables/dataset?》(Doc ID 1516454.1)给了我们答案。

按照文章所说,可以从数据库层面通过information_schema的tables视图了解innodb存储引擎的表容量(包括数据和索引),
mysql> select round((sum(data_length+index_length))/1024/1024) AS tables_M from information_schema.tables where engine="innodb";
返回:313
还可以通过统计操作系统的数据库文件容量来计算,
mysql> du -csh *.ibd
返回:
...
112K test1.ibd
16M test2.ibd
542M total
但是这两个值,很可能是不等的,文章中说明了,即如果通过DELETE进行了数据删除,就可能出现这种情况,
It depends on whether you’re wondering how much actual data you currently have (#1) or how much disk space is currently being used (#2). While an InnoDB table may currently only have 1K of data in it, the .ibd fle may be 100G in size (let’s say that you just ran a DELETE FROM {$tablename}; on that table).
为规避两者不一致的场景,文章中提到了,You can get rid of the unused space in the .ibd file using the OPTIMIZE TABLE command,可以通过执行optimize table,它会重组表数据和索引的物理存储,减少对存储空间使用和提升访问表时io效率,具体可参考《小白学习MySQL – InnoDB支持optimize table?》
因此如果有监控工具需要对MySQL空间容量进行监控的需求,就需要根据实际的需求,用准确的统计,避免出现误算。
声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/423983.html