1

我正在使用 BIGINT 来保存一个从 1 开始递增的 id 号。在一个表中,这将是主键,当然,它是唯一的;在其他表中,它将是一个外键。我试图弄清楚如果我设置 PACK_KEYS,这个密钥是否会被“打包”,因为会有很多前导零。

我很难理解表创建中 PACK_KEYS 表选项的 MySQL 文档。以下是文档中的相关引用:

在打包二进制数字键时,MySQL 使用前缀压缩:

  • 每个键都需要一个额外的字节来表示前一个键的多少字节与下一个键相同。

  • 指向行的指针以高字节优先顺序存储在键之后,以提高压缩率。

这意味着如果您在两个连续的行上有许多相等的键,那么所有随后的“相同”键通常只占用两个字节(包括指向该行的指针)。将此与以下键采用 storage_size_for_key + pointer_size(指针大小通常为 4)的普通情况进行比较。相反,只有当您有许多相同的数字时,您才能从前缀压缩中获得显着的好处。如果所有键都完全不同,则每个键多使用一个字节,如果该键不是可以具有 NULL 值的键。(在这种情况下,打包的密钥长度存储在用于标记密钥是否为 NULL 的相同字节中。)

他们让我迷失了“在两个连续的行上有许多相等的键,所有后面的“相同”键通常只占用两个字节(包括指向行的指针) ”。有人可以根据我要完成的工作为我解释上述文档吗?例如,对于主键,不会有任何“相等的键”——连续两行、连续三行、100 个不连续的行......或者他们正在驾驶的任何东西。

谢谢!

4

1 回答 1

1

Chances are you do not need PACK_KEYS. I see you are using BIGINT for your PK. How many rows are you looking at having in this table eventually?? What kind of data are you storing? How do you intend to retrieve/report on it and how often?? These are things I would consider first before using this feature.

If I read that documentation correctly, it's basically stating that if you have two consecutive records with long PKs say:

PK-x: 1002350025789001 PK-y: 1002350025789002

With PACK_KEYS, PK-y now becomes something like "[pointer to PK-x]2"

It's basically a way of saying PK-2 is the same as PK-1 except for the last number which is 2... without having to rewrite/store the same refix/preceding numbers.

The gains from this are most likely only realized when you are dealing with very long PKs and will mostly be gains in storage/memory, however I would imagine there's a cost to overall performance which may or may not be noticeable depending on how much access load that table gets.

May not be worth it... I've never used this feature, and I've built some pretty heavy apps on MySQL.

hope this helps.

Good Luck

于 2012-12-13T22:46:29.410 回答