在CakePHP中使用UUID,建议使用什么数据类型?

时间:2022-10-17 23:01:39

My Scenario:

我的情况:

  • New Cake (2.x) Project, No DB Yet
  • 新Cake (2.x)项目,还没有DB
  • MySQL Clustered, and maybe Oracle Clustered Prod
  • MySQL集群,或者Oracle集群Prod
  • No DATA needs to be migrated/imported
  • 没有数据需要迁移/导入。
  • Data can look like: Users -> HABTM -> Groups -> HABTM -> Other Groups
  • 数据如下:用户-> HABTM ->组-> HABTM ->其他组

I've been doing a little research on how to use UUIDs with CakePHP, and I have found the following:

我一直在研究如何使用uuid和akecphp,我发现了以下内容:

Cake has Native Support for UUID, but it assumes CHAR(36):

Cake对UUID有本地支持,但它假定CHAR(36):

http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

This Stack Answer points out that:

这个堆叠的答案指出:

The cost of doing UUID as CHAR(36) is ridiculously high, and becomes stupid at 1+ milion, 10+ million, 100+ millions of rows, in my humble experience

将UUID作为CHAR(36)的成本高得离谱,在我的经验中,在1+ milion、1000 + million、100+成百万行时变得很愚蠢

This Blog Post claims that BINARY(36) is better than CHAR(36):

这篇博文声称二进制(36)比CHAR(36)要好:

Although CakePHP does not support the 16 byte hex encoded UUID with the key type of BINARY(16), it does support BINARY(36) which is still better than using CHAR(36) which can be slowed down by collation.

虽然CakePHP不支持16字节的十六进制编码UUID和密钥类型的二进制(16),但它支持二进制(36),这仍然比使用CHAR(36)更好,可以通过排序来降低。

...but the Cake Docs don't say that...

…但是蛋糕医生并没有说……

My Question is, given CakePHP/MySQL (or CakePHP/Oracle), is CHAR(36) the only reasonable choice here, or is there a better, more efficient way to use UUIDs with CakePHP (or any other PHP Framework for that matter)?

我的问题是,给定CakePHP/MySQL(或CakePHP/Oracle), CHAR(36)是这里唯一合理的选择吗?

2 个解决方案

#1


3  

You probably already know this, but you could generate an id with String::uuid() for new records, and have BINARY(36) in your table - I don't think cake has any automagic past the uuid generation.

您可能已经知道这一点,但是您可以为新记录生成带有String: uuid()的id,并且在您的表中有二进制(36)——我认为cake在uuid生成之后没有任何自动机。

#2


2  

If CakePHP is given a model with an id field whose SQL type is Binary(36), then every time you create new records, the id field will be given a new GUID value. This behaviour is here since version 1.3.15.

如果给CakePHP一个具有id字段的模型,该id字段的SQL类型是二进制(36),那么每次创建新记录时,id字段都会被赋予一个新的GUID值。这种行为从1.3.15版本开始。

Regarding your other issue, since CHAR involves collation, I would go for the binary solution.

关于你的另一个问题,由于CHAR涉及到排序,我将使用二进制解决方案。

#1


3  

You probably already know this, but you could generate an id with String::uuid() for new records, and have BINARY(36) in your table - I don't think cake has any automagic past the uuid generation.

您可能已经知道这一点,但是您可以为新记录生成带有String: uuid()的id,并且在您的表中有二进制(36)——我认为cake在uuid生成之后没有任何自动机。

#2


2  

If CakePHP is given a model with an id field whose SQL type is Binary(36), then every time you create new records, the id field will be given a new GUID value. This behaviour is here since version 1.3.15.

如果给CakePHP一个具有id字段的模型,该id字段的SQL类型是二进制(36),那么每次创建新记录时,id字段都会被赋予一个新的GUID值。这种行为从1.3.15版本开始。

Regarding your other issue, since CHAR involves collation, I would go for the binary solution.

关于你的另一个问题,由于CHAR涉及到排序,我将使用二进制解决方案。