by zetalyrae · 2y ago · 0 comments
A much more salient concern for me is performance. UUIDv4 is widely supported but is completely random, which is not ideal for index performance. UUIDv7[0] is closer to Snowflake[1] and has some temporal locality but is less widely implemented.
There's an orthogonal approach which is using bigserial and encrypting the keys: https://github.com/abevoelker/gfc64
But this means 1) you can't rotate the secret and 2) if it's ever leaked everyone can now Fermi-estimate your table sizes.
Having separate public and internal IDs seems both tedious and sacrifices performance (if the public-facing ID is a UUIDv4).
I think UUIDv7 is the solution that checks the most boxes.
[0]: https://uuid7.com/
But it's not just the size of that one column, it's also the size of all the places that id is used as a FK and the indexes that may be needed on those FK columns. Think about something like a user id that might be referenced by dozens or even hundreds of FKs throughout your database.
!!!!
But those 5 other columns are not indexed.
---
There are three levels of database performance:
1. Indices and data fit in memory.
2. Indices fits in memory, data does not.
3. Neither indices not data fit in memory.
If you can do #1 great, but if you don't have that, fight like a madman for #2.
---
Doubling your index sizes is just makes it harder.
when x86-64 cpus were new the performance impact from switching to 64-bit pointers was so bad we had to create x32/ilp32 and the reason .NET still has "prefer 32-bit" as a default even today.
using 128-bit uuids as PKs in a database is an awful mistake
it's still the default in .net as of 4.8.1 (has been since it was introduced in 4.5 roughly coinciding w/java's pointer compression feature which is also still the default today)
Many libraries are straight up dropping NS2.0 TFM and overall community tends to perceive the request to still target it very negatively. Sure, Visual Studio still runs some in-process features on top of it, and so do Office add-ins, but it's an active work-in-progress to get rid of any new code having to be written while still targeting it.
So, in that regard, this setting does not matter.
Another variant of this approach: https://pgxn.org/dist/permuteseq/
It is also feasible to encrypt the value on display (when placing it in URLs, emails, &c):
https://wiki.postgresql.org/wiki/Pseudo_encrypt
This maintains many of the benefits of sequential indexes and does allow you to change the key. However, if the key is changed, it would break any bookmarks, invalidate anything sent in older emails -- it would have the same effect as renaming everything.
If you have a column that is used in many joins, there are performance reasons to make it as compact as possible (but not smaller).