用SQL命令查看Mysql数据库大小

要想知道每个数据库的大小的话,步骤如下:
1、进入information_schema 数据库(存放了其他的数据库的信息)

use information_schema;

2、查询所有数据的大小:

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;

3、查看指定数据库的大小:
比如查看数据库home的大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home';

4、查看指定数据库的某个表的大小
比如查看数据库home中 members 表的大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home' and table_name='members';

查询所有数据库占用磁盘空间大小的SQL语句:

select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),' MB') as data_size,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables
group by TABLE_SCHEMA
order by data_length desc;

查询单个库中所有表磁盘占用大小的SQL语句:

select TABLE_NAME, concat(truncate(data_length/1024/1024,2),' MB') as data_size,
concat(truncate(index_length/1024/1024,2),' MB') as index_size
from information_schema.tables where TABLE_SCHEMA = 'TestDB'
group by TABLE_NAME
order by data_length desc;

以上语句测试有效,注意替换以上的TestDB为数据库名

原文地址:https://www.cnblogs.com/xzlive/p/9381064.html