在 MariaDB 中获取数据库的大小

发布于

在MariaDB中,我们可以查询表来检查数据库的大小。information_schema.tables

此表返回有关服务器上每个数据库中的表和视图的信息。我们可以对结果进行分组并返回每个数据库的总金额。

示例

下面是返回服务器上所有数据库大小的查询:

SELECT 
    table_schema 'Database Name', 
    SUM(data_length + index_length) 'Size in Bytes', 
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) 'Size in MiB' 
FROM information_schema.tables 
GROUP BY table_schema;

输出:

+--------------------+---------------+-------------+
| Database Name      | Size in Bytes | Size in MiB |
+--------------------+---------------+-------------+
| information_schema |        212992 |        0.20 |
| KrankyKranes       |        131072 |        0.13 |
| Music              |         81920 |        0.08 |
| MyDB               |         32768 |        0.03 |
| mysql              |       4972544 |        4.74 |
| performance_schema |             0 |        0.00 |
| PetHouse           |         81920 |        0.08 |
| Zap                |         37460 |        0.04 |
+--------------------+---------------+-------------+

该表显示有关服务器上各种非表(数据库中的表除外)和视图的信息。information_schema.tables``TEMPORARY``Information Schema

在这里,我按数据库()对它们进行了分组,并对大小进行了一些计算。table_schema

单一数据库

我们可以用子句将其缩小到只有一个数据库:WHERE

SELECT 
    table_schema 'Database Name', 
    SUM(data_length + index_length) 'Size in Bytes', 
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) 'Size in MiB' 
FROM information_schema.tables
WHERE table_schema = 'KrankyKranes'
GROUP BY table_schema;

输出:

+---------------+---------------+-------------+
| Database Name | Size in Bytes | Size in MiB |
+---------------+---------------+-------------+
| krankykranes  |        131072 |        0.13 |
+---------------+---------------+-------------+

在 MySQL 中,我们可以使用该函数进行数据长度转换,但在撰写本文时,MariaDB 尚未实现这样的函数。sys.FORMAT_BYTES()