mysql中有一个默认的数据表information_schemainformation_schema这张数据表保存了MySQL服务器所有数据库的信息。如数据库名数据库的表,表栏的数据类型访问权限等。再简单点,这台MySQL服务器上,到底有哪些数据库、各个数据库有哪些表,每张表的字段类型是什么,各个数据库要什么权限才能访问,等等信息都保存在information_schema表里面,所以请勿删改此表。

切换数据库

1
use information_schema;

查看所有数据库容量大小

1
2
3
4
5
6
7
8
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;

查看指定数据库使用大小

1
2
3
4
5
6
7
8
# short_video 为库名
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
where table_schema='short_video';

查看指定表使用大小

1
2
3
4
5
6
7
# short_video为库名 video_info为表名 
select table_name as '表名',
table_rows as '记录数',
concat(round(sum(data_length/1024/1024),2),'MB') as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from tables
where table_schema='short_video' and table_name='video_info';

查看所有数据库各表容量大小

1
2
3
4
5
6
7
8
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc;

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

1
2
3
4
5
6
7
8
9
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
where table_schema='short_video'
order by data_length desc, index_length desc;