UUID v4 vs v7: Which One Should You Use?
not all UUIDs are equal. v4 is fully random, while v7 embeds a timestamp so new IDs sort by creation time. That small difference has big consequences for database indexes. This guide helps you choose between them.
What Is the Difference Between UUID v4 and v7?
A v4 UUID is built from random bytes only. A v7 UUID prepends a millisecond timestamp and keeps enough randomness after it, so it is both time-ordered and unpredictable.
Why Does Time Order Matter for Database Indexes?
A random primary key scatters inserts across an index, causing page splits and cache misses. A time-ordered key appends near the end of a B-tree, which keeps inserts fast and index pages warm under load.
Is v7 Less Random or Less Secure Than v4?
No. v7 still stores a healthy number of random bits, so it stays unique and unguessable. The timestamp makes it sortable without sacrificing meaningful randomness.
How Do You Generate Each Version?
Generate v4 when you want pure randomness and v7 when you want sortable keys, with or without hyphens, using the UUID generator, which supports both versions in bulk.