3 ms·
Because you don't want your record IDs to be perfectly sequential data. You only care about them being sequential enough that your database engine will write t
by piaste 5y ago
Because you don't want your record IDs to be perfectly sequential data.
You only care about them being sequential enough that your database engine will write them down quickly and efficiently (for engines like InnoDB or MSSQL which have such behaviour). But as a developer you typically want them to be random so that you can generate them in a decentralised manner, prevent guessing or accidental joins, safely merge records from multiple tables, etc.
Sequential UUIDs usually preserve enough randomness for the latter (as long as you don't do stuff like generating a trillion IDs exactly at midnight), while providing enough sequentiality for the former.
- roenxi 5y agoBut now the ID has a random concatenation of data in it, like the MAC address of the node that inserted it into the database and a timestamp. It'd be much more orderly for each node to have a sequential ID, the ID of the node that created it, a timestamp when it was created and a real UUID v4 for if someone wants to prevent collisions. Same data, but the primary key on an individual node is a lot smaller (half the size) and the metadata is available for use if someone wants it. There is natural room to build up namespaces and such. It is better to keep these observations separate instead of munging them all into the record ID.
- piaste 5y agoI 100% agree that metadata like timestamps and machine IDs, if you care about them, should have their own columns. I only disagree with: > It'd be much more orderly for each node to have a sequential ID I've outlined some reasons above why a random ID has advantages over a sequential one. I run Postgres in prod so I can use purely random UUIDv4, but for those running mysql or mssql, UUIDv6 and the like are a useful compromise between sequentiality and randomness. The fact that they happen to use timestamps as the source of sequentiality is only an implementation detail.