In MySQL, you can query the information_schema.tables
table to return information about the tables in a database. This table includes information about the data length, index length, as well as other details such as collation, creation time, etc. You can use the information in this table to find the size of a given database or all databases on the server.
You can also use the MySQL Workbench GUI to find details about the database (including its size).
This article provides a quick overview of both methods.
Code Example
Here’s an example of finding the size of each database by running a query against the information_schema.tables
table:
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 | 0 | 0.00 | | Music | 98304 | 0.09 | | mysql | 2506752 | 2.39 | | performance_schema | 0 | 0.00 | | sakila | 6766592 | 6.45 | | Solutions | 16384 | 0.02 | | sys | 16384 | 0.02 | | world | 802816 | 0.77 | +--------------------+---------------+-------------+
In this example I’ve listed the size in bytes and in mebibytes (MiB), but you can choose how you want to present it.
Of course, you can always narrow it down to a specific database if you need to. Simply add a WHERE
clause with the name of the database:
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 = 'sakila';
Result:
+---------------+---------------+-------------+ | Database Name | Size in Bytes | Size in MiB | +---------------+---------------+-------------+ | sakila | 6766592 | 6.45 | +---------------+---------------+-------------+
The FORMAT_BYTES() Function
You can use the FORMAT_BYTES()
function to save yourself converting the size into mebibytes, kibibytes, or whatever. This function takes a value, converts it to human-readable format and returns a string consisting of a value and a units indicator. The converted value will depend on the size of the value (so the result could be in bytes
, KiB
(kibibytes), MiB
(mebibytes), GiB
(gibibytes), TiB
(tebibytes), PiB
(pebibytes), or EiB
(exbibytes).
Here’s an example of rewriting the previous example to use the FORMAT_BYTES()
function:
SELECT table_schema 'Database Name', SUM(data_length + index_length) 'Size in Bytes', FORMAT_BYTES(SUM(data_length + index_length)) 'Size (Formatted)' FROM information_schema.tables GROUP BY table_schema;
Result:
+--------------------+---------------+------------------+ | Database Name | Size in Bytes | Size (Formatted) | +--------------------+---------------+------------------+ | information_schema | 0 | 0 bytes | | Music | 98304 | 96.00 KiB | | mysql | 2506752 | 2.39 MiB | | performance_schema | 0 | 0 bytes | | sakila | 6766592 | 6.45 MiB | | Solutions | 16384 | 16.00 KiB | | sys | 16384 | 16.00 KiB | | world | 802816 | 784.00 KiB | +--------------------+---------------+------------------+
MySQL Workbench
Another way of finding the database size is to use the MySQL Workbench GUI. Here’s how:
- Navigate to the database in the
Schemas
pane - Hover over the applicable database
- Click the little information icon beside the database name. This loads information about the database, including its approximate size, table count, collation, etc. The database size is listed on the
Info
tab (usually the default tab).