4

Storing unique v4 uuids in db, collision detect or not?

Comments
  • 5
    just add a primary key on the uuid field and you will get an exception, if two uuid are the same.

    Edit: And you get a faster search. What DBMS do you use?
  • 1
    no need. uuid has very low chance of collision.
  • 4
    @magicMirror "low chance" means "still a chance."
    Admittedly small, but the debugging to find the issue in a year or so would not be.
  • 5
    https://wikiwand.com/en/...

    "
    For example, the number of random version-4 UUIDs which need to be generated in order to have a 50% probability of at least one collision is 2.71 quintillion, computed as follows:[14]
    <skipped>
    This number is equivalent to generating 1 billion UUIDs per second for about 85 years, and a file containing this many UUIDs, at 16 bytes per UUID, would be about 45 exabytes, many times larger than the largest databases currently in existence, which are on the order of hundreds of petabytes
    "

    I think your website is gonna be fine..

    @Root FYI
  • 3
    @netikras Most of what makes a UUID unique is an encoded timestamp. If you generate multiple UUIDs simultaneously, the timestamp portion would (obv) be identical, significantly increasing the collision chance.

    Still a low chance, but worth considering depending on scale/throughput.
  • 1
    @Root sure, because 2bln IDs per second is something likely to happen and needs to be taken into account :) FK (unique) constraints won't protect us here..
  • 0
    If it should be unique...

    Use as PK or add an unique index to it.

    Mostly for the optimizer, but to make and leave an clear intent of what the column does.
  • 0
    @Root as I said: low chance. I will take that risk. Others don't belive in hash functions. 🤷‍♂️
  • 1
    @Root that was an old version. I think v2, that argumebt was the cause for the pure random v4
Add Comment