Skip to content
toolbelt
Back to Guides

UUIDs in a Database: The Real Tradeoffs

By ·

Updated

Random UUIDs avoid collisions and wreck your index performance. The version you pick has consequences.

What is this tool?

A UUID is a 128-bit identifier, written as 32 hex digits with hyphens. Version 4 is random; versions 1, 6 and 7 embed a timestamp. That difference matters more than most people realise.

How it works

Version 4 uses a cryptographically secure random source in your browser, then sets the version and variant bits so the result is a well-formed v4 UUID.

Example

550e8400-e29b-41d4-a716-446655440000

Version 4, variant 1, generated from 122 random bits.

When to use it

Anywhere you need an identifier that will not collide across machines without coordination — primary keys, idempotency keys, correlation IDs in logs.

Optimization Tips

Random v4 keys as an index are expensive in some databases, because each insert lands in a random part of the B-tree. UUIDv7 is time-ordered and much friendlier to indexes. If you are choosing now, choose v7; if you already have v4 primary keys, that is fine at small scale and painful at large scale.

Frequently Asked Questions

Can two UUIDs collide?
In practice, no. At a billion per second for a century you would still expect essentially none.
Are these truly random?
They come from the browser crypto API, which is a CSPRNG. Not Math.random.
Should I use them in URLs?
Yes, v4 leaks nothing about your row count, unlike sequential integers.
What about size?
A UUID is 16 bytes binary but 36 characters as text. Store it as a native uuid or binary column, not a varchar.

Ready to try it out?

Put this knowledge into practice with our free tool.

Go to Tool

Related tools