information_schema库中存储了MySQL数据库的统计信息,可以通过它来获取相关信息。
1. 查看某个数据库大小
如果只看数据大小,可以不加索引大小 sum(INDEX_LENGTH/1024/1024),1)
select concat(round(sum(DATA_LENGTH/1024/1024) + sum(INDEX_LENGTH/1024/1024),1),'M')
from information_schema.tables where table_schema='数据库名';
DATA_LENGTH:数据长度(字节单位)
INDEX_LENGTH:索引长度
/1024/1024:字节转换为M
round:四舍五入,留1位小数
concat:拼接M
2. 所有表大小
use information_schema;
# 加个别名
select concat(round(sum(DATA_LENGTH/1024/1024) + sum(INDEX_LENGTH/1024/1024),1),'M') as data from tables;
3. 某个库的某个表大小
select concat(round(sum(DATA_LENGTH/1024/1024)+sum(INDEX_LENGTH/1024/1024),1),'M') as data
from tables where table_schema='数据库名' and table_name='表名';