UUID v4 vs v7: Which One Should Your Database Use?

By The UtilCraft team · 4 min read ·

For years the standard advice was "use UUID v4 if you need IDs that are unique without a central counter". It works, but anyone who has run a large table with random UUID primary keys has probably met the downside: inserts get slower and indexes get bigger than they should. UUID version 7, standardised in RFC 9562 in 2024, was designed to fix exactly that. Here's what's different and when each one is the right choice.

What's inside each version

Both are 128 bits, written as the same 36-character string. The difference is how those bits are filled.

v4: 2f1c8a6e-93b0-4d5e-a7c2-6b1e0f9d8c34
    └──────────── 122 random bits ────────────┘

v7: 0192d4e8-7b3a-7c41-9f2e-5a8b1c3d4e6f
    └─ 48-bit ms ─┘    └── 74 random bits ──┘
       timestamp

v4 is pure randomness (apart from 6 bits that mark the version and variant). Two v4 UUIDs generated a microsecond apart are no more similar than two generated a year apart.

v7 starts with the current Unix time in milliseconds, then fills the rest with random bits. That means v7 UUIDs sort by creation time: one made later compares greater than one made earlier, both as bytes and as text. Within the same millisecond, implementations either rely on randomness or use a small counter to keep strict ordering, both of which the RFC allows.

Why random keys hurt databases

Most relational databases store indexes as B-trees: sorted structures split into fixed-size pages, typically 8 KB in PostgreSQL and 16 KB in MySQL's InnoDB. Where a new key goes depends on its value.

With an auto-incrementing integer, every new key is the largest so far, so it goes on the rightmost page. That page is almost certainly already in memory, it fills up neatly, and when it's full a fresh one is started.

With UUID v4, every new key lands at a random position in the index. In practice that means:

  • Cache misses. Once the index is bigger than memory, inserts keep touching pages that have to be read from disk.
  • Page splits. Inserting into the middle of a full page splits it into two half-empty pages. Over time, the index is much larger than the data strictly requires.
  • More write amplification. In PostgreSQL, the first change to a page after each checkpoint writes the whole page to the WAL. Random inserts touch many more distinct pages, so the WAL grows faster.

In MySQL/InnoDB it's worse, because the primary key is the table: rows are physically stored in primary key order, so random keys scatter the table itself, not just an index.

UUID v7 behaves much like an auto-increment for the index: new keys cluster at the right-hand edge. You keep the advantages of UUIDs (generate anywhere, merge data from different systems, no central sequence) while getting nearly the insert pattern of an integer key.

On a small table none of this matters. It starts to show once the index outgrows available memory, usually in the tens of millions of rows, or on write-heavy tables like events, logs and orders.

The trade-off: v7 reveals when it was created

Anyone who sees a v7 UUID can read the first 48 bits and recover the creation time to the millisecond. Often that doesn't matter, or the creation date is public anyway. But think twice when:

  • IDs appear in URLs and the timing is sensitive, such as when an account was created or when a document was uploaded.
  • You don't want competitors estimating your growth rate by sampling IDs over time.
  • Two records shouldn't be linkable by having been created at the same moment.

A common pattern is to use v7 as the internal primary key and expose a separate random identifier (v4, or a shorter random token) in public URLs.

Also remember that neither version is a secret. v7 has fewer random bits than v4 (74 vs 122), which is still plenty to avoid collisions, but don't use either as a password-reset token or API key. Use 256 random bits for those.

What about v1?

Version 1 is also time-based, but it stores the timestamp in a scrambled field order (low bits first), so it doesn't sort chronologically as bytes or text. It traditionally includes the machine's MAC address too, which is a privacy leak. v6 reorders v1's fields to make it sortable, mainly to help migrate existing v1 systems. For new designs, v7 is the one to use.

Storing UUIDs properly

The version matters less if the storage is wasteful. A few rules:

  • PostgreSQL: use the native uuid type (16 bytes). PostgreSQL 18 adds a built-in uuidv7() function; on earlier versions, generate v7 in your application.
  • MySQL / MariaDB: store as BINARY(16), not CHAR(36). MySQL's UUID_TO_BIN(uuid, 1) swap flag exists to make v1 UUIDs sortable; it isn't needed, and shouldn't be used, with v7. MariaDB 10.7+ has a native UUID type.
  • SQL Server: uniqueidentifier sorts by an unusual byte order, so v7 values don't index in creation order there unless you rearrange the bytes. SQL Server's own NEWSEQUENTIALID() is designed for that engine.
  • Never as text if you can avoid it: 36 bytes plus overhead instead of 16, and slower comparisons.

Which should you use?

SituationChoose
Primary key on a large or write-heavy tablev7
Public identifier where creation time should stay privatev4
Idempotency keys, correlation IDs, request IDsv4 (v7 if you like sortable logs)
Event IDs you'll often query by time rangev7
Compatibility with an existing v1 systemv1 (or v6)
Secrets: reset tokens, API keys, session IDsNeither: use 256 random bits

If you want to see the difference for yourself, generate a batch of each in our UUID Generator. The v7 list comes out already sorted, with the first groups barely changing from one to the next, while the v4 list is a jumble.

Tools used in this guide

Keep reading