Home›Practice›Watch an index grow
Course · Inside one serverLesson 9 of 330 read
Practice · Watch an index grow

Watch an index grow

Why do random UUID keys make inserts slower?

An index is a tree of pages, kept in key order. Insert rows below and watch where each key lands, when a page fills up and splits, and how full the pages stay. Then switch from sequential ids to random UUIDs and do it again.

You insert 300 rows with sequential ids, and 300 with random UUIDs. Which needs more pages, and roughly how many more?

Random UUIDs need roughly a quarter to three-quarters more pages, depending on what you compare against. Notebook 10 measured 38.4 MB for a UUIDv4 index against 21.4 MB for bigint. They split far more often in the middle of the tree. Sequential ids only ever split the last page, so every page they leave behind is full. Random keys split pages in the middle, and both halves stay part-empty. Press "Compare all three key types" below to see it.

What to notice

Four things the picture shows

Real pages are much bigger. Here a page holds 3 to 8 keys so you can see splits happen. A real 8 KB or 16 KB page holds hundreds of keys, so a table of millions of rows is only three or four levels deep. Real engines also leave a little room when they fill the last page: PostgreSQL B-tree indexes leave 10% free by default (fillfactor 90), and InnoDB tries to leave 1/16 of the page free. Tables default to fillfactor 100, so a table page is packed full.

Where this matters next: MySQL · InnoDB for the clustered index, why column order matters in an index, and the Query coach, which flags a UUID string primary key in InnoDB.

Connected

Where this shows up

The same mechanism, keys and ids, indexes and plans, elsewhere on the site: in real incidents, in measurements, and in designs.

Part 2 of 4 · Practice · 9 pagesAll pages

Can I see it happen? Simulations, an index you build yourself, a real PostgreSQL in your browser, experiments run on real servers, and tools for your own database: a query coach, an EXPLAIN reader, a config checker and a health check.