CertGrid CertGrid
Hands-on Lab·MySQL

MySQL Fragmentation and OPTIMIZE TABLE

A third of the rows deleted and the table is exactly as large as before - 20.5 MB either way. Deleting rows does not return space, and OPTIMIZE TABLE rebuilding the table is what finally does.

Monitoring and Operations Guide 43 of 45 Intermediate

OPTIMIZE TABLE on InnoDB is not an optimisation in the MyISAM sense - it rebuilds the table. Since 5.6 it is online for most cases, but it still copies the data and needs room to do it.

Every command on this page ran on db-a01.
Server NameIP AddressOSRolesCPURAMHDD
db-a01192.168.0.81Ubuntu 26.04 LTSPrimary / Source / Replica Set Member 12 Core4 GB50 GB

This guide includes

Use this when deleting a third of a table frees no space at all. This matters because InnoDB keeps the pages - the file stays exactly as large as it was, and only a rebuild returns the space.

Before you start

  1. Delete a third of the rows

  2. Rebuild the table

Official sources