与MySQL数据字典表(mysql.table_stats等)不同, 用户可以查询mysql.innodb_table_stats和mysql.innodb_index_stats。它们还包含许多有趣的统计数据。在开始之前,让我们更详细地检查这些表的来源。
正如手册告诉我们的,这些表存储的是InnoDB持久性统计信息,但前提是启用了这些统计信息。在MySQL 8中,默认情况是使用持久统计并自动更新它们,用户可以通过设置innodb_stats_Persistent=0变量禁用持久统计,或者通过设置innodb_stats_auto_recalc=0禁用自动更新,这意味着这些表中的数据可能丢失或非常陈旧。
此外,为了追求最大的灵活性,MySQL团队允许您使用STATS_PERSISTENT,STATS_AUTO_RECALC和STATS_SAMPLE_PAGES CREATE TABLE子句逐个表地启用/禁用这些选项。这意味着某些表可能存在信息,而其他表则没有。
因此,尽管这些数据在呈现时非常有价值,但您不应该指望它的存在或准确性。
好,让我们看一下这些表中的内容:
mysql> select * from mysql.innodb_table_stats where database_name='tpcc' and table_name='orders1' \G *************************** 1. row *************************** database_name: tpcc table_name: orders1 last_update: 2020-01-04 23:59:27 n_rows: 380197 clustered_index_size: 1185 sum_of_other_index_sizes: 672 1 row in set (0.00 sec)
复制
表信息包含估计的行数以及聚集索引的大小以及页面中组合的所有其他索引的大小。将其乘以innodb_page_size 变量,您将获得以字节为单位的大小。
另外,请注意last_update列,该列指定上次更新统计信息的时间。非常老的last_update可能意味着两件事-表未获得太多写入以触发统计信息更新,或者禁用了此表的自动统计信息更新。
现在让我们看一下innodb_index_stats表:
mysql> select * from mysql.innodb_index_stats where database_name='tpcc' and table_name='orders1'; +---------------+------------+-------------+---------------------+--------------+------------+-------------+-----------------------------------+ | database_name | table_name | index_name | last_update | stat_name | stat_value | sample_size | stat_description | +---------------+------------+-------------+---------------------+--------------+------------+-------------+-----------------------------------+ | tpcc | orders1 | PRIMARY | 2020-01-04 23:59:27 | n_diff_pfx01 | 9 | 11 | o_w_id | | tpcc | orders1 | PRIMARY | 2020-01-04 23:59:27 | n_diff_pfx02 | 101 | 20 | o_w_id,o_d_id | | tpcc | orders1 | PRIMARY | 2020-01-04 23:59:27 | n_diff_pfx03 | 380197 | 20 | o_w_id,o_d_id,o_id | | tpcc | orders1 | PRIMARY | 2020-01-04 23:59:27 | n_leaf_pages | 991 | NULL | Number of leaf pages in the index | | tpcc | orders1 | PRIMARY | 2020-01-04 23:59:27 | size | 1185 | NULL | Number of pages in the index | | tpcc | orders1 | idx_orders1 | 2020-01-04 23:59:27 | n_diff_pfx01 | 9 | 11 | o_w_id | | tpcc | orders1 | idx_orders1 | 2020-01-04 23:59:27 | n_diff_pfx02 | 101 | 20 | o_w_id,o_d_id | | tpcc | orders1 | idx_orders1 | 2020-01-04 23:59:27 | n_diff_pfx03 | 306329 | 20 | o_w_id,o_d_id,o_c_id | | tpcc | orders1 | idx_orders1 | 2020-01-04 23:59:27 | n_diff_pfx04 | 378338 | 20 | o_w_id,o_d_id,o_c_id,o_id | | tpcc | orders1 | idx_orders1 | 2020-01-04 23:59:27 | n_leaf_pages | 577 | NULL | Number of leaf pages in the index | | tpcc | orders1 | idx_orders1 | 2020-01-04 23:59:27 | size | 672 | NULL | Number of pages in the index | +---------------+------------+-------------+---------------------+--------------+------------+-------------+-----------------------------------+ 11 rows in set (0.00 sec)
复制
该表被设计为可扩展的,并且能够为同一索引保存许多不同的统计信息。
我们在这里看到一些统计数据。首先,有n_diff_pfxXX个统计信息显示不同索引前缀的基数-属于该前缀的特定列在stat_description 值中指定。如果执行tpcc.orders1命令的SHOW INDEX,则这些基数值相同 。
其他统计信息是n_leaf_pages和size,它们为每个索引指定完整大小(以页为单位)以及叶页数。两者之间的区别在于表具有的非叶页数。
在此示例中,orders1表具有95个非叶子页和577个叶子页,或总计672页。
如果您要完全缓存特定的索引,或者至少要缓存非叶页(确保索引查找不会进行多个IO操作),则此信息可以帮助您确定InnoDB缓冲池中需要多少空间。
总结
mysql.innodb_table_stats和mysql.innodb_index_stats可以作为有关InnoDB表和索引的详细信息的重要来源,并且它们在现代MySQL版本中默认启用并可用。但是请记住,在某些配置下,这些表中的信息不可用或严重过时。另外,由于统计数据是通过抽样计算得出的,因此它们并不准确-在某些情况下可能会非常不准确。