问题
I want to use UUIDs as IDs for my JPA Objects.
I am currently just using a String to store the UUID. What would be more efficient?
回答1:
How are you measuring efficiency?
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?
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.
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.
来源:https://stackoverflow.com/questions/730788/best-most-efficient-datatype-to-use-for-uuids-as-jpa-ids