UUID v4 vs UUID v7 vs NanoID: choosing the right database ID

Compare random UUID v4, time-ordered UUID v7, and URL-safe NanoID formats for SQL primary keys, distributed tracing, and bulk ID generation.

Generate UUIDs Bulk ID & MAC Generator

Auto-increment integers (1, 2, 3...) make SQL inserts fast, yet expose row counts, leak business volume, and create primary-key collisions when merging microservices offline. Generating distributed unique identifiers solves coordination—provided you pick the format suited to your storage engine and URLs.

Here is how UUID v4, UUID v7, and NanoID compare in production systems.

Side-by-Side Comparison

Identifier Format & Length Entropy Database B-Tree Locality Ideal Fit
UUID v4 36 chars (xxxxxxxx-xxxx-4xxx-...) 122 random bits Poor (random index splits at huge scale) Correlation IDs, opaque object tokens, session IDs
UUID v7 36 chars (48-bit timestamp + random tail) 74 random bits + ms timestamp Sequential / Append-friendly PostgreSQL / MySQL primary keys, event logs
NanoID (21 chars) 21 URL-safe chars (A-Za-z0-9_-) ~126 bits Random unless prefixed Share links, short invite codes, React keys, URLs

Generate standards-compliant IDs immediately inside your browser using our UUID Generator.

UUID v4 GeneratorGenerate one UUID v4 or a batch of five, using the browser’s native crypto.randomUUID.Generate UUIDs

When Randomness Hurts SQL Indexes (UUID v4 vs. v7)

In PostgreSQL, InnoDB (MySQL), and SQLite clustered or secondary B-tree indexes, inserting purely random UUID v4 strings forces the database to jump across arbitrary index pages on disk rather than appending neatly to the rightmost leaf page. Over tens of millions of rows, random insertions amplify write IO and index bloat.

RFC 9562 UUID v7 solves this cleanly by placing a 48-bit Unix millisecond timestamp in the most significant bits followed by cryptographic random bits:

  • New rows sort chronologically without extra created_at composite keys
  • Client nodes and distributed workers still mint globally unique keys with zero central lock server

Need Compact URL Slugs, Test Fixture Batches, or MACs?

When 36 hyphenated UUID characters look clunky in URLs like /share/... or test-fixture seeders need thousands of mock rows quickly, switch to URL-safe alphanumeric tokens or batch generators. Open our Bulk ID & MAC Generator to mint bulk UUIDs, ULID/NanoID-style tokens, or randomized locally administered (02:... / unicast) test MAC addresses cleanly offline.

Tools used in this guide

Free, and they run in your browser — nothing is uploaded. See every tool on the Crypto Tools page.

Frequently asked questions

Why is UUID v7 preferred over UUID v4 for SQL primary keys?

UUID v4 is completely random, causing out-of-order B-tree index inserts and page fragmentation on huge tables. UUID v7 embeds a 48-bit Unix millisecond timestamp prefix so new IDs insert chronologically like auto-increment integers while staying globally unique.

How likely is a collision with 128-bit UUID v4?

With 122 random bits in UUID v4, generating roughly 2.71 quintillion IDs yields a 50% birthday-paradox collision probability—collision risk in normal application traffic is astronomically negligible when backed by a CSPRNG.

When is NanoID better than UUID?

Standard 21-character NanoID uses a URL-safe 64-character alphabet (~126 bits of entropy) while staying much shorter than a 36-character hyphenated UUID, making it cleaner in public URLs, share links, and API slugs.

Are generated UUID batches or test MAC addresses logged anywhere?

No. Single and bulk identifier generation runs strictly client-side in your browser using the Web Crypto API.