UUID v4 vs v7: Which ID to Use for Your Database
How UUIDs are built, why random v4 keys can slow down database inserts, what time-ordered v7 fixes, and when a plain auto-increment number is the better choice.

Sooner or later every developer has to decide how records get their IDs. A UUID is the popular answer: you can create one anywhere, without asking the database, and it will not clash with anyone else’s. But not all UUIDs behave the same once you have millions of rows. This guide explains the format, the difference between v4 and v7, and when you do not need a UUID at all. You can create both kinds with the UUID generator and paste any existing one into its decoder.
What a UUID actually is
A UUID (Microsoft calls it a GUID) is a 128-bit number, usually written as 32 hexadecimal digits in five groups: 8-4-4-4-12. For example:
0192a8f4-7c3e-7b21-9d4a-3f6e2c1b8a90
Two positions tell you what kind it is. The first digit of the third group is the version (here 7). The first digit of the fourth group shows the variant, and for standard UUIDs it is 8, 9, a or b. Everything else depends on the version. Uppercase and lowercase mean the same thing, and braces around it, as in {…}, are just Microsoft’s display style.
v4: random and private
A version 4 UUID is 122 random bits plus the version and variant markers. It is supported by every language and database and reveals nothing about when or where it was made. Collisions are not a practical concern: you would need to generate around a billion per second for about 85 years to reach a 50 percent chance of a single duplicate, as long as the generator uses a proper random source.
The weakness shows up in databases. Most primary keys are stored in a B-tree index that is kept in sorted order. New v4 values land at random places in that tree, so each insert touches a different page. On large tables this means more page splits, a bigger index, worse use of the cache and slower inserts. Databases that store rows in primary key order, such as MySQL with InnoDB, feel it the most.
v7: time first, then random
Version 7, standardised in RFC 9562 in 2024, starts with a 48-bit Unix timestamp in milliseconds, followed by random bits. New IDs sort after old ones, so inserts go to the end of the index, just like an auto-increment number, while you keep all the advantages of UUIDs.
| UUID v4 | UUID v7 | |
|---|---|---|
| Content | Random | Timestamp + random |
| Sortable by creation time | No | Yes (to the millisecond) |
| Index behaviour on insert | Scattered | Appends at the end |
| Reveals creation time | No | Yes |
That last row is the only real trade-off. Anyone who sees a v7 ID can read when it was created; the decoder in the generator shows it in a second. For an order or a document, that is usually harmless. For something like a user account where the signup date should stay private, prefer v4 for the public ID.
When an auto-increment is better
A plain integer that counts up is still a good choice in many cases:
- A single database creates all the rows, and nothing needs an ID before the insert.
- Storage matters: a
bigintis 8 bytes, a UUID 16, and every secondary index repeats the key. - People read or type the IDs, like invoice or ticket numbers.
UUIDs win when IDs are created in several places (mobile apps working offline, several services, merged databases), when you want to know the ID before saving, or when the ID appears in URLs. Sequential numbers there reveal how many orders you have and invite people to try /orders/1001, /orders/1002. A UUID does not fix missing access checks, but it stops casual guessing. A common compromise is an integer key inside the database and a UUID as the public identifier.
Common mistakes
- Storing UUIDs as text. 36 characters instead of 16 bytes. Use the native
uuidtype in PostgreSQL orBINARY(16)in MySQL. - Using a UUID as a secret. An ID is not a password reset token. Tokens should come from a dedicated secure random generator and expire.
- Expecting the same UUID for the same input. v4 and v7 are new every time. If you need a stable ID derived from content, that is a hashing job: name-based UUIDs (v5) use SHA-1 internally, and for file or text fingerprints the hash generator gives you a SHA-256.
- Mixing formats. Uppercase in one system, lowercase in another, with and without hyphens: normalise before comparing.
Our take
For a new table where you want UUIDs, pick v7 by default. You get the flexibility of UUIDs with index behaviour close to an auto-increment, and the timestamp is handy when debugging. Use v4 when the creation time must stay private. Do not migrate a working system from integers to UUIDs just because it feels modern: if one database creates all the rows and nobody outside sees the IDs, an auto-increment is simpler, smaller and perfectly fine.
Anthony has worked in IT for more than 17 years: office and field networks, servers, telephony, user support and security patching in international organisations. He writes about networking, security, developer tools and calculators.




