查看MySql数据库容量大小

2019/11 03 01:11
//查询所有数据库总大小
use information_schema;
select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES;
+---------+
| data    |
+---------+
| 25.33MB |
+---------+


//统计每个库大小
SELECT table_schema,SUM(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 AS total_mb FROM information_schema.TABLES group by table_schema;
+--------------------+-------------+
| table_schema       | total_mb    |
+--------------------+-------------+
| dbqb               |  0.04687500 |
| information_schema |        NULL |
| mysql              |  0.79547882 |
| performance_schema |  0.00000000 |
| uwlog              | 24.51486588 |
+--------------------+-------------+

//查看所有数据库各容量大小
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;
+--------------------+-----------+------------------+------------------+
| 数据库             | 记录数    | 数据容量(MB)     | 索引容量(MB)     |
+--------------------+-----------+------------------+------------------+
| uwlog              |      1700 |            24.51 |             0.00 |
| mysql              |      2553 |             0.70 |             0.03 |
| dbqb               |         6 |             0.04 |             0.00 |
| information_schema |      NULL |             0.00 |             0.00 |
| performance_schema |     74026 |             0.00 |             0.00 |
+--------------------+-----------+------------------+------------------+