Rechercher dans le manuel MySQL
13.7.3.4 OPTIMIZE TABLE Syntax
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 the
storage engine used
by that table.
Use OPTIMIZE TABLE
in these
cases, depending on the type of table:
After doing substantial insert, update, or delete operations on an
InnoDB
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 a
FULLTEXT
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 a
MyISAM
orARCHIVE
table, or making many changes to aMyISAM
orARCHIVE
table 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 requires SELECT
and INSERT
privileges for the
table.
OPTIMIZE TABLE
works for
InnoDB
,
MyISAM
, and
ARCHIVE
tables.
OPTIMIZE TABLE
is also supported
for dynamic columns of in-memory
NDB
tables. It does not work for
fixed-width columns of in-memory tables, nor does it work for
Disk Data tables. The performance of OPTIMIZE
on NDB Cluster tables can be tuned using
--ndb-optimization-delay
, which
controls the length of time to wait between processing batches
of rows by OPTIMIZE TABLE
. For
more information, see
Previous NDB Cluster Issues Resolved in NDB Cluster 7.3.
For NDB Cluster tables, OPTIMIZE
TABLE
can be interrupted by (for example) killing the
SQL thread performing the OPTIMIZE
operation.
By default, OPTIMIZE TABLE
does
not work for tables created using any other
storage engine and returns a result indicating this lack of
support. You can make OPTIMIZE
TABLE
work for other storage engines by starting
mysqld with the --skip-new
option. In this case, OPTIMIZE
TABLE
is just mapped to ALTER
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, see
Section 23.3.4, “Maintenance of Partitions”.
By default, the server writes OPTIMIZE
TABLE
statements to the binary log so that they
replicate to replication slaves. To suppress logging, specify
the optional NO_WRITE_TO_BINLOG
keyword or
its alias LOCAL
.
OPTIMIZE TABLE Output
OPTIMIZE TABLE
returns a result
set with the columns shown in the following table.
Column | Value |
---|---|
Table |
The table name |
Op |
Always optimize |
Msg_type |
status , error ,
info , note , or
warning |
Msg_text |
An 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
.MYD
or .MYI
file is
different from the user ID of the mysqld
process, OPTIMIZE TABLE
generates a "cannot change ownership of the file" error unless
mysqld is started by the
root
user.
For InnoDB
tables,
OPTIMIZE TABLE
is mapped to
ALTER TABLE ...
FORCE
, which rebuilds the table to update index
statistics and free unused space in the clustered index. This
is displayed in the output of OPTIMIZE
TABLE
when you run it on an
InnoDB
table, as shown here:
- +----------+----------+----------+-------------------------------------------------------------------+
- +----------+----------+----------+-------------------------------------------------------------------+
- +----------+----------+----------+-------------------------------------------------------------------+
OPTIMIZE TABLE
uses
online DDL for
regular and partitioned InnoDB
tables,
which reduces downtime for concurrent DML operations. The
table rebuild triggered by OPTIMIZE
TABLE
and performed under the cover by
ALTER TABLE ...
FORCE
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:
When the
old_alter_table
system variable is enabled.When the mysqld
--skip-new
option is enabled.
OPTIMIZE TABLE
using
online DDL is not
supported for InnoDB
tables that contain
FULLTEXT
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 as
MyISAM
) will. When considering whether or
not to run optimize, consider the workload of transactions
that your server will process:
Some level of fragmentation is expected.
InnoDB
only fills pages 93% full, to leave room for updates without having to split pages.Delete operations might leave gaps that leave pages less filled than desired, which could make it worthwhile to optimize the table.
Updates to rows usually rewrite the data within the same page, depending on the data type and row format, when sufficient space is available. See Section 15.9.1.5, “How Compression Works for InnoDB Tables” and Section 15.10, “InnoDB Row Formats”.
High-concurrency workloads might leave gaps in indexes over time, as
InnoDB
retains multiple versions of the same data due through its MVCC mechanism. See Section 15.3, “InnoDB Multi-Versioning”.
For MyISAM
tables,
OPTIMIZE TABLE
works as
follows:
If the table has deleted or split rows, repair the table.
If the index pages are not sorted, sort them.
If the table's statistics are not up to date (and the repair could not be accomplished by sorting the index), update them.
OPTIMIZE TABLE
is performed
online for regular and partitioned InnoDB
tables. Otherwise, MySQL locks
the table during the time OPTIMIZE
TABLE
is running.
OPTIMIZE TABLE
does not sort
R-tree indexes, such as spatial indexes on
POINT
columns. (Bug #23578)
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-optimize-table.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.