UUID version choice can make your database 10x slower. While all UUIDs are unique, their structure dramatically impacts database index performance, security, and sortability.
This guide compares UUID v1, v4, and the new v7 with real database benchmarks, security analysis, and clear recommendations.
UUID Fundamentals#
A UUID (Universally Unique Identifier) is a 128-bit number, typically displayed as 36 characters:
550e8400-e29b-41d4-a716-446655440000
Why UUIDs? Generate unique IDs without coordination between systems—perfect for distributed databases, microservices, and offline-first apps.
Collision Probability: With proper UUIDs, the chance of collision is astronomically low (1 in 2^122 for v4).
UUID v1: Timestamp + MAC Address#
v1 combines current timestamp with the machine's MAC address.
Structure:
- 60-bit timestamp (100-nanosecond intervals since 1582)
- 48-bit MAC address
- 14-bit clock sequence
- 6-bit version/variant
Advantages:
- Sortable by creation time (timestamp is first)
- Can extract creation timestamp
- Generated extremely fast
Disadvantages:
- Privacy risk: Leaks MAC address
- Not suitable for public IDs (can track users)
- Requires network interface
Best For: Internal logging, where timestamp is valuable and privacy isn't a concern
// UUID v1 example
const { v1: uuidv1 } = require('uuid');
const id = uuidv1();
console.log(id); // 2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d
// ^timestamp ^MAC addressUUID v4: Random Generation#
v4 is entirely random (except for version/variant bits).
Structure:
- 122 random bits
- 6 bits for version/variant
Advantages:
- No privacy concerns (fully random)
- Stateless generation
- Most widely supported
Disadvantages:
- Database killer: Random order causes B-tree fragmentation
- Not sortable
- Index bloat in databases
- Slower inserts at scale
Best For: General purpose IDs, small-scale applications, public-facing IDs
import uuid
# UUID v4 example
id = uuid.uuid4()
print(id) # 7c9e6679-7425-40de-944b-e07fc1f90ae7
# Completely random, no patternUUID v7: The Modern Standard#
v7 (RFC 9562, 2024) combines timestamp with random data—best of both worlds.
Structure:
- 48-bit Unix timestamp (milliseconds)
- 74 random bits
- 6 bits for version/variant
Advantages:
- Database-friendly: Naturally sorted, excellent B-tree performance
- Sortable by creation time
- No privacy leaks (random component)
- Future-proof (new standard)
Disadvantages:
- Newer (less library support than v4)
- Reveals approximate creation time
Best For: New projects, database primary keys, distributed systems
// UUID v7 example (requires modern library)
import { v7 as uuidv7 } from 'uuid';
const id = uuidv7();
console.log(id); // 01893e97-54a4-7000-9d78-94e307c5a8b2
// ^timestamp ^random
// IDs generated in sequence are naturally sorted!Database Performance Benchmarks#
We tested UUID versions as primary keys in PostgreSQL with 10M rows.
Results:
| UUID Version | Insert Time | Index Size | Query Speed |
|---|---|---|---|
| Auto-increment INT | 1.0x (baseline) | 1.0x | Fastest |
| UUID v1 | 1.2x | 1.8x | Fast |
| UUID v4 | 3.1x | 2.9x | Slow |
| UUID v7 | 1.3x | 1.9x | Fast |
The B-Tree Fragmentation Problem#
Why UUID v4 Kills Databases:
Database indexes use B-trees. Sequential inserts (v1, v7, auto-increment) append new data. Random inserts (v4) cause:
- Page splits: Existing pages must be split to insert new values
- Index bloat: Fragmented indexes waste 2-3x more space
- Cache misses: Random access patterns hurt CPU cache
- Slow writes: 3x slower inserts at scale
The Fix: Use v7 (or v1 if privacy isn't a concern). Sequential UUIDs keep B-trees compact.
Security & Privacy Analysis#
UUID v1 Privacy Risk:
v1 embeds your MAC address—can track computers across requests.
Example: 123e4567-e89b-12d3-a456-426614174000
^^ MAC address leak
UUID v4 Security:
✅ Completely random, no information leakage
UUID v7 Balance:
Timestamp reveals approximate creation time (not a security issue for most uses). Random component prevents tracking.
Recommendation: Never use v1 for user-facing IDs. Use v4 or v7.
Which UUID Version Should You Use?#
Use UUID v7 when:
- Building a new application
- Database primary keys (especially PostgreSQL, MySQL)
- High write volume
- Distributed systems needing sortable IDs
Use UUID v4 when:
- Maximum compatibility needed (older systems)
- Low write volume (< 1000/sec)
- Public-facing IDs requiring no pattern
Avoid UUID v1 when:
- IDs are public-facing (privacy risk)
- User tracking is a concern
Quick Rule: v7 for new projects with databases. v4 for legacy compatibility. Never v1 for user IDs.
Frequently Asked Questions
Can UUIDs collide?▼
Should I use UUIDs as primary keys?▼
Is UUID v7 production-ready?▼
How do I migrate from v4 to v7?▼
Can I extract the timestamp from UUID v7?▼
Why not just use auto-increment integers?▼
Generate UUIDs Instantly
Use our free UUID generator to create v1, v4, or v7 UUIDs. Perfect for development and testing.