如何检查MySQL数据库和表大小

时间:2019-05-19 01:26:12  来源:igfitidea点击:

MySQL是一种关系数据库管理系统,广泛用作Linux系统的数据库系统。
本文将通过SQL查询计算MySQL或MariaDB服务器中的表和数据库的大小。
MySQL将所有与表相关的信息存储在information_schema数据库中。
我们将使用information_schema表来查找表和数据库的大小。

检查MySQL中单个数据库的大小

这个查询将计算MySQL服务器中单个数据库的大小。

SELECT table_schema "Database Name", SUM( data_length + index_length)/1024/1024
"Database Size (MB)" FROM information_schema.TABLES where table_schema = '  **mydb**  ';

+---------------+--------------------+
| Database Name | Database Size (MB) |
+---------------+--------------------+
| mydb          |         0.15625000 |
+---------------+--------------------+
1 row in set (0.04 sec)

检查MySQL中所有数据库的大小

这个查询将计算mysql服务器中所有数据库的大小。

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

+--------------------+--------------------+
| Database Name      | Database Size (MB) |
+--------------------+--------------------+
| demodb             |         0.15625000 |
| information_schema |         0.00976563 |
| mydb               |         0.15625000 |
| mysql              |         0.81098557 |
| performance_schema |         0.00000000 |
+--------------------+--------------------+
5 rows in set (0.01 sec)

检查MySQL数据库中单个表的大小

这个查询将计算mysql服务器中数据库中单个表的大小。

SELECT table_name "Table Name", table_rows "Rows Count", round(((data_length + index_length)/1024/1024),2)
"Table Size (MB)" FROM information_schema.TABLES WHERE table_schema = "  **mydb**  " AND table_name ="  **table_one**  ";

+---------------------+------------+-----------------+
| Table Name          | Rows Count | Table Size (MB) |
+---------------------+------------+-----------------+
| archive_one         |          8 |            0.09 |
+---------------------+------------+-----------------+
1 row in set (0.00 sec)

检查MySQL数据库中所有表的大小

这个查询将计算mysql服务器中数据库中所有表的大小。
请用实际数据库名称更改“ mydb”。
它还将列出每个表中的行数。

SELECT table_name "Table Name", table_rows "Rows Count", round(((data_length + index_length)/1024/1024),2)
"Table Size (MB)" FROM information_schema.TABLES WHERE table_schema = "  **mydb**  ";

+----------------------+------------+-----------------+
| Table Name           | Rows Count | Table Size (MB) |
+----------------------+------------+-----------------+
| table_one  	       |          8 |            0.09 |
| table_two 	       |          0 |            0.02 |
| table_three          |          0 |            0.02 |
| table_four           |        174 |            0.03 |
+----------------------+------------+-----------------+
4 rows in set (0.00 sec)