Generate a time-ordered UUID v7
Mint UUID version 7: Unix-ms timestamp in the high bits, random in the rest. Better for indexes than v4.
UUID v7 puts a 48-bit Unix millisecond timestamp in the high bits, then random data. New IDs sort roughly in create order, which keeps B-tree indexes from random page splits you get with v4.
The version nibble is 7. Two IDs from the same millisecond still differ in the random bits. This is not a substitute for a monotonic sequence if you need gap-free numbers.
Generate a pair of v4 and v7 in the studio and compare the prefix. Pick v7 for new tables where you query by id and care about insert locality. Keep v4 for public tokens where order must not leak create time.
Version 7 stores Unix milliseconds in the front, so ids created later sort after ids created earlier in a database index. The rest is random, which keeps two ids from the same millisecond apart. It is not a secret timestamp you can hide: anyone can read the time from the hex. Clocks that jump backward can emit an id that sorts before an older row. This page opens on version 7. Random v4 is the page to use when order must not leak creation time.
FAQ
Does v7 leak when it was made?
The timestamp is in the ID. Do not use v7 if create time must stay hidden.
Drop-in for v4?
Same string length. Existing v4 rows can stay; new rows can be v7 if the column is UUID.
Clock skew?
A jumped clock can disorder inserts. NTP and a stable clock matter more than with v4.
