Mysql 查看数据库各表容量大小

本文详细介绍了如何使用MySQL命令来查询数据库及表的容量大小,包括查询所有数据库、指定数据库的总容量,以及各个表的容量信息。通过提供的SQL语句,你可以快速获取数据容量和索引容量等关键数据。

本文介绍MySQL查看数据库表容量大小的命令语句,提供完整查询语句及实例,方便大家学习使用。


1.查看所有数据库容量大小

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;


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

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;


3.查看指定数据库容量大小
例:查看mysql库容量大小

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='mysql';

4.查看指定数据库各表容量大小
例:查看mysql库各表容量大小

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='mysql'
order by data_length desc, index_length desc;


 

MySQL 中,可使用以下几种方法查询数据库占用空间: - 查询数据库中所有数据占用空间: ```sql SELECT concat( round( sum( DATA_LENGTH / 1024 / 1024 ), 2 ), 'MB' ) AS DATA FROM TABLES; ``` - 查询指定数据库的指定数据占用空间: ```sql SELECT concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'MB' ) AS DATA FROM TABLES WHERE table_schema = '{your database}' AND table_name = '{your table name}'; ``` - 查询指定数据库的指定索引占用空间: ```sql SELECT concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'MB' ) AS DATA FROM TABLES WHERE table_schema = '{your database}' AND table_name = '{your table name}'; ``` - 查看某个数据库的各个占用的空间(以 MB 为单位),包含记录数、数据容量和索引容量: ```sql 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='mysql' order by data_length desc, index_length desc; ``` - 查询指定数据库的数据大小、索引大小和总大小: ```sql SELECT round(sum(data_length / 1024 / 1024),2) 'Data Size in MB',round(sum(index_length / 1024 / 1024),2) 'Index Size in MB',round(sum((index_length + data_length) / 1024 / 1024),2) 'All Size in MB' FROM information_schema.TABLES WHERE table_schema = '库名'; ``` 上述代码分别来自不同的引用,具体引用信息如下:第一种方法来自引用 [1],第二种方法来自引用 [1],第三种方法来自引用 [1],第四种方法来自引用 [2],第五种方法来自引用 [4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值