|
|
|
|
|
by hot_gril
797 days ago
|
|
UUIDs should not be used as database primary keys unless the DBMS recommends it or you have a well-studied special reason for it. Postgres and MySQL are meant to use bigserial by default, even Citus. Some special sharded DBMSes like Spanner need non-sequential pkeys, but even Spanner explicitly tells you to use uuid4 because k-sortable keys cause hotspotting: https://cloud.google.com/spanner/docs/schema-design#uuid_pri... |
|
But if I could go back 25 years and only give myself one bit of advice, it would be to use UUIDs as the primary key. Because in a different context to raw performance, it offers a lot of advantages.
While there are advantages in numerous areas, I'll focus on one for this post. The area of distributed data.
We started by running a database on prem. Each branch or store got their own db. 15 years later always-on networking happened. 15 years after that, all businesses have fibre.
So now all the branches use a giant shared online database. With merged data. Uuid based this task would be trivial. Bigint based, yeah, it's not.
Along the same timeline data started escaping from our database. It would go to a phone, travel around a bit, change, get new records, then come home. Think lots of sales folk, in places without reception, doing stuff.
So you're right in the context of a single database (cluster) which encompasses all the data all the time.
But in the context where data lives beyond the database, using uuids solves a lot of problems.
There are other places as well where uuids shine.
So as with most advice when it comes to SQL, I'd add "context matters".