{"canonical_url":"https://engineer.company/portfolio/quicker-answers-from-a-better-choice-of-key/","content_markdown":"# Time‑ordered keys for a growing data model\n\nAdopted UUID v7 time‑ordered identifiers (PostgreSQL 18) as entity keys to reduce B‑tree index fragmentation and speed up queries.\n\n2025\n\n**Situation.** Every entity needs a unique id, and the reflex choice is a random UUID. The trouble is that random ids land all over a B‑tree index. Inserts scatter, the index fragments, and as tables grow both writes and range scans pay for it. On a platform meant to keep growing, that's a slow leak you'd rather not build in.\n\n**Task.** Keep the global uniqueness of a UUID but drop the fragmentation that comes with the randomness.\n\n**Action.** The standard became UUID v7, which is time‑ordered — the leading bits are a timestamp, so new rows sort into the index instead of peppering it. PostgreSQL 18 has this natively as uuidv7(), wrapped in a small uuid_generate_v7() function so the same call behaves cleanly on Azure's Flexible Server, then made the default for entity primary keys across the schema. Nothing exotic about it; it's the kind of decision that's cheap if you make it early and a pain to retrofit later.\n\n**Result.** UUID v7 became the configured key convention, giving new identifiers time ordering. Reduced fragmentation and faster queries were design aims; no before‑and‑after workload benchmark is recorded in this case.\n\n---\n\n- Role: Database Engineer\n- Categories: [Backend Engineering](https://data.engineer.company/categories/backend/), [Databases](https://data.engineer.company/categories/databases/), [PostgreSQL](https://data.engineer.company/categories/postgresql/), [Data Engineering](https://data.engineer.company/categories/data-engineering/), [Performance Tuning](https://data.engineer.company/categories/performance/)\n- Services: [Database Design \u0026 Modeling](https://data.engineer.company/services/database-design/), [Database Performance Tuning](https://data.engineer.company/services/database-performance/)\n\n\u003chttps://engineer.company/portfolio/quicker-answers-from-a-better-choice-of-key/\u003e\n","date_modified":"2026-11-15T19:47:44Z","description":"Adopted UUID v7 time-ordered keys in PostgreSQL 18, aiming to reduce B-tree fragmentation and improve inserts and range queries for entity tables.","formats":{"gemtext":"https://data.engineer.company/portfolio/quicker-answers-from-a-better-choice-of-key/index.gmi","gopher":"https://data.engineer.company/portfolio/quicker-answers-from-a-better-choice-of-key/gophermap","html":"https://data.engineer.company/portfolio/quicker-answers-from-a-better-choice-of-key/","json":"https://data.engineer.company/portfolio/quicker-answers-from-a-better-choice-of-key/index.json","markdown":"https://data.engineer.company/portfolio/quicker-answers-from-a-better-choice-of-key/index.md","pdf":"https://data.engineer.company/portfolio/quicker-answers-from-a-better-choice-of-key/index.pdf"},"kind":"page","language":"en","title":"Adopted UUID v7 keys to cut index fragmentation","url":"https://data.engineer.company/portfolio/quicker-answers-from-a-better-choice-of-key/","version":1}