Best (most efficient) DataType to use for UUIDs as JPA IDs

时光总嘲笑我的痴心妄想 提交于 2019-12-23 18:17:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!