mysql查看数据库或表的占用磁盘空间

mysql查看数据库或表的占用磁盘空间

查看全部库
```
select TABLE_SCHEMA,
concat(truncate(sum(data_length)/1024/1024,2),' MB') as data_size,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables group by TABLE_SCHEMA order by data_length desc;
```
单个库
```
select concat(truncate(sum(data_length)/1024/1024,2),'MB') as data_size,
concat(truncate(sum(max_data_length)/1024/1024,2),'MB') as max_data_size,
concat(truncate(sum(data_free)/1024/1024,2),'MB') as data_free,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables where TABLE_SCHEMA ='databasename';
```
单个库所有表
```
select TABLE_NAME, concat(truncate(data_length/1024/1024,2),' MB')as data_size,
concat(truncate(index_length/1024/1024,2),' MB') as index_size
from information_schema.tables where
TABLE_SCHEMA = 'databasename' group by TABLE_NAME order by data_length desc;
```
注:sql语句中的databasename替换成自己要查的库名

或者通过磁盘统计大致查看:
```
mysql> select @@datadir;
+-----------------------+
| @@datadir |
+-----------------------+
| /usr/local/mysql/var/ |
+-----------------------+
1 row in set (0.00 sec)
```
```
du -h /usr/local/mysql/var/ --max-depth=1
```

posted @ 2020-09-05 10:21  小明大强  阅读(817)  评论(0编辑  收藏  举报