简体   繁体   English

正确选择 UUID 作为主键的数据类型

[英]Right choice of data type for UUID as primary key

Prior Conditions are先决条件是

  1. Table will have data in billions表将有数十亿的数据
  2. Table will have secondary indexes表将有二级索引
  3. Table's primary key will be a foreign key of another table.表的主键将是另一个表的外键。
  4. Table will have a heavy data (another column may be Text).表将有大量数据(另一列可能是文本)。

Primary Key must be Unique, cause my database is replicated over machine's that's why I am choosing UUID.主键必须是唯一的,因为我的数据库是通过机器复制的,这就是我选择 UUID 的原因。

PS: space is also a concern so I guess Varchar(36) might be the bad idea PS:空间也是一个问题,所以我猜 Varchar(36) 可能是个坏主意

I agree with BINARY(16) .我同意BINARY(16) (16 bytes is better than 37.) (16 字节比 37 字节好。)

But UUIDs are hopelessly inefficient for huge tables.但是 UUID 对于大表来说效率低得无可救药。 (I assume your billion-row table will not fit in RAM.) (我假设您的十亿行表不适合 RAM。)

I discuss those and more issues in http://mysql.rjweb.org/doc.php/uuid我在http://mysql.rjweb.org/doc.php/uuid 中讨论了这些和更多问题

I would go for BINARY(16).我会选择 BINARY(16)。 If you want to use CHAR then CHAR(32) is large enough without the hyphens, but BINARY is smaller and faster.如果你想使用 CHAR,那么 CHAR(32) 就足够大了,没有连字符,但 BINARY 更小更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM