Guide Databases & MySQL

How to optimise a MySQL database

OPTIMIZE TABLE reclaims space and refreshes statistics — but it behaves differently depending on the storage engine underneath.

Updated 7 min read Intermediate

A database that has had a lot of rows deleted or updated over time can end up larger on disk than the data it actually holds, and its query planner can end up working from statistics that no longer reflect reality. Optimising a database addresses both: it reclaims the wasted space and refreshes the numbers MySQL uses to decide how to run a query.

What OPTIMIZE TABLE actually does

OPTIMIZE TABLE table_name;

Run through the SQL tab in phpMyAdmin, or by ticking a table in the database view and choosing Optimize table from the "With selected" dropdown. What happens underneath depends on the storage engine:

EngineWhat OPTIMIZE TABLE does
MyISAMDefragments the table file directly, reclaiming space left by deleted rows and rebuilding the index.
InnoDBEffectively rebuilds the table using a copy — the same mechanism as ALTER TABLE ... ENGINE=InnoDB — which also reclaims space, but does so by copying the whole table rather than defragmenting in place.
On a large InnoDB table, this takes time and locks the table

Because InnoDB's version of this operation copies the entire table, running it on a large, actively used table can take a noticeable amount of time and will affect performance while it runs. Do this during a quiet period for anything of significant size, not in the middle of your busiest traffic of the day.

Refreshing statistics with ANALYZE TABLE

Separately from reclaiming space, MySQL keeps internal statistics about the distribution of values in a table's indexes, and uses those statistics to decide how to run a query efficiently. Statistics that have gone stale — usually after a lot of inserts, updates or deletes — can lead the query planner to make a worse decision than it would with current numbers:

ANALYZE TABLE table_name;

This is quick, non-destructive, and worth running on a table that has recently grown or changed a lot, even outside of a full optimise.

Adding the indexes that actually help

Optimising existing structures only gets you so far if the underlying queries have no index to use in the first place. If you have not already worked through finding the query slowing your site down, that is the more targeted next step — indexing the specific columns a slow query filters or sorts by usually produces a bigger improvement than any amount of optimising the table's existing structure.

When it is worth doing

  • After a large deletion. Clearing out years of spam comments, expired sessions, or old log entries leaves exactly the kind of gaps OPTIMIZE TABLE reclaims.
  • Periodically on a busy, long-running site. A WordPress install that is a few years old and has never had this done is a reasonable candidate, particularly on tables like wp_postmeta or a logging table that grows continuously.
  • After a repair. A table just recovered with REPAIR TABLE often benefits from an optimise afterwards — see repairing a corrupted MySQL table.

It is not something that needs to run on a schedule for most sites. A database that is not visibly bloated and is not showing signs of slow queries does not need routine optimising just for its own sake.

Optimising every table at once

phpMyAdmin lets you select every table in a database at once rather than one at a time — open the database, tick the checkbox at the top of the table list to select all of them, then choose Optimize table from the dropdown. This is convenient for a periodic tidy-up, but be aware that it runs the operation described above against every table in turn, including any large InnoDB tables, so the same warning about time and locking applies multiplied across however many tables you select. On an account with one very large table and several small ones, it is often better to target the large one specifically during a quiet period rather than running the whole database through it at once during business hours.

Checking whether it actually helped

Compare the table's reported size before and after using a query against information_schema:

SELECT table_name AS "Table",
       ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
FROM information_schema.TABLES
WHERE table_schema = "your_database_name"
ORDER BY (data_length + index_length) DESC;

Our guide to checking how big your database is covers this query and how to read the result in more detail, including the difference between data and index size that this query reports separately.

Back up before optimising anything you cannot easily recreate

OPTIMIZE TABLE is a routine, low-risk operation, but it does rewrite the table's underlying storage. If the data is not otherwise backed up and cannot be easily regenerated, exporting it first costs little and removes any doubt.

Related reading