《转载》查看 MySQL 数据库中每个表占用的空间大小

时间:2024-04-06 19:16:20

 

查看 MySQL 数据库中每个表占用的空间大小

原文地址:

如何查看mysql数据库各个表数据占用空间大小? - sunrainamazing的博客 - CSDN博客
https://blog.csdn.net/sunrainamazing/article/details/80342157

查询mysql中一个数据库里各个表所占用空间的大小 - qq_40202396的博客 - CSDN博客
https://blog.csdn.net/qq_40202396/article/details/86489912

 

1)查看information_schematables 表:

  • 首先进入mysql数据库
  • 打开information_schema这个库
  • 找到tables 这张表,如下图所示:

《转载》查看 MySQL 数据库中每个表占用的空间大小

 

2information_schema.tables表中,各字段含义:

通常情况下,只需要 当前库的数据的大小,因此要知道下面的字段的含义

  • table_schema 库名
  • table_name  : table_schema库下对应的表名
  • engine:  表的引擎
  • version:  当前版本
  • table_rows:  table_name表对应的记录数
  • data_length: 数据大小
  • index_length: 索引结构大小(两者加起来是总数据空间的容量 data_length + index_length)
  • table_collation: 字符校对集

其他字段请参考MySQL的手册,我们只需要了解这几个就足够了。

所以要知道一个表占用空间的大小,那就相当于是 数据大小 + 索引大小 即可。

 

3)查看MySQL数据库中每个表占用的空间大小

 

-- 查询所有库的信息

select * from information_schema.tables;

 

--查询所有数据的大小 

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

 

--查看指定数据库实例的大小,比如说数据库 db_test

select concat(round(sum((data_length + index_length)/1024/1024), 2),'MB') as data  from TABLES where table_schema='db_test';

 

--查看指定数据库的表的大小,比如说数据库 db_test中的 info表 

select concat(round(sum((data_length + index_length)/1024/1024), 2),'MB') as data as indexLen from TABLES where table_schema='db_test' and table_name='info';