OPTIMIZE TABLE

Use this MySQL 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)

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

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

Explanation

Use this MySQL statement to optimize the data contained in a MySQL table. Optimization is useful when many rows have been deleted from a table in MySQL. It's also useful to run this statement periodically with a MySQL 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.

This MySQL 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.

Examples

Here is an example of this MySQL 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 in MySQL were optimized successfully and the activity was not written to the binary log file.