php 处理大 (UUID) 作为 MySQL 表主键的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16122934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:32:44  来源:igfitidea点击:

Best way to handle large (UUID) as a MySQL table primary key

phpmysqlhexdecimal

提问by Aaron Murray

I have a UUID string that I want to use as my MySQL tables primary key, the UUID is a 32 character hexadecimal string (after '-' characters are stripped). Due to the fact that it is better to use a numeric column (int) as your primary key in a database, I would like to convert this to an integer but not sure of the best way to handle it.

我有一个 UUID 字符串,我想用作我的 MySQL 表主键,UUID 是一个 32 个字符的十六进制字符串(在“-”字符被剥离之后)。由于在数据库中最好使用数字列 (int) 作为主键,我想将其转换为整数,但不确定处理它的最佳方法。

  1. Due to the size of the string (ie. uuid='a822ff2bff02461db45ddcd10a2de0c2'), do I need to break this into multiple 'substrings'.
  2. I am running PHP on a 32 bit architecture at the moment so converting it within PHP will not work due to PHP_INT_MAX size (max 0xFFFFFFFF). And I suspect that would be the same restriction for MySQL.
  3. I do not like the idea of multiple primary keys as a fix for this, I would rather use a string representation even though that's not the preferred method.
  1. 由于字符串的大小(即 uuid='a822ff2bff02461db45ddcd10a2de0c2'),我是否需要将其分解为多个“子字符串”。
  2. 我目前在 32 位体系结构上运行 PHP,因此由于 PHP_INT_MAX 大小(最大 0xFFFFFFFF),在 PHP 中将其转换将不起作用。我怀疑这对 MySQL 来说也是一样的限制。
  3. 我不喜欢使用多个主键来解决这个问题,我宁愿使用字符串表示,即使这不是首选方法。

I might be thinking about this all wrong, and am not against reading documentation, so either examples or suggested reading as a response would be acceptable.

我可能认为这一切都是错误的,并且不反对阅读文档,因此无论是示例还是建议阅读作为回应都是可以接受的。

回答by Hazzit

For most cases it is best to store UUIDs/GUIDs as BINARY(16). See these related StackOverflow questions:

在大多数情况下,最好将 UUID/GUID 存储为BINARY(16). 请参阅这些相关的 StackOverflow 问题:

The conversion can (and probably should) be done in MySQL instead of PHP so whether you're using a 32bit PHP client or 64bits doesn't matter a bit (pun intended :P)

转换可以(并且可能应该)在 MySQL 而不是 PHP 中完成,因此无论您使用的是 32 位 PHP 客户端还是 64 位都无关紧要(双关语:P)

回答by PaulProgrammer

Use a string type, not an integer. Better is only better if it solves a problem.

使用字符串类型,而不是整数。只有解决问题才能更好。

If you're really concerned about lookup speed, use a synthetic (auto increment) primary key. You can place a unique constraint on the UUID column, and use it only once to look up the synthetic key which is subsequently used for your joins etc.

如果您真的很关心查找速度,请使用合成(自动增量)主键。您可以在 UUID 列上放置唯一约束,并且仅使用一次来查找随后用于连接等的合成键。

回答by giuseppe

It depends on storage engine also. TokuDB should handle all these problems.

这也取决于存储引擎。TokuDB 应该处理所有这些问题。