views:

581

answers:

1

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?

+2  A: 

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.

erickson
Joins and Index Efficiency
Grasper