如何检查数据库各个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;
    原文作者:耀凯考前突击大师
    原文地址: https://www.jianshu.com/p/2b67dfa3fc9a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞