8.3.8 InnoDB and MyISAM Index Statistics CollectionInnoDB和MyISAM索引统计集合

Storage engines collect statistics about tables for use by the optimizer. 存储引擎收集有关表的统计信息以供优化器使用。Table statistics are based on value groups, where a value group is a set of rows with the same key prefix value. 表统计信息基于值组,其中值组是一组具有相同键前缀值的行。For optimizer purposes, an important statistic is the average value group size.对于优化器,一个重要的统计信息是平均值组大小。

MySQL uses the average value group size in the following ways:MySQL通过以下方式使用平均值组大小:

As the average value group size for an index increases, the index is less useful for those two purposes because the average number of rows per lookup increases: For the index to be good for optimization purposes, it is best that each index value target a small number of rows in the table. 随着索引的平均值组大小的增加,索引对于这两个目的的用处就越小,因为每次查找的平均行数会增加:为了使索引更好地用于优化目的,最好每个索引值都以表中的少量行为目标。When a given index value yields a large number of rows, the index is less useful and MySQL is less likely to use it.当一个给定的索引值产生大量的行时,索引就不太有用,MySQL也不太可能使用它。

The average value group size is related to table cardinality, which is the number of value groups. 平均值组大小与表基数有关,表基数是值组的数目。The SHOW INDEX statement displays a cardinality value based on N/S, where N is the number of rows in the table and S is the average value group size. SHOW INDEX语句基于N/S显示基数值,其中N是表中的行数,S是平均值组大小。That ratio yields an approximate number of value groups in the table.这个比率在表中产生了近似数量的值组。

For a join based on the <=> comparison operator, NULL is not treated differently from any other value: NULL <=> NULL, just as N <=> N for any other N.对于基于<=>比较运算符,NULL与任何其他值的处理方式没有区别:NULL <=> NULL,就像N <=> NN代表任何其他的N

However, for a join based on the = operator, NULL is different from non-NULL values: expr1 = expr2 is not true when expr1 or expr2 (or both) are NULL. 但是,对于基于=运算符的联接,NULL与非NULL值不同:当expr1expr2(或两者)为NULL时,expr1 = expr2不是trueThis affects ref accesses for comparisons of the form tbl_name.key = expr: MySQL does not access the table if the current value of expr is NULL, because the comparison cannot be true.这会影响用于比较tbl_name.key = expr格式的ref访问:如果expr的当前值为NULL,MySQL不会访问该表,因为比较不能为true

For = comparisons, it does not matter how many NULL values are in the table. 对于=比较,表中有多少NULL值无关紧要。For optimization purposes, the relevant value is the average size of the non-NULL value groups. 出于优化目的,相关值是非NULL值组的平均大小。However, MySQL does not currently enable that average size to be collected or used.但是,MySQL目前无法收集或使用该平均大小。

For InnoDB and MyISAM tables, you have some control over collection of table statistics by means of the innodb_stats_method and myisam_stats_method system variables, respectively. 对于InnoDBMyISAM表,您可以分别通过innodb_stats_methodmyisam_stats_method系统变量来控制表统计信息的收集。These variables have three possible values, which differ as follows:这些变量有三个可能的值,其差异如下:

If you tend to use many joins that use <=> rather than =, NULL values are not special in comparisons and one NULL is equal to another. 如果您倾向于使用<=>而不是=,则NULL值在比较中不再特殊,一个NULL等于另一个NULLIn this case, nulls_equal is the appropriate statistics method.在这种情况下,nulls_equal是合适的统计方法。

The innodb_stats_method system variable has a global value; the myisam_stats_method system variable has both global and session values. innodb_stats_method系统变量具有全局值;myisam_stats_method系统变量同时具有全局值和会话值。Setting the global value affects statistics collection for tables from the corresponding storage engine. 设置全局值会影响从相应存储引擎收集表的统计信息。Setting the session value affects statistics collection only for the current client connection. 设置会话值仅影响当前客户端连接的统计信息收集。This means that you can force a table's statistics to be regenerated with a given method without affecting other clients by setting the session value of myisam_stats_method.这意味着您可以通过设置myisam_stats_method的会话值,强制使用给定的方法重新生成表的统计信息,而不会影响其他客户端。

To regenerate MyISAM table statistics, you can use any of the following methods:要重新生成MyISAM表统计信息,可以使用以下任何方法:

Some caveats regarding the use of innodb_stats_method and myisam_stats_method:关于使用innodb_stats_methodmyisam_stats_method的一些注意事项: