Database Administration Language Reference
OPTIMIZE TABLE Syntax
OPTIMIZE TABLE tbl_name[,tbl_name]...
OPTIMIZE TABLE should be used if you have deleted a large part of a table or if you have made many changes to a table with variable-length rows (tables that have VARCHAR, BLOB, or TEXT columns). Deleted records are maintained in a linked list and subsequent INSERT operations reuse old record positions. You can use OPTIMIZE TABLE to reclaim the unused space and to defragment the data file.
For the moment, OPTIMIZE TABLE only works on MyISAM and BDB tables. For BDB tables, OPTIMIZE TABLE is currently mapped to ANALYZE TABLE. See Section 4.5.2.
You can get OPTIMIZE TABLE to work on other table types by starting mysqld with --skip-new or --safe-mode, but in this case OPTIMIZE TABLE is just mapped to ALTER TABLE.
OPTIMIZE TABLE works the following way:
If the table has deleted or split rows, it repairs the table.
If the index pages are not sorted, it sorts them.
If the statistics are not up to date (and the repair couldn’t be done by sorting the index), it updates them.
OPTIMIZE TABLE for a MyISAM table is equivalent to running myisamchk --quick --check-only-changed --sort-index --analyze on the table.
Note that the table is locked during the time OPTIMIZE TABLE is running!
ANALYZE TABLE Syntax
ANALYZE TABLE tbl_name[,tbl_name...]
ANALYZE TABLE analyses and stores the key distribution for the table. During the analysis, the table is locked with a read lock. This works on MyISAM and BDB tables. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access