Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 35.1Mb
PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
Excerpts from this Manual

13.7.2.4 OPTIMIZE TABLE Statement

OPTIMIZE [NO_WRITE_TO_BINLOG | LOCAL]    TABLEtbl_name [,tbl_name] ...

OPTIMIZE TABLE reorganizes the physical storage of table data and associated index data, to reduce storage space and improve I/O efficiency when accessing the table. The exact changes made to each table depend on thestorage engine used by that table.

UseOPTIMIZE TABLE in these cases, depending on the type of table:

  • After doing substantial insert, update, or delete operations on anInnoDB table that has its own.ibd file because it was created with theinnodb_file_per_table option enabled. The table and indexes are reorganized, and disk space can be reclaimed for use by the operating system.

  • After doing substantial insert, update, or delete operations on columns that are part of aFULLTEXT index in anInnoDB table. Set the configuration optioninnodb_optimize_fulltext_only=1 first. To keep the index maintenance period to a reasonable time, set theinnodb_ft_num_word_optimize option to specify how many words to update in the search index, and run a sequence ofOPTIMIZE TABLE statements until the search index is fully updated.

  • After deleting a large part of aMyISAM orARCHIVE table, or making many changes to aMyISAM orARCHIVEtable with variable-length rows (tables that haveVARCHAR,VARBINARY,BLOB, orTEXT columns). Deleted rows are maintained in a linked list and subsequentINSERT operations reuse old row positions. You can useOPTIMIZE TABLE to reclaim the unused space and to defragment the data file. After extensive changes to a table, this statement may also improve performance of statements that use the table, sometimes significantly.

This statement requiresSELECT andINSERT privileges for the table.

OPTIMIZE TABLE works forInnoDB,MyISAM, andARCHIVE tables.OPTIMIZE TABLE is also supported for dynamic columns of in-memoryNDB tables. It does not work for fixed-width columns of in-memory tables, nor does it work for Disk Data tables. The performance ofOPTIMIZE on NDB Cluster tables can be tuned using--ndb-optimization-delay, which controls the length of time to wait between processing batches of rows byOPTIMIZE TABLE. For more information, seePrevious NDB Cluster Issues Resolved in NDB Cluster 8.0.

For NDB Cluster tables,OPTIMIZE TABLE can be interrupted by (for example) killing the SQL thread performing theOPTIMIZE operation.

By default,OPTIMIZE TABLE doesnot work for tables created using any other storage engine and returns a result indicating this lack of support. You can makeOPTIMIZE TABLE work for other storage engines by startingmysqld with the--skip-new option. In this case,OPTIMIZE TABLE is just mapped toALTER TABLE.

This statement does not work with views.

OPTIMIZE TABLE is supported for partitioned tables. For information about using this statement with partitioned tables and table partitions, seeSection 22.3.4, “Maintenance of Partitions”.

By default, the server writesOPTIMIZE TABLE statements to the binary log so that they replicate to replicas. To suppress logging, specify the optionalNO_WRITE_TO_BINLOG keyword or its aliasLOCAL.

OPTIMIZE TABLE Output

OPTIMIZE TABLE returns a result set with the columns shown in the following table.

ColumnValue
TableThe table name
OpAlwaysoptimize
Msg_typestatus,error,info,note, orwarning
Msg_textAn informational message

OPTIMIZE TABLE table catches and throws any errors that occur while copying table statistics from the old file to the newly created file. For example. if the user ID of the owner of the.frm,.MYD, or.MYI file is different from the user ID of themysqld process,OPTIMIZE TABLE generates a "cannot change ownership of the file" error unlessmysqld is started by theroot user.

InnoDB Details

ForInnoDB tables,OPTIMIZE TABLE is mapped toALTER TABLE ... FORCE, which rebuilds the table to update index statistics and free unused space in the clustered index. This is displayed in the output ofOPTIMIZE TABLE when you run it on anInnoDB table, as shown here:

mysql> OPTIMIZE TABLE foo;+----------+----------+----------+-------------------------------------------------------------------+| Table    | Op       | Msg_type | Msg_text                                                          |+----------+----------+----------+-------------------------------------------------------------------+| test.foo | optimize | note     | Table does not support optimize, doing recreate + analyze instead || test.foo | optimize | status   | OK                                                                |+----------+----------+----------+-------------------------------------------------------------------+

OPTIMIZE TABLE usesonline DDL for regular and partitionedInnoDB tables, which reduces downtime for concurrent DML operations. The table rebuild triggered byOPTIMIZE TABLE is completed in place. An exclusive table lock is only taken briefly during the prepare phase and the commit phase of the operation. During the prepare phase, metadata is updated and an intermediate table is created. During the commit phase, table metadata changes are committed.

OPTIMIZE TABLE rebuilds the table using the table copy method under the following conditions:

OPTIMIZE TABLE usingonline DDL is not supported forInnoDB tables that containFULLTEXT indexes. The table copy method is used instead.

InnoDB stores data using a page-allocation method and does not suffer from fragmentation in the same way that legacy storage engines (such asMyISAM) do. When considering whether or not to runOPTIMIZE TABLE, consider the workload of transactions that your server is expected to process:

MyISAM Details

ForMyISAM tables,OPTIMIZE TABLE works as follows:

  1. If the table has deleted or split rows, repair the table.

  2. If the index pages are not sorted, sort them.

  3. If the table's statistics are not up to date (and the repair could not be accomplished by sorting the index), update them.

Other Considerations

OPTIMIZE TABLE is performed online for regular and partitionedInnoDB tables. Otherwise, MySQLlocks the table during the timeOPTIMIZE TABLE is running.

OPTIMIZE TABLE does not sort R-tree indexes, such as spatial indexes onPOINT columns. (Bug #23578)