Why instead of a numeric ID
- Does not reveal the number of records in a table (
/users/1 vs /users/550e8400-…) - Can be generated on the client or in a distributed system without a DB round-trip
- No conflicts when merging data from multiple databases
Versions
- UUID v4 — fully random. Most common
- UUID v7 — includes a timestamp, so it sorts chronologically. Better for DB primary keys (less index fragmentation)
Drawback
UUID v4 as an InnoDB primary key causes B-Tree index fragmentation due to random insert order. UUID v7 or ULID solve this problem.