Name

OPTIMIZE TABLE

Synopsis

OPTIMIZE [LOCAL|NO_WRITE_TO_BINLOG] TABLE table[,  . . . ]

Use this statement to optimize the data contained in a table. Optimization is useful when many rows have been deleted from a table. It’s also useful to run this statement periodically with a table that contains several variable-character-width columns (i.e., VARCHAR, BLOB, and TEXT columns). This statement generally works only with MyISAM, BDB, and InnoDB tables. It may work on other tables, however, if the mysqld daemon is started with the --skip-new option or the --safe-mode option. See Chapter 10 for more information on setting server startup options.

This statement will repair some row problems and sort indexes. It will temporarily lock the tables involved while optimizing. Multiple tables can be listed for optimization in a comma-separated list. To prevent the activities of this statement from being recorded in the binary log file, use the NO_WRITE_TO_BINLOG flag or its alias, LOCAL. Here is an example of its use:

OPTIMIZE LOCAL TABLE workreq, clients;
+----------------------+----------+----------+----------+
| Table                | Op       | Msg_type | Msg_text |
+----------------------+----------+----------+----------+
| workrequests.workreq | optimize | status   | OK       |
| workrequests.clients | optimize | status   | OK       |
+----------------------+----------+----------+----------+

Here two tables were optimized successfully and the activity was not written to the binary log file.

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.