UUIDGenerator v4 & v7
Random v4 or time-ordered v7, in bulk, in the format you need.
crypto.getRandomValues, never Math.random
First one, in detail
—
UUIDs
Press Generate.
Java
Generated —
Version —
Size 0 B
Ready
v4 or v7 — which one
Both are 128 bits and both are unique in practice. The difference only shows up once they are a database key.
- v4 is 122 random bits. Every insert lands at a random point in the index, so a busy table keeps rewriting pages all over the B-tree and the working set stops fitting in memory. Perfect when the id must reveal nothing.
- v7 puts 48 bits of Unix milliseconds first, so ids sort by creation time and inserts append instead of scattering. That is the whole reason it exists, and why it is the better default for a primary key.
- The trade with v7 is that the row's creation time is readable from its id. If the id is a public token, that is a leak — use v4.
- Java:
UUID.randomUUID()is v4. There is no v7 in the JDK yet, so the snippet on the right builds one fromInstant.now()andSecureRandom.