UUID v4 vs UUID v7: Which Should You Use as a Primary Key?
UUID v7 sorts by time, which makes it a faster primary key than random v4. What’s inside each, when to use which, and how to generate them today.
Sooner or later every database table needs a primary key, and the choice usually comes down to an auto-incrementing integer or a UUID. For years the UUID answer meant version 4: 122 random bits that never collide. Now there's a newer option, UUID version 7, designed specifically for database keys.
This guide explains what's inside a UUID, how v4 and v7 differ, why v7 is often faster as a primary key, and when you should still use v4 (or a plain integer).
What is a UUID?
A UUID (Universally Unique Identifier, also called a GUID in the Microsoft world) is a 128-bit number, usually written as 32 hexadecimal digits in five groups:
01a186fe-adc0-7b31-9d27-4f8a1c3e5b60
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
- M, the first digit of the third group, is the version:
4or7here. - N, the first digit of the fourth group, holds the variant. For standard UUIDs it's
8,9,aorb.
Anything can generate a UUID without asking a central server, and the chance of two ever colliding is so small you can ignore it. That's the whole appeal: a client, a server and a background job can all create IDs independently.
UUIDs are defined by RFC 9562, published in 2024, which replaced the older RFC 4122 and added versions 6, 7 and 8.
UUID v4: pure randomness
A version 4 UUID is 122 random bits, plus the six bits that mark the version and variant.
3b9f7c2e-8d41-4a6f-b0e3-5c27d91f8a44
- Unpredictable. Knowing one v4 UUID tells you nothing about any other.
- Collision-proof in practice. You'd need to generate around 2.7 quintillion of them to have a 50% chance of a single duplicate.
- No information leaks: no time, no machine, no sequence.
The downside only shows up at scale: because v4 values are random, every new row lands at a random place in the primary key's index.
UUID v7: time first, then randomness
A version 7 UUID starts with the time, in milliseconds since the Unix epoch, and fills the rest with random bits (or a counter that keeps IDs in order within the same millisecond):
01a186fe-adc0-7b31-9d27-4f8a1c3e5b60
└─────┬─────┘ ↑
│ └ version 7 (the rest is random, apart from two variant bits)
└ 48-bit timestamp, in milliseconds
- Sortable by creation time. IDs created later sort later, as text and as bytes.
- Still unique without coordination: 74 bits of randomness per millisecond is plenty.
- The creation time is readable from the ID itself, which is handy for debugging.
You can paste one into the UUID generator on this site to read its timestamp, or generate v4 and v7 UUIDs in bulk.
Why v7 makes better primary keys
Most databases store a table's primary key in a B-tree index, kept in sorted order. Where a new key lands in that tree matters:
- Random keys (v4) land anywhere. The database has to load an old index page from disk to insert into it, pages split to make room, and the index ends up full of half-empty pages. Once the index no longer fits in memory, inserts slow down noticeably.
- Time-ordered keys (v7) always land at the end, like an auto-increment integer. The "hot" pages stay in memory, and pages fill up neatly.
The effect is small on a table with a few thousand rows, and real on one with hundreds of millions. Write-heavy systems, and databases that cluster rows by primary key (MySQL's InnoDB, SQL Server), feel it most.
Time order has other nice side effects:
- "Newest first" queries can use the primary key's order.
- Logs and events with v7 IDs sort into the order they happened.
- Pagination by "everything after this ID" works naturally.
When to stick with v4
UUID v7 isn't automatically better. The timestamp is a feature for databases, but it's information you're publishing:
- The creation time leaks. Anyone who sees a v7 ID can tell when the record was created. For an order or a user account, that might reveal how fast you're growing, or when someone signed up.
- IDs are partly guessable. The random part is still large, so you can't guess an exact ID. But never rely on any UUID being secret. Check permissions on every request, whatever the ID.
- Security tokens need all the randomness they can get. Password reset links, API keys, invitation codes and session IDs should be fully random. Use a dedicated random token generator (a v4 UUID is fine; 32 random bytes is better).
A common pattern is to use v7 internally as the primary key, and expose a separate random ID, or a slug, in URLs.
What about auto-increment integers?
Plain integers (SERIAL, IDENTITY, AUTO_INCREMENT) are still a great choice:
- Smaller: 8 bytes instead of 16, so indexes and foreign keys are smaller.
- Readable: "order 10432" is easier to say on the phone than a UUID.
- But they need the database to hand them out, so you can't create an ID before inserting the row, and merging data from several databases is painful.
- And they leak counts: if your newest invoice is number 1043, that tells people something.
Rule of thumb: integers for small, single-database apps; UUID v7 when IDs are created in many places, in offline clients, or across services.
Generating UUID v7 today
Support has arrived quickly:
PostgreSQL 18
PostgreSQL 18 (September 2025) has a built-in uuidv7() function:
CREATE TABLE orders (
id uuid PRIMARY KEY DEFAULT uuidv7(),
customer_id uuid NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
Older versions have gen_random_uuid() for v4. For v7 there, generate IDs in your application or use an extension.
JavaScript and TypeScript
The browser and Node.js have crypto.randomUUID(), which makes v4 only. For v7, use the uuid package:
import { v4 as uuidv4, v7 as uuidv7 } from 'uuid';
uuidv4(); // '3b9f7c2e-8d41-4a6f-b0e3-5c27d91f8a44'
uuidv7(); // '01a186fe-adc0-7b31-9d27-4f8a1c3e5b60'
Python 3.14
Python 3.14 added uuid.uuid7() to the standard library (alongside uuid6() and uuid8()):
import uuid
uuid.uuid4() # random
uuid.uuid7() # time-ordered
On older Python versions, the uuid6 package on PyPI provides uuid7().
Reading the timestamp out of a v7 UUID
The first 12 hex digits are the time in milliseconds:
function uuidv7Time(id) {
const ms = parseInt(id.replace(/-/g, '').slice(0, 12), 16);
return new Date(ms);
}
uuidv7Time('01a186fe-adc0-7b31-9d27-4f8a1c3e5b60'); // 2026-10-29T09:30:00.000Z
That's useful for debugging ("when was this row created?"), but don't use it as your only timestamp column. Keep a real created_at: it's clearer, and it's what your queries and reports should use.
Storing UUIDs efficiently
- Use the database's native UUID type (
uuidin PostgreSQL,UNIQUEIDENTIFIERin SQL Server). It takes 16 bytes. - In MySQL, store UUIDs as
BINARY(16), notCHAR(36). The text form is more than twice the size, in the table and in every index that includes it. - Keep the lowercase, hyphenated form in APIs and logs. It's the standard text format, and it's what people expect to search for.
Summary
| UUID v4 | UUID v7 | Auto-increment | |
|---|---|---|---|
| Created without a database | Yes | Yes | No |
| Sorts by creation time | No | Yes | Yes |
| Index-friendly inserts | No | Yes | Yes |
| Reveals creation time | No | Yes | Roughly (via order) |
| Size | 16 bytes | 16 bytes | 4–8 bytes |
| Best for | Random tokens, public IDs | Primary keys, events, logs | Small single-database apps |
For a new table that needs UUIDs, v7 is the better default for the primary key. Keep v4 (or longer random tokens) for anything that must not reveal when it was made.
Want to see the difference? Generate a batch of each in the UUID generator: the v7 ones line up in order, and the v4 ones don't.