Reputation: 2577
Very simple question.
When a Microsoft GUID is generated, what part of GUID is considered unique? Take this guid for instance:
19C73070-400A-11E2-B017-3D05D2B0F3CE
I know that in a GUID it has 60 bits designated for a timestamp, 48 bits for a computer identifier, 14 bits of uniqueness, and six bits are fixed. I just don't know which order these are in.
What part of the above GUID could I use to get be unique? Doesn't have to be unduplicatable, but enough to identify off of it.
Thanks.
Upvotes: 0
Views: 258
Reputation: 20758
The uniqueness comes from the chances of you or anyone getting that same guid again using a fair guid-generating algorithm. The guid algorithm is basically a random number generator and the number of choices is so astronomical, that is is extremely unlikely that any two fairly generated guid generations will result in the same number.
There are 2^128 unique guids because guids are 128 bits. If you only have 64-bits (8-bytes), then you only have 2^64 choices. That is a MUCH smaller number and if you choose your 64-bit numbers randomly then there's a much higher chance that you'll "collide" with another random selection.
If you gave some structure, say 4-bytes for the date and time down to the minute (smalldatetime) then you'd have 60-bits left for a random number to uniquely identify any records for that one minute. If you're ok with the (n-1)/2^60 th possibility of repeating an ID for the nth new record every minute, then you can use that. (You'd still use all 64 of those bits as your ID) Otherwise, increase your bits!
Further, note that GUID doesn't have to be truly globally unique to be useful - the implicit information of what-it's-for is part of its uniqueness too! If you have a record in your database for managing file uploads, and that SAME guid is used by programmer in Prague to define some ActiveX plugin - there's absolutely no issue because there is no time where some code will use your record id to load the activex, nor the browser to accidentally load your record when it was intending to load the plugin! The context of the number is an important part of the number itself.
BUT, unless you have to generate new IDs between 2 or more systems which aren't always connected (offline or peer-to-peer clients, or multiple servers) then use serially incrementing longs instead, managed by the central authority.
Upvotes: 0
Reputation: 180884
All 128 Bits are required for uniqueness, maybe apart from 6 fixed bits. The same random part could be generated for a different timestamp, and anyone generating a V1 GUID might get the same as a V4 GUID, apart from the version.
Use the whole GUID or nothing, if you need something shorter then there might be other options with a Random Number Generator + application identifier.
Recommended reading: GUIDs are globally unique, but substrings of GUIDs aren't
Upvotes: 3
Reputation: 220904
You shouldn't depend on the implementation of the GUID generator, as it may change. The correct thing to do here would be to use a hashing algorithm to generate an 8-byte hash of the GUID and use that instead.
Upvotes: 2