官方文档的解释:
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. 列值只是个估计值,并不准确。(我用的innodb数据库实际的例子中这个值也不是太准确)
3. 列值不会自动更新,需要通过analyze table来进行更新。
4. 列值的大小影响join时是否选用这个index的判断。
5. 初建index时,MyISAM的表cardinality的值为null,InnoDB的表cardinality的值大概为行数。
6. MyISAM与InnoDB对于cardinality的计算方式不同。
本质:Cardinality表示是某列作为索引,不重复记录数的预估值。
1、Cardinality/n_rows_in_table 应该尽可能==1,如果非常小,那用户考虑不要建这个索引了。
本地测试:
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`name` varchar(64) CHARACTER SET utf8 NOT NULL COMMENT '名字',
`sex` int(4) DEFAULT NULL,
`age` int(11) NOT NULL COMMENT '年龄',
`tid` int(11) NOT NULL COMMENT 'tid',
`leaveamount` decimal(10,0) DEFAULT NULL COMMENT '剩余可用额度',
`updatedate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新',
`lastdate` date DEFAULT NULL,
`refid` varchar(50) CHARACTER SET latin1 DEFAULT NULL,
`supplier_name` varchar(255) CHARACTER SET latin1 DEFAULT NULL COMMENT '供应商名称',
`uuid` varchar(200) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_student_uuid` (`uuid`),
UNIQUE KEY `UK_student_refid` (`refid`)
) ENGINE=InnoDB AUTO_INCREMENT=100202 DEFAULT CHARSET=utf8mb4
mysql> select count(1) from student;
+----------+
| count(1) |
+----------+
| 100201 |
+----------+
1 row in set (0.08 sec)
mysql> show index from student;
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | id | A | 36722 | NULL | NULL | | BTREE | | |
| student | 0 | UK_student_uuid | 1 | uuid | A | 36540 | NULL | NULL | | BTREE | | |
| student | 0 | UK_student_refid | 1 | refid | A | 36722 | NULL | NULL | YES | BTREE | | |
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.02 sec)
mysql> analyze local table student;
+---------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------------+---------+----------+----------+
| mysql.student | analyze | status | OK |
+---------------+---------+----------+----------+
1 row in set (0.02 sec)
mysql> show index from student;
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | id | A | 99552 | NULL | NULL | | BTREE | | |
| student | 0 | UK_student_uuid | 1 | uuid | A | 96697 | NULL | NULL | | BTREE | | |
| student | 0 | UK_student_refid | 1 | refid | A | 99552 | NULL | NULL | YES | BTREE | | |
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
强制刷新Cardinality语句
analyze local table net_user;