Skip to content

UUID v7 and Binary Storage for Primary Keys #203

@rolandbeisel

Description

@rolandbeisel

Migrate from UUID v4 (stored as VARCHAR(255)) to UUID v7 (stored as BINARY(16) or native UUID type) for all primary key columns to improve database performance.

Motivation

Current Implementation

id VARCHAR(255) NOT NULL PRIMARY KEY
  • UUID v4: Completely random, no time-based ordering
  • VARCHAR(255): 36 bytes + length prefix, string comparisons

Problems with UUID v4 + VARCHAR

  1. Poor B-Tree Index Performance

    • Random UUIDs scatter inserts across the entire B-Tree
    • Causes frequent page splits and index fragmentation
    • More disk I/O, worse cache utilization
  2. Storage Overhead

    • VARCHAR stores 36 characters (+ overhead) vs. 16 bytes for binary
    • String comparisons are slower than binary comparisons
    • Larger index size = more memory needed
  3. No Natural Ordering

    • UUID v4 has no relationship to creation time
    • Requires separate created_at column and index for time-based queries

Proposed Solution

UUID v7 (RFC 9562) provides:

  • Time-ordered: Contains millisecond-precision Unix timestamp
  • Monotonically increasing: New IDs are always "greater" than older ones
  • Sequential inserts: Append-only behavior in B-Tree indexes

Expected Performance Gains

Metric Improvement
Insert throughput 20-40% faster
Index size ~50% smaller
Primary key lookups 10-20% faster
Storage space ~50% less for ID columns

Alternatives Considered

UUID v7 with VARCHAR storage (no schema change)

Pros:

  • No breaking change
  • Still improves insert performance (sequential ordering)

Cons:

  • Misses storage and comparison performance gains
  • Half-measure

Verdict: Could be done in v1.x as a quick win, but full migration should happen in v2.0.
| Storage space | ~50% less for ID columns |

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions