
下面的这段代码可以看到数据库表的数据容量以及索引所占的空间大小,data_length和index_length表示数据长度和索引长度,单位为byte,除以两次1024(计算机领域表示长度或容量的进制为1024,1P=1024T=1024²G=1024³M=(1024*1024)²Byte),单位转换成MB。数据库名作为条件,可以同时查询多个库,查询结果从大到小排序。
注意:执行该sql要在shema下,不要选择任何数据库。
SELECT table_schema AS '数据库', table_name AS '表名', table_rows AS '记录数', TRUNCATE (data_length / 1024 / 1024, 2) AS '数据容量(MB)', TRUNCATE (index_length / 1024 / 1024, 2) AS '索引容量(MB)'FROM information_schema.tables where table_schema = 'tablename'ORDER BY data_length DESC, index_length DESC; #工具变种(查总记录数以及总容量) SELECT table_schema AS '数据库', table_name AS '表名', table_rows AS '记录数', TRUNCATE (data_length / 1024 / 1024, 2) AS '数据容量(MB)', TRUNCATE (index_length / 1024 / 1024, 2) AS '索引容量(MB)'FROM information_schema.tables where table_schema = 'tablename'ORDER BY data_length DESC, index_length DESC;

执行结果
从结果可以明确的看出每个表对应的记录数,所占的空间大小以及所以所占空间大小。
排在最后的一些表没有任何记录,可以从业务的角度分析原因,根据需要清理冗余的表好使数据库瘦身。


