当前位置: 首页 > 科技观察

查看MySQL占用表的大小

时间:2023-03-18 23:26:54 科技观察

前言mysql中有一个默认的数据表information_schema,information_schema数据表存储了MySQL服务器所有数据库的信息。比如数据库名,数据库的表,表列的数据类型和访问权限等。简单的说就是这个MySQL服务器上有哪些数据库,每个数据库中有哪些表,字段是什么information_schema表中存储了每张表的类型、访问每张数据库需要什么权限等信息,因此请不要删除或更改此表。代码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你自己的表名:tablenamemysql>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'database',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'database',sum(table_rows)as'numberofrecords',sum(truncate(data_length/1024/1024,2))as'datacapacity(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.98MB141.98MBselectconcat(round(sum(data_length/12024/12424/),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.94MB285.00MB