BANCO-DE-DADOS
How to Check Allocated Space in a MySQL Database
Learn how to use SQL queries to check allocated space and free space in a MySQL database, allowing better management of your system.
In this post I will show some queries I use to know the allocated space in the MySQL database. I usually use queries in the database administrator PHPMyadmin As these are simple queries, I believe you should not have problems if you use SSH or software on your local machine.
Simple queries
At the beginning we use SELECT table_schema to select the database in our database base.
The SUM command sums the storage values data_length + index_length.
The next SUM takes the sum of data_free to display what we still have available space in the DB.
We select this data from the tables with FROM information_schema.TABLES and group by DB with GROUP BY table_schema
data_length - store the actual data.
index_length - store the table index.
SELECT table_schema "Database Name",
sum( data_length + index_length ) / (1024 * 1024) "Size in MB",
sum( data_free )/ (1024 * 1024) "Free Space in MB"
FROM information_schema.TABLES
GROUP BY table_schema;
Optimize display
SELECT table_schema "Database Name",
SUM( ROUND( (data_length + index_length) / ( 1024 *1024 ) , 2 ) ) "Size in MB",
SUM( ROUND( (data_free) / ( 1024 *1024 ) , 2 ) ) "Free Space in MB"
FROM information_schema.TABLES
GROUP BY table_schema;
Maybe you want to view specifically the size of the tables within the DB.
In this example the value eduardolira received by table_schema is the name of my database.
The cool thing is that now we can view in the columns what we summed previously, using data_length + index_length.
SELECT TABLE_NAME, table_rows, data_length, index_length,
ROUND( (data_length + index_length) / (1024 * 1024), 2) "Size in MB"
FROM information_schema.TABLES
WHERE table_schema = "eduardolira";
Optimizing more:
SELECT concat( table_schema, '.', table_name ) TableName,
concat( round( data_length / ( 1024 * 1024 ) , 2 ) , 'MB' ) data_length,
concat( round( index_length / ( 1024 * 1024 ) , 2 ) , 'MB' ) index_length,
concat( round( round( data_length + index_length ) / ( 1024 * 1024 ) , 2 ) , 'MB' ) Total
FROM information_schema.TABLES
ORDER BY data_length DESC;
Through the links below you can find other useful queries.
References -> https://dev.mysql.com/doc/refman/8.4/en/information-schema-tables-table.html https://dev.mysql.com/doc/refman/8.4/en/show-table-status.html
Thx!
Revisited in November 2026
I fixed the third query, the per-table size one. It divided by 1024 and then multiplied by 1024 (/1024 * 1024), so
the result came out in bytes, not MB. It now divides by (1024 * 1024). I also swapped the documentation links, which
pointed to MySQL 5.1, for the 8.4 ones.
And two notes on what the columns mean:
data_freeisn’t the space left for the database to grow. It’s disk space already reserved but unused inside the table files, usually after many deletions. The database’s limit is set by the disk or the hosting quota.- For InnoDB tables,
table_rowsis an estimate. For the exact number, onlySELECT COUNT(*)will do.
Who writes this
Eduardo Lira
Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.