Is it safe to pre-generate a Guid and insert it into a table as the primary key?

I've seen many situations before where PK is a directive with no default and as such the developer generates random guides and uses them to insert into the database.

I've often wondered what is the likelihood that they will generate a number that already exists?

+1


a source to share


1 answer


Some info: the number of values ​​that 16 bytes can store is 2 ^ 128.

Total = number of common values ​​that can be represented

Actual = number of values ​​that have already been used

The probability of inserting an identifier into an already existing table can be formulated as follows:

1 - (Total - Actual) / Total

I did some preliminary calculations and I came up with the following:

Let's say you had a table with 10 million records in the table, then the probability would be



1 - (2 ^ 128 - 10,000,000) / 2 ^ 128

which appromixately

0.00000000000000000000000000000003

I think now I know why he said that:

"While each generated GUID is not guaranteed to be unique, the total number of unique keys is so great that the likelihood that the same number is generated twice is infinitesimal."

from wiki

+4


a source







All Articles