UUID

UUID

UUIDs are just 128 bit pieces of data, that is displayed as (128/4) = 32 hexadecimal digits, like this : 2a8a7da6-8bc5-11ed-9228-0242ac140003

Pros

  • hide the real number of records that a table may have

Compare different version of the UUID

Version Features Using Scenario
v1 Uniqueness, orderable Specify the uuid from different device and create time
v3 Non-Random UUIDs, MD5 Hash function If the namespace and input name are the SAME, then you will get the SAME uuid
v4 Randomness, No inherent logic the entire uuid is random, irrespective of the time or the device
v5 Non-Random UUIDs, SHA1 Hash function If the namespace and input name are the SAME, then you will get the SAME uuid

UUID Example

UUID v1

Uniqueness, orderable (device id + time)

Device UUID v1
Computer A 5f7e563c-8bc4-11ed-bec0-aaaaaaaaaaaa
Computer A 5f7e62da-8bc4-11ed-bec0-aaaaaaaaaaaa
Computer A 5f7e68ca-8bc4-11ed-bec0-aaaaaaaaaaaa
Computer B 5f7e563c-8bc4-11ed-bec0-bbbbbbbbbbbb
Computer B 5f7e62da-8bc4-11ed-bec0-bbbbbbbbbbbb
Computer B 5f7e68ca-8bc4-11ed-bec0-bbbbbbbbbbbb

UUID V1

UUID v3

Non-Random UUIDsMD5 Hash function

Namespace Name Example UUID v3
5f7e563c-8bc4-11ed-bec0-0242ac140003 KJ Uuid::uuid3(’5f7e563c-8bc4-11ed-bec0-0242ac140003’, ‘KJ’) bb5fc59b-dda5-34dd-b777-a9219e068fa5
5f7e563c-8bc4-11ed-bec0-0242ac140003 Kay Uuid::uuid3(’5f7e563c-8bc4-11ed-bec0-0242ac140003’, ‘Kay’) 677dfb56-79c9-3de6-9c36-82114fcc0145
5f7e563c-8bc4-11ed-bec0-0242ac140003 Jay Uuid::uuid3(’5f7e563c-8bc4-11ed-bec0-0242ac140003’, ‘Jay’) 0edf8586-c0c6-33e2-94ee-e76ac5772e4a

UUID v4

Randomness, No inherent logic

Device UUID v4
Computer A 80f84d78-0c88-414c-b05b-dd17ba0ccb65
Computer A 4fc7e6bd-e15a-45f0-884d-bb4cbb0c596f
Computer A 31736994-1e92-4714-bd7f-bb3013e5dbfa
Computer B 39ff7491-6c66-4a7c-a705-15b1f2b2e138
Computer B 0bd9312c-849b-4c75-96f3-7e2845865da5
Computer B 45474299-f665-456a-900a-f972b2332953

UUID v5

Non-Random UUIDsSHA1 Hash function

Namespace Name Example UUID v5
5f7e563c-8bc4-11ed-bec0-0242ac140003 KJ Uuid::uuid5(’5f7e563c-8bc4-11ed-bec0-0242ac140003’, ‘KJ’) ef6ece10-d825-5a02-b6fa-f56962cd7041
5f7e563c-8bc4-11ed-bec0-0242ac140003 Kay Uuid::uuid5(’5f7e563c-8bc4-11ed-bec0-0242ac140003’, ‘Kay’) 45786586-5a6c-5903-becb-b29fa77421ac
5f7e563c-8bc4-11ed-bec0-0242ac140003 Jay Uuid::uuid5(’5f7e563c-8bc4-11ed-bec0-0242ac140003’, ‘Jay’) 30858268-a085-5f4f-80a9-940a1a5a306c

Question

Is there a chance that a UUID could be duplicated?

The short answer is no. With the sheer number of possible combinations (2^128), it would be almost impossible to generate a duplicate unless you are generating trillions of IDs EVERY SECOND, for many years.

Store the uuid as string or uuid type?

To put some perspective, an index of UUIDs for a table with 1,000,000 rows would weight around 16MB. If you stored it like a string, you would have an index weighting around 288MB. Never store UUID like strings. NEVER.

Reference

Performance