Skip to content
Control Plane Labs

UUID v7 vs v4 for Database Primary Keys

UUIDv7 sorts by creation time and inserted 2.2–2.8x faster than v4 in our SQLite test. How the formats differ, what v7 exposes, and when v4 still wins.

Control Plane Labs Staff

Published November 10, 2026

The UUID generator makes both kinds, plus the Nil UUID, and its inspector reads the version, variant and timestamp out of any UUID you paste. This guide covers how the two formats differ, what the ordering buys you in a database, and the cases where random v4 is still the right call.

What the generator does

It produces 1 to 100 UUIDs at a time in canonical, uppercase, braced or hyphen-free form. Version 4 values come from the browser’s crypto.randomUUID(), which MDN documents as a v4 UUID built with a cryptographically secure random number generator. No browser API emits v7 yet, so the tool assembles those values byte by byte from Date.now() and crypto.getRandomValues(). Everything runs in your browser and nothing is sent to a server.

How v4 and v7 are built

Both formats are defined in RFC 9562, which obsoleted RFC 4122 in May 2024. Each is 128 bits, with 4 version bits and 2 variant bits fixed. The difference is in the other 122 bits.

Field UUIDv4 (§5.4) UUIDv7 (§5.7)
Bits 0–47 random unix_ts_ms, big-endian Unix milliseconds
Bits 48–51 version 0100 version 0111
Bits 52–63 random rand_a, 12 bits: random or a counter
Bits 64–65 variant 10 variant 10
Bits 66–127 random rand_b, 62 random bits

The timestamp sits in the most significant bits, so v7 values sort by creation time as raw bytes or as strings, with no parsing. The RFC says this is deliberate: v6 and v7 “sort as opaque raw bytes without the need for parsing or introspection.”

Millisecond resolution is coarse, though. A service that creates thousands of IDs in one millisecond needs something extra to keep them in order, and §6.2 of the RFC offers three methods:

  • Method 1, fixed-length counter. Part or all of rand_a becomes a counter. It gets a random seed at each new millisecond and increments within it.
  • Method 2, monotonic random. rand_b is treated as a number and increased by a random amount for each ID in the same millisecond.
  • Method 3, extra clock precision. Up to 12 sub-millisecond timestamp bits go into rand_a.

Our generator uses Method 1. The 12-bit counter is seeded in the lower half of its range, which leaves its top bit as the rollover guard the RFC describes. If a single millisecond uses up the counter, the timestamp is moved forward by one millisecond, which the RFC also allows. Python 3.14’s uuid.uuid7() uses a 42-bit counter for the same purpose. The PostgreSQL 18 docs say its uuidv7() combines a millisecond timestamp, a sub-millisecond fraction and random bits, which is Method 3.

Our test: one million inserts into SQLite

RFC 9562 says random keys have “poor database-index locality” and that the difference “can be one order of magnitude or more.” We wanted a number we could reproduce, so on November 10, 2026 we ran a small test.

We inserted one million 16-byte keys into a SQLite 3.50.4 WITHOUT ROWID table, where the primary key B-tree holds the rows themselves. Keys came from Python 3.14.3’s uuid4() and uuid7(). They went in generation order, in 10,000-row transactions, with journaling off. Each configuration ran three times, and the table shows the median.

Page cache v4 insert time v7 insert time v7 speedup v4 file v7 file
8 MB (smaller than the table) 2.46 s 0.89 s 2.8x 26.7 MB 27.5 MB
128 MB (whole table fits) 1.99 s 0.89 s 2.2x 26.6 MB 27.5 MB

Two results stood out. First, v7 was faster even when the whole table fit in memory. Random inserts cost more than disk reads: each one walks to a different leaf page and dirties it. Second, the files came out almost the same size. SQLite’s rebalancing kept v4 leaf pages 90.5% full and v7 pages 87.5% full, so in this engine the cost of v4 is time spent, not wasted space. Other engines split pages differently, so measure your own database before quoting a size difference. The raw results list every run.

The test also checked ordering. We generated 100,000 v7 values, and their byte order matched their generation order exactly, so the counter worked.

This is one machine and an in-process database, so the absolute times won’t carry over to a networked server. The ratio is the useful part: it tells you which way the index is working.

When to use v7, and when to stay on v4

Choose v7 when the UUID is a primary key or another indexed column with heavy inserts. It’s also the better fit for sorting records by creation time without a separate created_at index, for IDs created across many services that should still merge in rough time order, and for systems moving off v1 or v6. RFC 9562 says implementations “SHOULD utilize UUIDv7 instead of UUIDv1 and UUIDv6 if possible.”

Stay on v4 when the creation time is sensitive. Anyone holding a v7 value can read when it was made: paste one into the UUID inspector and it shows the instant down to the millisecond. That matters for public user IDs, order numbers that reveal sales volume, and anything where timing helps an attacker correlate events. v4 also suits low-write tables where index locality doesn’t matter.

Don’t use either format as a secret. RFC 9562 §6.9 says UUIDs “MUST NOT be used as security capabilities”, meaning identifiers that grant access just by being known. A password reset link or an API key needs a purpose-built random token, not a UUID. If you need a UUID inside a security-sensitive operation, the RFC recommends v4, because v7 has only 62 random bits once the timestamp and counter are filled.

Switching a service to v7 in three steps

  1. Pick your generator. PostgreSQL 18, released September 25, 2025, added uuidv7() along with a uuidv4() alias, per its release notes. Python has uuid.uuid7() from 3.14. In the browser and in Node.js, crypto.randomUUID() only makes v4, so you need a library or a short function like the one behind our generator.
  2. Keep the column type. A v7 value is still a valid UUID, so uuid and BINARY(16) columns don’t change, and existing v4 rows can stay. New keys sort after old ones from the first v7 insert onward. Old v4 rows keep their random positions in the index.
  3. Check what you generate. Paste a sample into the generator’s inspector and confirm version 7, variant 10 and a timestamp close to now. In PostgreSQL, uuid_extract_version() and uuid_extract_timestamp() make the same checks in SQL. A timestamp years off usually means the clock source was in seconds, not milliseconds.

Frequently asked questions

Is UUIDv7 better than UUIDv4?+
For database keys, usually yes. UUIDv7 sorts by creation time, so inserts stay at the end of the index, and in our SQLite test one million v7 keys inserted 2.2 to 2.8 times faster than v4. UUIDv4 is better when the creation time must stay hidden, because a v7 value exposes it to anyone who has the ID.
Can two UUIDv7 values collide?+
In practice, no. Two v7 values from different generators collide only if they share the same millisecond and the same 74 bits of counter and random data. Within one generator, the counter in RFC 9562 section 6.2 keeps values unique and in order inside a single millisecond.
Does UUIDv7 reveal when a record was created?+
Yes. The first 48 bits are the Unix time in milliseconds. Any UUID inspector, including the one on our UUID generator page, or PostgreSQL's uuid_extract_timestamp(), turns a v7 value back into a timestamp. Use v4 for identifiers where that would leak something.
Can I mix v4 and v7 UUIDs in the same column?+
Yes. Both are valid 128-bit UUIDs with the same variant bits, so the column type doesn't change. Existing v4 rows keep their random positions in the index, and new v7 rows sort after them by time. Code that reads timestamps out of IDs has to check the version first.
Is a UUID safe to use as a password reset token?+
No. RFC 9562 says UUIDs must not be used as security capabilities, meaning identifiers that grant access just by being known. Generate a dedicated random token from a cryptographically secure source, store a hash of it, and give it a short expiry.
Does PostgreSQL support UUIDv7 natively?+
Yes, from PostgreSQL 18, released September 25, 2025. It added uuidv7(), which can take an optional interval to shift the timestamp, and a uuidv4() alias for gen_random_uuid(). Earlier versions need an extension or application-side generation.

Tags: #uuid, #uuidv7, #databases, #primary-keys, #rfc-9562