HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects
MIT License
5.12k stars 520 forks source link

UUIDs are bad primary keys #136

Closed Koff closed 1 year ago

Koff commented 1 year ago

Some feedback about this like:

Defining a primary key can also go there. Potential candidate for that is the UUIDField

In Postgres, primary keys as UUIDs much slower in joins, and their indexes are a lot heavier. https://www.cybertec-postgresql.com/en/int4-vs-int8-vs-uuid-vs-numeric-performance-on-bigger-joins/

Primary keys should be an integer, and if needed, a separate UUID column with a unique constraint can be used to offer a customer facing id for an object.

krystofbe commented 1 year ago

People often mention ID generation as a bottleneck, but from my experience, it's rarely an issue outside of tech giants' scenarios. For my application and ETL processes, the advantages of UUIDs—especially avoiding DB roundtrips for ID generation—outweighed using bigint.

RadoRado commented 1 year ago

@Koff @krystofbe Thanks for bringing this topic up!

I'm really sorry for taking so long to respond.

I'd say I agree with the fact that unless explicitly needed, one should opt-out of using UUIDs as primary keys, especially if you are using Postgres.

For the sake of simplicity and using the good defaults provided by Django, I'll remove the mention of changing the primary key in that section, not to suade people towards that.

Cheers!