https://dev.mysql.com/doc/refman/5.7/en/analyze-table.html

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

ANALYZE TABLE performs a key distribution analysis and stores the distribution for the named table or tables. For MyISAM tables, this statement is equivalent to using myisamchk --analyze.

ANALYZE TABLE执行键分布分析并存储命名表的分布。对于MyISAM表,此语句相当于使用myisamchk--analyze。

This statement requires SELECT and INSERT privileges for the table.

此语句需要表的SELECT和INSERT权限

ANALYZE TABLE works with InnoDB, NDB, and MyISAM tables. It does not work with views.

ANALYZE TABLE可用于InnoDB、NDB和MyISAM表。它不适用于视图。

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 13.1.8, “ALTER TABLE Statement”, and Section 21.3.4, “Maintenance of Partitions”.

分区表支持ANALYZE TABLE,您可以使用 ALTER TABLE ... ANALYZE PARTITION分析一个或多个分区;有关更多信息,请参阅第13.1.8节“ALTER TABLE语句”和第21.3.4节“分区维护”。

During the analysis, the table is locked with a read lock for InnoDB and MyISAM.

在分析过程中,表被InnoDB和MyISAM的读锁锁定。

ANALYZE TABLE removes the table from the table definition cache, which requires a flush lock. If there are long running statements or transactions still using the table, subsequent statements and transactions must wait for those operations to finish before the flush lock is released. Because ANALYZE TABLE itself typically finishes quickly, it may not be apparent that delayed transactions or statements involving the same table are due to the remaining flush lock.

ANALYZE TABLE从表定义缓存中删除该表,这需要一把flush lock。如果有长时间运行的语句或事务仍在使用表,则后续语句和事务必须等待这些操作完成,然后释放flush lock。

因为ANALYZE TABLE本身通常很快结束,所以延迟的事务或涉及同一表的语句可能不是由于剩余的flush锁造成的。

.By default, the server writes ANALYZE TABLE statements to the binary log so that they replicate to replicas. To suppress logging, specify the optional NO_WRITE_TO_BINLOG keyword or its alias LOCAL.

默认情况下,服务器将ANALYZE TABLE语句写入二进制日志,以便将它们复制到副本中。要禁止日志记录,请指定可选的NO_WRITE_TO_BINLOG关键字或其别名LOCAL。

ANALYZE TABLE Output

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

ANALYZE TABLE返回包含下表所示列的结果集。

ANALYZE TABLE_ide

ANALYZE TABLE_sed_02

Key Distribution Analysis

If the table has not changed since the last key distribution analysis, the table is not analyzed again.

如果自上次键分布分析后该表没有更改,则不会再次分析该表。

MySQL uses the stored key distribution to decide the table join order for joins 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.

MySQL使用存储的键分布来决定表连接顺序,以连接除常量以外的其他对象。 此外,在决定对查询中的特定表使用哪些索引时,可以使用键分布。

To check the stored key distribution cardinality, use the SHOW INDEX statement or the INFORMATION_SCHEMA STATISTICS table. See Section 13.7.5.22, “SHOW INDEX Statement”, and Section 23.24, “The INFORMATION_SCHEMA STATISTICS Table”.

要检查存储的密钥分发基数,请使用SHOW INDEX语句或INFORMATION_SCHEMA统计表。参见第13.7.5.22节“SHOW INDEX Statement”和第23.24节“INFORMATION_SCHEMA STATISTICS Table”。

For InnoDB tables, ANALYZE TABLE determines index cardinality by performing random dives on each of the index trees and updating index cardinality estimates accordingly. Because these are only estimates, repeated runs of ANALYZE TABLE could produce different numbers. This makes ANALYZE TABLE fast on InnoDB tables but not 100% accurate because it does not take all rows into account.

对于InnoDB表,ANALYZE TABLE通过对每个索引树执行随机潜水并相应地更新索引基数估计来确定索引基数。 因为这些只是估计,所以重复运行ANALYZE TABLE可能会产生不同的数字。 这使得对InnoDB表的ANALYZE TABLE速度很快,但由于未考虑所有行,因此不能100%准确。

You can make the statistics collected by ANALYZE TABLE more precise and more stable by enabling innodb_stats_persistent, as explained in Section 14.8.11.1, “Configuring Persistent Optimizer Statistics Parameters”. When innodb_stats_persistent is enabled, it is important to run ANALYZE TABLE after major changes to index column data, as statistics are not recalculated periodically (such as after a server restart).

您可以通过启用innodb_stats_persistent使ANALYZE TABLE收集的统计信息更加精确和稳定,如第14.8.11.1节“配置持久性优化器统计信息参数”中所述。 启用innodb_stats_persistent时,在对索引列数据进行重大更改之后运行ANALYZE TABLE是很重要的,因为不会定期重新计算统计信息(例如,在服务器重新启动之后)。

If innodb_stats_persistent is enabled, you can change the number of random dives by modifying the innodb_stats_persistent_sample_pages system variable. If innodb_stats_persistent is disabled, modify innodb_stats_transient_sample_pages instead.

如果启用了innodb_stats_persistent,则可以通过修改innodb_stats_persistent_sample_pages系统变量来更改随机潜水的次数。 如果禁用了innodb_stats_persistent,请改为修改innodb_stats_transient_sample_pages。

For more information about key distribution analysis in InnoDB, see Section 14.8.11.1, “Configuring Persistent Optimizer Statistics Parameters”, and Section 14.8.11.3, “Estimating ANALYZE TABLE Complexity for InnoDB Tables”.

有关InnoDB中键分布分析的更多信息,请参阅第14.8.11.1节“配置持久优化器统计参数”和第14.8.11.3节“估计InnoDB表的分析表复杂性”。

MySQL uses index cardinality estimates in join optimization. If a join is not optimized in the right way, try running ANALYZE TABLE. In the few cases that ANALYZE TABLE does not produce values good enough for your particular tables, you can use FORCE INDEX with your queries to force the use of a particular index, or set the max_seeks_for_key system variable to ensure that MySQL prefers index lookups over table scans. See Section B.3.5, “Optimizer-Related Issues”.

MySQL在联接优化中使用索引基数估计。 如果没有以正确的方式优化联接,请尝试运行ANALYZE TABLE。 在少数情况下,ANALYZE TABLE不能为您的特定表提供足够好的值,您可以在查询中使用FORCE INDEX来强制使用特定索引,或者设置max_seeks_for_key系统变量以确保MySQL比表更喜欢索引查找 扫描。 请参见第B.3.5节“与优化程序有关的问题”。

Other Considerations

其他注意事项

ANALYZE TABLE clears table statistics from the INFORMATION_SCHEMA.INNODB_SYS_TABLESTATS table and sets the STATS_INITIALIZED column to Uninitialized. Statistics are collected again the next time the table is accessed.

ANALYZE TABLE会从INFORMATION_SCHEMA.INNODB_SYS_TABLESTATS表中清除表统计信息,并将STATS_INITIALIZED列设置为Uninitialized。 下次访问该表时,将再次收集统计信息。

Important