UUID v4 vs v7: which to use as a database key

By Hammad Rasool · November 11, 2026

Quick answer: UUID v4 is completely random; UUID v7 starts with the time it was created, so new IDs sort after older ones. Use v7 for database primary keys and anything you want to sort by creation time: in a test with 200,000 rows, the v4 primary key in MySQL took 67% more space than the v7 one, and SQLite inserted v7 rows four times faster. Use v4 when an ID must not reveal when it was made. The UUID generator now creates both.

How this was tested: on 11 November 2026, ToolStash's new v7 generator produced 100,000 IDs that the independent uuid npm package (version 11.1.1) accepted as valid version 7 UUIDs, and IDs made by that package decoded to the time they were created for. The database test inserted 200,000 rows keyed by v4 and by v7 IDs into MySQL 8.4.9 (InnoDB, default 16 KB pages, 128 MB buffer pool) and SQLite 3.50.6 on one Windows laptop, two or three runs each. PostgreSQL and SQL Server were not tested.

What's inside each version

v4  f47ac10b-58cc-4372-a567-0e02b2c3d479
                  ^ version 4; the rest is random (apart from the variant bits)

v7  01a1c946-61fe-76d5-9a9e-e4ee6505619c
    ^^^^^^^^^^^^^ Unix time in milliseconds (48 bits)
                  ^ version 7; the rest is random or a counter
UUID v4UUID v7
Defined inRFC 9562 (originally RFC 4122)RFC 9562 (May 2024)
First 48 bitsRandomUnix timestamp in milliseconds
Random bits122Up to 74; some generators use 12 of them as a counter
Sorts by creation timeNoYes
Reveals creation timeNoYes, to the millisecond
Format and length36 characters, 16 bytesSame

Both have the same shape, so any column, API or library that accepts a UUID accepts either. The version is the first digit of the third group: 4 or 7.

v7 IDs sort in the order they were made

Three of the 100,000 v7 IDs generated in one burst:

01a1c946-61fe-76d5-9a9e-e4ee6505619c
01a1c946-61ff-7383-97c1-510e6efbd79c
...
01a1c946-6438-7826-92af-8bf83e92dda4

Sorting all 100,000 as plain strings gave exactly the order they were created in; sorting ten v4 IDs did not. The burst produced up to 247 IDs in the same millisecond. To keep those in order too, the generator uses 12 of the random bits as a counter that starts at a random value each millisecond, one of the methods RFC 9562 describes.

Why the database cares: 200,000 rows measured

Testv4 keysv7 keys
MySQL: primary key size (pages)1,123675
MySQL: table data size17.5 MB10.5 MB
MySQL: insert time, 2 runs10.5 s, 11.0 s10.0 s, 9.7 s
SQLite: insert time, 3 runs1.79 s, 1.71 s, 1.74 s0.39 s, 0.40 s, 0.40 s
SQLite: file size (pages)1,9431,995

InnoDB stores a table in primary key order. New v7 keys always go at the end, so pages fill up one after another. Random v4 keys land anywhere, pages split, and many stay part-empty: the same rows needed 1,123 pages instead of 675. A bigger index means more memory and more disk reads once the table no longer fits in the buffer pool. Here everything fitted in memory, which is why the insert times were close; on a large table the gap grows.

SQLite (a WITHOUT ROWID table keyed by the UUID) ended up with nearly the same file size, but inserting in key order was more than four times faster than inserting at random positions.

In MySQL, store UUIDs as BINARY(16) rather than CHAR(36): the test used BINARY(16), and text keys are more than twice as long, which makes every index on the table larger.

CREATE TABLE orders (
  id BINARY(16) PRIMARY KEY,   -- 16 bytes instead of CHAR(36)
  ...
);
INSERT INTO orders (id, ...) VALUES (UNHEX(REPLACE(?, '-', '')), ...);

The trade-off: v7 tells everyone when it was created

The first 12 hex digits of a v7 ID are a timestamp anyone can read:

const hex = id.replace(/-/g, "").slice(0, 12);
new Date(parseInt(hex, 16)).toISOString();
// "2026-11-11T09:30:00.123Z" for an ID created at that moment

That's harmless for most records, but think twice where the creation time is sensitive or where IDs are public and someone could count how many you create per day: password reset tokens, invitation links, IDs of user accounts. For those, use v4. And for secrets such as session tokens or API keys, don't rely on a UUID at all; use a dedicated random token generator.

Which one to use

  • Database primary keys, event and log IDs, anything listed newest first: v7.
  • Public identifiers where the creation time should stay private: v4.
  • An existing system that already uses v4: keep it unless the index size is a real problem; mixing versions in one column works, but loses v7's ordering for the old rows.
  • Secrets and tokens: neither; use a token made of random bytes.

Generating them in code

// v4: built into Node.js and every modern browser
crypto.randomUUID();

// v7: the uuid package (tested with uuid 11.1.1)
import { v7 as uuidv7 } from "uuid";
uuidv7();

According to their documentation, Python 3.14 added uuid.uuid7() and PostgreSQL 18 added a uuidv7() function. Those weren't tested here; check that your version has them before relying on them.

FAQ

Can two v7 UUIDs collide?

Only if two are made in the same millisecond with the same random bits. With up to 74 random bits per millisecond that is extremely unlikely, and a generator with a counter also keeps IDs from one process unique within the millisecond. None of the 100,000 IDs in the test repeated.

Is v7 slower to generate than v4?

Not in a way that matters: both need 16 random bytes and a little formatting. Generating the 100,000 test IDs took a fraction of a second.

What happened to v6?

v6 is a reordered version of the old time-based v1. RFC 9562 recommends v7 instead for new systems.

Are these UUIDs generated on a server?

No. The UUID generator runs in your browser with the browser's secure random number generator.

Sources