Skip to main content

UUID v4 vs UUID v7 vs ULID: Database Primary Keys Explained

Compare randomness, B-tree index locality, timestamp extraction, and string lengths across UUID v4, UUID v7, and ULID.

Updated 2026-11-28

UUID v4: pure randomness, fragmented indexes

UUID v4 places 122 random bits into a 128-bit identifier. Collision risk is negligible with a CSPRNG, making it safe for distributed generation without coordination. However, random distribution causes B-tree page splits and cache churn when used as a clustered primary key on large tables.

UUID v7: time-ordered standard (RFC 9562)

UUID v7 encodes a 48-bit Unix millisecond timestamp in the most significant bits followed by random bits. New rows append sequentially to database B-tree indexes while retaining standard 36-character UUID syntax and native PostgreSQL `uuid` column compatibility.

ULID: compact 26-character Base32

ULID combines a 48-bit millisecond timestamp with 80 random bits encoded in Crockford Base32 (`26` characters, URL-safe, case-insensitive, lexicographically sortable). Choose UUID v7 when your database has a native UUID type and ULID when compact string IDs are preferred.

Try the related tools