Get the Size of a Database in MariaDB

In MariaDB, we can query the information_schema.tables table to check the size of a database.

This table returns information about the tables and views in each database on the server. We can group the results and return the aggregate amounts for each database.

Example

Here’s a query that returns the size of all databases on the server:

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;

Result:

+--------------------+---------------+-------------+
| 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 |
+--------------------+---------------+-------------+

The information_schema.tables table shows information about the various non-TEMPORARY tables (except tables from the Information Schema database) and views on the server.

Here, I grouped them by database (table_schema) and ran some calculations on the sizes.

Single Database

We can narrow it down to just one database with a WHERE clause:

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;

Result:

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

In MySQL, we can use the sys.FORMAT_BYTES() function to do the data length conversion, but as of this writing, MariaDB has not yet implemented such a function.