The perils of UUID primary keys in SQLite
- Databases
- Infrastructure
- Programming
The post benchmarks several SQLite schema choices for primary keys and shows why random UUIDs are a bad fit for SQLite’s storage model. The key distinction is not just "UUID versus int". It is whether inserts land in order or spray across a B-tree. In SQLite, an `INTEGER PRIMARY KEY` aliases the hidden `rowid`, so inserts are append-like and compact. A `WITHOUT ROWID` table keyed by UUIDv4 forces the table itself to be ordered by a random 128-bit value, which means more page splits, larger indexes, and much worse insert throughput. The author updated the post to fix a rowid detail and added a rowid-plus-secondary-UUID case, which still showed a penalty for random UUIDv4 because the secondary index is random even if the base table stays append-ordered.
If you use SQLite as a local or single-writer store, default integer rowids are still the safe baseline. If you need globally generated IDs for sync, offline creation, or distributed writes, prefer UUIDv7 or keep a local rowid for joins and add a separate UUID column for external identity.
- andersmurphy.com
- Discuss on HN