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 15 for more information on setting server startup options.

This statement also repairs some row problems and sort indexes. It temporarily locks 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 keyword or its alias, LOCAL. Here is an example of the statement’s 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 are optimized successfully and the activity is not written to the binary log file.

Get MySQL in a Nutshell, 2nd Edition 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.