大师网-带你快速走向大师之路 解决你在学习过程中的疑惑,带你快速进入大师之门。节省时间,提升效率

如何检查数据库各个Database和Table的大小?

查看database大小

如下命令会显示各Database大小(以MB计算)。

SELECT table_schema AS "Database", SUM(data_length + index_length) / 1024 / 1024 AS "Size (MB)" FROM information_schema.TABLES GROUP BY table_schema

结果如下:

| Database           | Size (MB)    |
+--------------------+--------------+
| http_certs         | 941.79687500 |
| information_schema |   0.15625000 |
| mysql              |   2.39560223 |
| performance_schema |   0.00000000 |
| sys                |   0.01562500 |
+--------------------+--------------+

查看某个特定table大小

将下面命令中的$DB_NAME$TABLE_NAME替换为要查询的database和table名即可。

SELECT 
    table_name AS `Table`, 
    round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
WHERE table_schema = "$DB_NAME"
    AND table_name = "$TABLE_NAME";

查看全部table的大小

下面的命令由大到小列出每一个数据库中每一个表的大小。

SELECT 
     table_schema as `Database`, 
     table_name AS `Table`, 
     round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
ORDER BY (data_length + index_length) DESC;