How to Check Database Size in MySQL Quickly
To check the size of a MySQL database, use the
information_schema database and query the TABLES table to sum data_length and index_length. This shows the total size in bytes for the specified database.Syntax
Use this SQL query to find the size of a database in MySQL:
TABLE_SCHEMA: The name of your database.data_length: Size of the data stored.index_length: Size of the indexes.- The sum of
data_lengthandindex_lengthgives the total size in bytes.
sql
SELECT table_schema AS 'Database', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)' FROM information_schema.TABLES WHERE table_schema = 'your_database_name' GROUP BY table_schema;
Example
This example shows how to check the size of a database named shop. It sums the data and index sizes and converts the result to megabytes (MB) rounded to two decimals.
sql
SELECT table_schema AS 'Database', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)' FROM information_schema.TABLES WHERE table_schema = 'shop' GROUP BY table_schema;
Output
Database | Size (MB)
shop | 12.34
Common Pitfalls
Common mistakes when checking database size include:
- Forgetting to replace
'your_database_name'with the actual database name. - Not summing both
data_lengthandindex_length, which underestimates size. - Confusing table size with database size by querying individual tables only.
sql
/* Wrong: Only data_length, missing index size */ SELECT table_schema AS 'Database', ROUND(SUM(data_length) / 1024 / 1024, 2) AS 'Size (MB)' FROM information_schema.TABLES WHERE table_schema = 'shop' GROUP BY table_schema; /* Correct: Sum data_length and index_length */ SELECT table_schema AS 'Database', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)' FROM information_schema.TABLES WHERE table_schema = 'shop' GROUP BY table_schema;
Quick Reference
| Term | Description |
|---|---|
| TABLE_SCHEMA | Name of the database |
| data_length | Size of table data in bytes |
| index_length | Size of table indexes in bytes |
| SUM(data_length + index_length) | Total size of database in bytes |
| ROUND(... / 1024 / 1024, 2) | Convert bytes to megabytes with 2 decimals |
Key Takeaways
Use information_schema.TABLES to get database size in MySQL.
Sum data_length and index_length for accurate size measurement.
Convert bytes to megabytes by dividing by 1024 twice.
Always specify the correct database name in the query.
Check both data and index sizes to avoid underestimating.