一、统计数据库大小

1、查看Mysql数据库大小
SELECT sum(DATA_LENGTH)+sum(INDEX_LENGTH) FROM information_schema.TABLES where TABLE_SCHEMA='数据库名';
# 得到的结果是以字节为单位,除1024为K,除1048576(=1024*1024)为M。

2、查看该数据库实例下所有库大小(MB为单位)



mysql> select table_schema,sum(data_length)/1024/1024 as data_length,sum(index_length)/1024/1024 as index_length,sum(data_length+index_length)/1024/1024 as sum from information_schema.tables;
+--------------------+---------------+---------------+---------------+
| table_schema       | data_length   | index_length  | sum           |
+--------------------+---------------+---------------+---------------+
| information_schema | 5665.37265301 | 2683.47949219 | 8348.85214520 |
+--------------------+---------------+---------------+---------------+
1 row in set (0.36 sec)

mysql>

3、查看该实例下各个库大小(MB为单位)


mysql> select table_schema, sum(data_length+index_length)/1024/1024 as total_mb, \ sum(data_length)/1024/1024 as data_mb, sum(index_length)/1024/1024 as index_mb, \
count(*) as tables, curdate() as today from information_schema.tables group by table_schema order by 2 desc;
+--------------------+---------------+---------------+---------------+--------+------------+
| table_schema       | total_mb      | data_mb       | index_mb      | tables | today      |
+--------------------+---------------+---------------+---------------+--------+------------+
| test_db            | 4118.46875000 | 2911.87500000 | 1206.59375000 |     74 | 2016-04-19 |
| mysql              |    2.74277020 |    2.52890301 |    0.21386719 |     31 | 2016-04-19 |
| information_schema |    0.15625000 |    0.15625000 |    0.00000000 |     61 | 2016-04-19 |
| sys                |    0.01562500 |    0.01562500 |    0.00000000 |    101 | 2016-04-19 |
| performance_schema |    0.00000000 |    0.00000000 |    0.00000000 |     87 | 2016-04-19 |
+--------------------+---------------+---------------+---------------+--------+------------+

二、统计表大小

1、 查看表的基本信息
select * from information_schema.TABLES where information_schema.TABLES.TABLE_SCHEMA = '数据库名' and information_schema.TABLES.TABLE_NAME = '表名';

2、查看表大小(MB为单位)

select concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB,  
     concat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB  
     from information_schema.tables 
     where table_schema='数据库名' and table_name = '表名';  

扩展

information_schema 库的 TABLES 表,主要字段分别是:

TABLE_SCHEMA :数据库名
TABLE_NAME:表名
ENGINE:所使用的存储引擎
TABLES_ROWS:记录数
DATA_LENGTH:数据大小
INDEX_LENGTH:索引大小

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐