且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Mysql列上的索引具有比计数不同的基数更低的基数?

更新时间:2023-01-29 08:14:44

基数是估计值索引中唯一值的数量。根据文档


基数以整数存储的统计数计算,因此即使小表也不一定精确。



I have a varchar(50) field named token. When I do

count(distinct token) from table 

I get ~ 400k. However, if I do

create index idx on table (token)

The cardinality is only 200. What could be going on here? Shouldn't cardinality be the same as the number of distinct tokens?

The cardinality is an estimate of the number of unique values in the index. According to the documentation:

Cardinality is counted based on statistics stored as integers, so the value is not necessarily exact even for small tables.