HeadlinesBriefing favicon HeadlinesBriefing.com

SQLite suffers 10x slowdown with random UUID primary keys

Hacker News •
×

Using random UUIDs as primary keys in SQLite triggers severe slowdown, a new Hacker News post demonstrates. The author benchmarks a standard integer rowid against UUID4 and UUID7 in tables built with and without the implicit rowid. Inserting ten million rows shows the unordered UUID4 inserts run ten‑to‑twelve times slower than the integer baseline, and highlights why many ORM defaults may be unsuitable.

The test creates a plain table with an INT primary key, then repeats the load using a BLOB primary key defined WITHOUT ROWID. Profiling with a diffgraph reveals most time spent balancing the B‑tree and performing reads and writes. Resulting page faults dominate CPU usage, confirming the theoretical cost of random inserts. The unordered nature of UUID4 forces SQLite to constantly rebalance, inflating I/O.

Switching to time‑ordered UUID7 cuts the penalty dramatically; the same ten‑million‑row run averages 1.25 seconds per batch, only marginally slower than the integer case despite the primary key doubling in size. The experiment underscores that UUID choice directly impacts SQLite’s clustered index performance, and developers should prefer ordered identifiers when high insert rates matter in production workloads.