MySQL里执行SHOW INDEX结果中Cardinality的含义

本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明
网址: http://www.penglixun.com/tech/database/mysql_show_index_cardinality.html

今天在写一个Perl脚本,想自动化查找出MySQL数据库中可能无效的索引,于是根据朝阳的书上提到的一些规则,我来设计了一些判断方法,其中发现某个我想要的值就是SHOW INDEX FROM table里的Cardinality,于是查了下它的含义以验证我的想法。

MySQL中SHOW INDEX FROM table 会有以下结果列

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |

我想知道的是Cardinality列的含义。
查看官方文档的解释:
An estimate of the number of unique values in the index. This is updated by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on statistics stored as integers, so the value is not necessarily exact even for small tables. The higher the cardinality, the greater the chance that MySQL uses the index when doing joins.

所以这个Cardinality会有如下的含义:
1. 列值代表的是此列中存储的唯一值的个数(如果此列为primary key 则值为记录的行数)
2. 列值只是个估计值,并不准确。
3. 列值不会自动更新,需要通过Analyze table来更新一张表或者mysqlcheck -Aa来进行更新整个数据库。
4. 列值的大小影响Join时是否选用这个Index的判断。
5. 创建Index时,MyISAM的表Cardinality的值为null,InnoDB的表Cardinality的值大概为行数。
6. MyISAM与InnoDB对于Cardinality的计算方式不同。

InnoDB中会采用什么计算方式呢?我回家后试一试。

这篇文章介绍很详细:

http://www.mysqlperformanceblog.com/2008/09/03/analyze-myisam-vs-innodb/

 
 
 
posted @ 2013-11-25 16:33  seasonzone  阅读(445)  评论(0编辑  收藏  举报