前言
我们在使用mysql时,经常需要查询mysql中各库的大小。这时,我们可以:
正文
mysql中有一个默认的系统库:information_schema,其中有个table表。我们可以通过查询此表来查看各表大小。
SQL如下:
USE information_schema;
SELECT TABLE_SCHEMA, SUM(DATA_LENGTH) FROM TABLES GROUP BY TABLE_SCHEMA; #默认单位为B
若想将其转化为KB、MB、GB,则分别除以1、2、3个1024即可。
USE information_schema;
SELECT TABLE_SCHEMA, SUM(DATA_LENGTH)/1024 FROM TABLES GROUP BY TABLE_SCHEMA; #默认单位为KB
USE information_schema;
SELECT TABLE_SCHEMA, SUM(DATA_LENGTH)/1024/1024 FROM TABLES GROUP BY TABLE_SCHEMA; #默认单位为MB
USE information_schema;
SELECT TABLE_SCHEMA, SUM(DATA_LENGTH)/1024/1024/1024 FROM TABLES GROUP BY TABLE_SCHEMA; #默认单位为GB