MySQL – Calculating Disk Usage Per Database

The answer usually lies within /var/lib/mysql/[DATABASE]. But the answer can also be found in, you guessed it, information_schema! 😀

Here’s an one-liner for you, that displays database sizes in megabytes (nice long float() included)

select table_schema, sum((data_length+index_length)/1024/1024) AS MB from information_schema.tables group by 1;

If gigabytes are more your thing, run the following:

select table_schema, sum((data_length+index_length)/1024/1024/1024) AS GB from information_schema.tables group by 1;

Example output:

+--------------------+-------------+
| table_schema       | MB          |
+--------------------+-------------+
| information_schema |  0.20312500 |
| mysql              |  2.93750000 |
| sys                |  0.03125000 |
| wordpress          |  6.37047864 |
+--------------------+-------------+

Query on 👾

Leave a Reply

Your email address will not be published. Required fields are marked *