用于UUID的最佳(最有效)DataType作为JPA ID [英] Best (most efficient) DataType to use for UUIDs as JPA IDs
问题描述
我想将UUID用作我的JPA对象的ID。
I want to use UUIDs as IDs for my JPA Objects.
我目前只使用String来存储UUID。什么会更有效?
I am currently just using a String to store the UUID. What would be more efficient?
推荐答案
你如何衡量效率?
例如,将UUID(这是 byte []
的文本编码)存储为几个 long
值将允许您在64位体系结构上快速比较它们(比 String
比较,这是逐个字符的快得多)。但是,您的编码效率会受到影响,因为您必须编写自定义类型。
For example, storing the UUID (which is a text encoding of a byte[]
) as a couple of long
values will allow you to compare them very quickly on a 64-bit architecture (much more quickly than String
comparison, which is character-by-character). However, your coding efficiency would suffer, because you have to write a custom type.
在您的情况下重要的是什么?
What is important in your case?
如果您对数据库中的性能感兴趣,性能将在一定程度上取决于您选择的数据库,但所有这些都将逐字节地比较UUID。因此,重要的是使键之间的差异更可能朝向键的开头。
If you are interested in performance within the database, performance will depend somewhat on the database you choose, but all will essentially compare UUIDs byte-by-byte. So, the important thing is to make differences between keys more likely to be toward the beginning of the key.
UUID在这方面设计得很好。最好的UUID是随机风格。它们在任何地方都没有很多共同的子串,包括开头,并且它们不会泄漏有关生成它们的机器的信息。但是,UUID的时间戳风格也是一个很好的关键,因为低位(快速变化)首先在字符串中,而高位(对于连续的UUID不会改变)来之后。
UUIDs are well-designed in this respect. The best UUID is the "random" style. They don't have a lot of common substrings anywhere, including the beginning, and they don't leak information about the machine that generated them. But, the "timestamp" style of UUIDs makes a good key as well, since the low-order bits (that vary quickly) are first in the string, while the high-order bits (that don't change for successive UUIDs) come later.
这篇关于用于UUID的最佳(最有效)DataType作为JPA ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!