mysql - 在mysql中,为什么二进制列上长度为1的前缀索引的基数是534?不应该是256吗?

标签 mysql uuid binary-data

我在存储 UUId 的二进制 16 列上创建了一个大小为 1 的前缀索引。它显示的基数为 532。第一个字符可以有 256 个不同的值,那么基数怎么比它高这么多?

create index my_index on my_table (foreign_key(1));

desc my_table;

Field           | Type         | Null | Key | Default | Extra
foreign_key     | binary(16)   | YES  | MUL | NULL    |       

select count(*) from my_table;
+----------+
| count(*) |
+----------+
|   103979 |
+----------+



ANALYZE TABLE my_table;

show index from my_table;

+--------------------+------------+---------------------------------------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table              | Non_unique | Key_name                                                | Seq_in_index | Column_name     | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+---------------------------------------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+    
| user_problem_types |          1 | my_index                                                | 1            | my_index        | A         |         534 |        1 | NULL   | YES  | BTREE      |         |               |

最佳答案

您的索引中单字节前缀键的分布似乎不均匀,这会略微推高每个索引条目的平均行数。

基数大致计算如下:

(Number of Rows) / (Average number of rows per index entry)

在你的例子中,总和看起来是这样的:

103979 / 194

您和我都知道一个字节中最多可以有 256 个不同的值,但是为了提高效率,基数是基于上述计算的近似值。结果,您得到 534,虽然不精确,但肯定在正确的数量级内,并且对于优化器近似来说已经足够好了。

关于mysql - 在mysql中,为什么二进制列上长度为1的前缀索引的基数是534?不应该是256吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21184118/

相关文章:

python - Python解析二进制数据时嵌套循环的优化

python - 从列表中读入MySql

php - 在链接上调用 php 函数

c# - 在 C# 中解码原始串行协议(protocol)

c++ - 获取系统的唯一标识符(不是 MAC 地址)?

android - Android 上的 RFC 4122 UUID?

binary - Artillery.io - POST 二进制数据

php - 从 CS 购物车的网站搜索中排除类别

php - 在 php 的一页中出现多个表单的问题

ios - 将发现外围设备的UUID存储在CoreBluetooth LE iOS中