简体   繁体   English

将UUID存储在MYSQL表中的各种选项及其权衡是什么?

[英]What are the various options and their tradeoffs for storing a UUID in a MYSQL table?

I'm planning on using client provided UUID's as the primary key in several tables in a MySQL Database. 我打算使用客户端提供的UUID作为MySQL数据库中几个表的主键。

I've come across various mechanisms for storing UUID's in a MySQL database but nothing that compares them against each other. 我遇到过各种机制,用于将UUID存储在MySQL数据库中,但没有任何机制可以将它们相互比较。 These include storage as: 这些包括存储为:

  • BINARY(16) BINARY(16)
  • CHAR(16) CHAR(16)
  • CHAR(36) CHAR(36)
  • VARCHAR(36) VARCHAR(36)
  • 2 x BIGINT 2 x BIGINT

Are there any better options, how do the options compare against each other in terms of: 有没有更好的选择,这些选项如何在以下方面相互比较:

  • storage size? 存储大小?
  • query overhead? 查询开销? (index issues, joins etc.) (索引问题,联接等)
  • ease of inserting and updating values from client code? 是否易于从客户端代码中插入和更新值? (typically Java via JPA) (通常是JPA的Java)

Are there any differences based on which version of MySQL your running, or the storage engine? 根据您运行的MySQL版本或存储引擎,是否存在任何差异? We're currently running 5.1 and were planning on using InnoDB. 我们目前正在运行5.1,并计划使用InnoDB。 I'd welcome any comments based on practical experience of trying to use UUIDs. 我欢迎任何基于尝试使用UUID的实践经验的评论。 Thanks. 谢谢。

I would go with storing it in a Binary(16) column, if you are indeed set on using UUIDs at all. 如果你真的开始使用UUID,我会将它存储在二进制(16)列中。 something like 2x bigint would be quite cumbersome to manage. 像2x bigint这样的东西管理起来会非常麻烦。 Also, i've heard of people reversing them because the start of the UUIDs on the same machine tend to be the same at the beginning, and the different parts are at the end, so if you reverse them, your indexes will be more efficient. 另外,我听说有人倒车,因为同一台机器上UUID的启动在开始时往往是相同的,而且不同的部分都在最后,所以如果你反转它们,你的索引会更有效率。

Of course, my instinct says that you should be using auto increment integers unless you have a really good reason for using the UUID. 当然,我的直觉说你应该使用自动增量整数,除非你有充分的理由使用UUID。 One good reason is generating unique keys accross different databases. 一个很好的理由是在不同的数据库中生成唯一的密钥。 The other option is that you plan to have more records than an INT can store. 另一种选择是您计划拥有的记录多于INT可以存储的记录。 Although not many applications really need things like this. 虽然没有多少应用程序真的需要这样的东西。 THere is not only a lot of efficiency lost when not using integers for your keys, and it's also harder to work with them. 如果不为键使用整数,这不仅会降低很多效率,而且使用它们也更难。 they are too long to type in, and passing them around in your URLs make the URLs really long. 它们太长而无法输入,并且在您的网址中传递它们会使网址变得非常长。 So, go with the UUID if you need it, but try to stay away. 所以,如果你需要它,请使用UUID,但试着远离它。

I have used UUIDs for smart client online/offline storage and data synchronization and for databases that I knew would have to be merged at some point. 我已经使用UUID进行智能客户端在线/离线存储和数据同步,以及我知道必须在某些时候合并的数据库。 I have always used char(36) or char(32)(no dashes). 我一直使用char(36)或char(32)(没有破折号)。 You get a slight performance gain over varchar and almost all databases support char. 与varchar相比,您获得了轻微的性能提升,几乎所有数据库都支持char。 I have never tried binary or bigint. 我从来没有尝试二进制或bigint。 One thing to be aware of, is that char will pad with spaces if you do not use 36 or 32 characters. 需要注意的一点是,如果不使用36或32个字符,char将填充空格。 Point being, don't write a unit test that sets the ID of an object to "test" and then try to find it in the database. 重点是,不要写一个单元测试,将对象的ID设置为“test”,然后尝试在数据库中找到它。 ;) ;)

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

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