Documentation Home
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 32.3Mb
PDF (A4) - 32.3Mb
PDF (RPM) - 30.4Mb
HTML Download (TGZ) - 7.8Mb
HTML Download (Zip) - 7.8Mb
HTML Download (RPM) - 6.7Mb
Man Pages (TGZ) - 142.5Kb
Man Pages (Zip) - 201.7Kb
Info (Gzip) - 3.0Mb
Info (Zip) - 3.0Mb


Pre-General Availability Draft: 2017-05-29

14.7.2.1 ANALYZE TABLE Syntax

ANALYZE [NO_WRITE_TO_BINLOG | LOCAL] TABLE
    tbl_name [, tbl_name] ...

ANALYZE TABLE analyzes and stores the key distribution for a table. During the analysis, the table is locked with a read lock for InnoDB and MyISAM. This statement works with InnoDB, NDB, and MyISAM tables. For MyISAM tables, this statement is equivalent to using myisamchk --analyze. This statement does not work with views.

Note

If the innodb_read_only system variable is enabled, ANALYZE TABLE may update the table itself (for example, if it is a MyISAM table) but still fail because it cannot update the dynamic statistics tables in the data dictionary, which use InnoDB. To obtain the updated statistics, set information_schema_stats=LATEST.

For more information on how the analysis works within InnoDB, see Section 16.6.11.1, “Configuring Persistent Optimizer Statistics Parameters” and Section 16.6.11.3, “Estimating ANALYZE TABLE Complexity for InnoDB Tables”. Also see Section 16.8.1.7, “Limits on InnoDB Tables”. In particular, when you enable the innodb_stats_persistent option, you must run ANALYZE TABLE after loading substantial data into an InnoDB table, or creating a new index for one.

MySQL uses the stored key distribution to decide the order in which tables should be joined when you perform a join on something other than a constant. In addition, key distributions can be used when deciding which indexes to use for a specific table within a query.

This statement requires SELECT and INSERT privileges for the table.

ANALYZE TABLE is supported for partitioned tables, and you can use ALTER TABLE ... ANALYZE PARTITION to analyze one or more partitions; for more information, see Section 14.1.7, “ALTER TABLE Syntax”, and Section 21.3.4, “Maintenance of Partitions”.

ANALYZE TABLE returns a result set with the following columns.

ColumnValue
TableThe table name
OpAlways analyze
Msg_typestatus, error, info, note, or warning
Msg_textAn informational message

You can check the stored key distribution with the SHOW INDEX statement. See Section 14.7.5.22, “SHOW INDEX Syntax”.

If the table has not changed since the last ANALYZE TABLE statement, the table is not analyzed again.

By default, the server writes ANALYZE 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.


User Comments
  Posted by James Day on July 26, 2004
Use mysqlcheck -Aa -uroot -p to run analyze table for all databases and tables (including InnoDB) on a running server. Available in MySQL 3.23.38 and later.
  Posted by Rob Blick on April 27, 2005
Note that ANALYZE TABLE requires SELECT and INSERT privileges, only (LOCK TABLES is not needed).
Sign Up Login You must be logged in to post a comment.