Skip to content

Largest tables and their free space

Data, index and reclaimable space per table. A large data_free is fragmentation an OPTIMIZE TABLE would recover.

MySQL · SQL
SELECT CONCAT(table_schema, '.', table_name) AS table_name,       table_rows AS estimated_rows,       ROUND((data_length + index_length) / 1048576, 1) AS total_mb,       ROUND(data_length / 1048576, 1) AS data_mb,       ROUND(index_length / 1048576, 1) AS index_mb,       ROUND(data_free / 1048576, 1) AS free_mbFROM information_schema.tablesWHERE table_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')  AND table_type = 'BASE TABLE'ORDER BY (data_length + index_length) DESCLIMIT 25

This query is part of the "MySQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What the query shows

This query lists the 25 largest base tables on the server, ordered by data size plus index size. System schemas (mysql, information_schema, performance_schema and sys) and views are left out.

Run it when the disk is filling up, before you plan a backup window or a migration, or when one table seems to grow faster than the business does. It reads information_schema.tables, so it is cheap and does not scan your data.

How to read each column

  • table_name: schema and table, joined with a dot.
  • estimated_rows: the table_rows value. For InnoDB it is an estimate, not a count.
  • total_mb: data plus index, in MB. This is the number the list is sorted by.
  • data_mb: for InnoDB, the size of the clustered index, which holds the rows.
  • index_mb: the size of all secondary indexes together.
  • free_mb: the data_free value, space inside the tablespace that is allocated but not used.

What is normal and what is not

There is no universal size limit. What matters is how the numbers compare with each other and with last month. These are rules of thumb, not MySQL limits:

  • index_mb larger than data_mb usually means too many or too wide indexes. Check them with the unused indexes query.
  • free_mb above 20 to 30 percent of total_mb on a large table points to fragmentation after many deletes or updates.
  • One table that is most of the server is a candidate for archiving, partitioning or a retention job.

What to do about it

To recover fragmented space, rebuild the table. On InnoDB, OPTIMIZE TABLE is mapped to ALTER TABLE ... FORCE plus ANALYZE TABLE. It runs as an online operation, so reads and writes continue, but it takes a short exclusive metadata lock at the start and at the end. A long transaction on the table can make it wait. It also needs free disk space close to the size of the table while it works.

-- Rebuild one table and refresh its statistics.
-- NO_WRITE_TO_BINLOG keeps it from running on replicas too.
OPTIMIZE NO_WRITE_TO_BINLOG TABLE shop.orders;

Rebuilding only returns space to the operating system when the table has its own tablespace. That is the default (innodb_file_per_table = ON). If you want the real file size, compare with the .ibd file on disk.

If the table is large because it is full of old rows, no rebuild will help. Delete or archive in small batches, then rebuild.

Limits of this query

  • InnoDB row counts in table_rows can be off by 40 to 50 percent. Use SELECT COUNT(*) when you need the exact number.
  • MySQL 8.0 caches table statistics for information_schema for 24 hours by default (information_schema_stats_expiry). Run ANALYZE TABLE, or set the variable to 0 in your session, to see fresh numbers.
  • For tables in a shared tablespace, data_free reports the free space of the whole shared tablespace, not of the table.
  • The user only sees the tables it has some privilege on. A low-privilege account will show a shorter list.
  • MariaDB has the same columns. It does not cache them the way MySQL 8.0 does.

Run it every week in DataRunner

Table growth is easy to miss because it happens a little every day. In DataRunner you can save this query, pick a weekly schedule and a time zone, and receive the result as CSV, Excel, PDF or HTML by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets. Comparing this week with the last one tells you which table is growing.

Frequently asked questions

How do I check the size of a table in MySQL?
Query information_schema.tables and add data_length and index_length. Divide by 1048576 to get MB. The query on this page does exactly that for the 25 largest tables and also shows data_free.
Why is my MySQL table bigger than the number of rows suggests?
Indexes, row overhead and free space inside the tablespace all count. After many deletes the file keeps its size and data_free grows. Rebuilding the table with OPTIMIZE TABLE recovers it when the table has its own tablespace.
Does OPTIMIZE TABLE lock the table in MySQL?
On InnoDB it runs online, so reads and writes continue. It takes a brief exclusive metadata lock at the start and at the end, and it needs disk space for a copy of the table. Run it in a quiet period and check for long open transactions first.
Is information_schema.tables accurate for InnoDB?
Sizes are close, but table_rows is an estimate. In MySQL 8.0 the values are also cached for 24 hours unless you run ANALYZE TABLE or lower information_schema_stats_expiry.