今天就跟大家聊聊有关MySQL中怎么查看表占用空间大小,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

代码

1,切换数据库

useinformation_schema;

2,查看数据库使用大小

selectconcat(round(sum(data_length/1024/1024),2),’MB’)asdatafromtableswheretable_schema=’DB_Name’;

3,查看表使用大小

selectconcat(round(sum(data_length/1024/1024),2),’MB’)asdatafromtableswheretable_schema=’DB_Name’andtable_name=’Table_Name’;

网上找的一个,亲测可用:

先进去MySQL自带管理库:information_schema

然后查询 data_length,index_length

你自己的数据库名:dbname

你自己的表名:tablename

mysql>useinformation_schema;Databasechangedmysql>selectdata_length,index_length->fromtableswhere->table_schema='dbname'->andtable_name='tablename';+-------------+--------------+|data_length|index_length|+-------------+--------------+|166379520|235782144|+-------------+--------------+rowinset(0.02sec)

mysql>selectconcat(round(sum(data_length/1024/1024),2),'MB')asdata_length_MB,->concat(round(sum(index_length/1024/1024),2),'MB')asindex_length_MB->fromtableswhere->table_schema='dbname'->andtable_name='tablename';+----------------+-----------------+|data_length_MB|index_length_MB|+----------------+-----------------+|158.67MB|224.86MB|+----------------+-----------------+rowinset(0.03sec)

1.查看所有数据库容量大小

selecttable_schemaas'数据库',sum(table_rows)as'记录数',sum(truncate(data_length/1024/1024,2))as'数据容量(MB)',sum(truncate(index_length/1024/1024,2))as'索引容量(MB)'frominformation_schema.tablesgroupbytable_schemaorderbysum(data_length)desc,sum(index_length)desc;```###2.查看所有数据库各表容量大小```sqlselecttable_schemaas'数据库',table_nameas'表名',table_rowsas'记录数',truncate(data_length/1024/1024,2)as'数据容量(MB)',truncate(index_length/1024/1024,2)as'索引容量(MB)'frominformation_schema.tablesorderbydata_lengthdesc,index_lengthdesc;

3.查看指定数据库容量大小

例:查看mysql库容量大小

selecttable_schemaas'数据库',sum(table_rows)as'记录数',sum(truncate(data_length/1024/1024,2))as'数据容量(MB)',sum(truncate(index_length/1024/1024,2))as'索引容量(MB)'frominformation_schema.tableswheretable_schema='mysql';

4.查看指定数据库各表容量大小

例:查看mysql库各表容量大小

selecttable_schemaas'数据库',table_nameas'表名',table_rowsas'记录数',truncate(data_length/1024/1024,2)as'数据容量(MB)',truncate(index_length/1024/1024,2)as'索引容量(MB)'frominformation_schema.tableswheretable_schema='mysql'orderbydata_lengthdesc,index_lengthdesc;

selectconcat(round(sum(data_length/1024/1024),2),'MB')asdata_length_MB,concat(round(sum(index_length/1024/1024),2),'MB')asindex_length_MBfromtableswheretable_schema='passport'andtable_name='tb_user_info';

-- 569.98MB 141.98MB

selectconcat(round(sum(data_length/1024/1024),2),'MB')asdata_length_MB,concat(round(sum(index_length/1024/1024),2),'MB')asindex_length_MBfromtableswheretable_schema='passport_v2'andtable_name='tb_user_info';

-- 2128.94MB 285.00MB

看完上述内容,你们对MySQL中怎么查看表占用空间大小有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。