表统计信息保存在表 mysql.innodb_table_stats 里
比如表 ytt_sample_persist 的统计信息- clustered_index_size:主键的数据页个数
- sum_of_other_index_sizes:二级索引的数据页个数
以下例子可以看到表 ytt_sample_persist 表行数大概为 36W 行,聚簇索引页数为 15162,二级索引页数为 4113。mysql> select n_rows,clustered_index_size,sum_of_other_index_sizes from innodb_table_stats where database_name ='ytt' and table_name = 'ytt_sample_persist';+--------+----------------------+--------------------------+| n_rows | clustered_index_size | sum_of_other_index_sizes |+--------+----------------------+--------------------------+| 356960 | 15162 | 4113 |+--------+----------------------+--------------------------+1 row in set (0.00 sec)
其实表 ytt_sample_persist 真实数据为 40W 行。mysql> select count(*) from ytt_sample_persist;+----------+| count(*) |+----------+| 406644 |+----------+1 row in set (0.90 sec)
mysql> analyze table ytt_sample_persist;+------------------------+---------+----------+----------+| Table | Op | Msg_type | Msg_text |+------------------------+---------+----------+----------+|+------------------------+---------+----------+----------+1 row in set (0.28 sec)
统计信息已经更新为最新,数据又离真实数据接近一点。
mysql> select n_rows,clustered_index_size,sum_of_other_index_sizes from innodb_table_stats where database_name ='ytt' and table_name ='ytt_sample_persist';+--------+----------------------+--------------------------+| n_rows | clustered_index_size | sum_of_other_index_sizes |+--------+----------------------+--------------------------+| 387202 | 16380 | 4562 |+--------+----------------------+--------------------------+1 row in set (0.01 sec)