生成GUID的哪种方法最适合确保GUID真正唯一?

时间:2021-07-04 16:56:33

I have been using this for a long long time, but I couldn't find clear explanation on when it is not guaranteed unique. Here are my comments, please correct as you see fit.

我已经使用了很长时间了,但我找不到明确的解释,说明它何时不能保证唯一。以下是我的意见,请您认为合适。

Value is guaranteed to be unique(ly created) on the machine it is created. So GUIDs generated by .net app on a machine will never conflict with those that are created by SQL Server on the same machine.

保证值在创建它的机器上是唯一的(创建)。因此,.net app在计算机上生成的GUID永远不会与SQL Server在同一台计算机上创建的GUID冲突。

In all other cases there is no guarantee. Theoretically it is always possible that if there are multiple creation sources(say applications on different computers) for the unique identifier field in a db on a different computer, there can be duplicates that'll be inserted.

在所有其他情况下,无法保证。理论上,如果在不同计算机上的数据库中有唯一标识符字段的多个创建源(比如说在不同计算机上的应用程序),则总是可能存在重复项。

EDIT: Apologies for not forming the question properly. I guess I wanted to know the probability of conflicts when generated on the same machine vs different machines, if c#'s Guid.NewGuid(), SQL Server's newid(),newsequentialid() functions or any other functions by different applications are used. The 'already answered' link has one of the replies where it says he actually encountered a case where that happened. Makes you wonder how frequently that can happen.

编辑:抱歉没有正确地形成问题。我想我想知道在不同机器上生成冲突的可能性,如果使用c#的Guid.NewGuid(),SQL Server的newid(),newsequentialid()函数或不同应用程序的任何其他函数。 “已经回答”的链接中有一条回复说它实际上遇到了发生这种情况的案例。让你想知道这种情况会发生多久。

If I have a table with a uniqueidentifier field as the pk, should I have to worry about checking for uniqueness each time I do an insert by creating a new value either by different apps or SQL Server's functions?

如果我有一个带有uniqueidentifier字段的表作为pk,我是否应该担心每次通过不同的应用程序或SQL Server的函数创建新值来检查唯一性?

1 个解决方案

#1


17  

I guess I wanted to know the probability of conflicts when generated on the same machine vs different machines, if c#'s Guid.NewGuid(), SQL Server's newid(), newsequentialid() functions or any other functions by different applications

我想我想知道在同一台机器和不同的机器上生成冲突的可能性,如果c#的Guid.NewGuid(),SQL Server的newid(),newsequentialid()函数或不同应用程序的任何其他函数

That's an impossibly broad question. "any other function in a different application" is not something we can reason about.

这是一个不可思议的广泛问题。 “不同应用程序中的任何其他功能”不是我们可以推理的。

Instead let's ask an answerable question and then answer it.

相反,让我们问一个可回答的问题,然后回答它。

What are the different mechanisms for generating GUIDs?

生成GUID的不同机制有哪些?

Version one GUIDs combine the MAC address of the current machine, the current time, and a few more bits of implementation-specific origin. They are therefore unique in time and space.

版本1 GUID组合了当前机器的MAC地址,当前时间以及一些特定于实现的原点。因此它们在时间和空间上是独一无二的。

Version three and five GUIDs use a crypto-strength hash of a unique string. Their probability of collision is based on the probability of a hash collision.

版本3和五个GUID使用唯一字符串的加密强度哈希。它们的碰撞概率基于哈希冲突的概率。

Version four GUIDs use a pseudorandom number generator. Their probability of collision is based on the probability of the PRNG generating a collision.

版本四GUID使用伪随机数生成器。它们的碰撞概率基于PRN*生碰撞的概率。

What is the probability of a version one GUID collision on two different machines, given the assumption that machines have unique MAC addresses?

假设机器具有唯一的MAC地址,在两台不同的机器上发生第一版GUID冲突的概率是多少?

Zero.

What is the probability of a version one GUID collision when running two virtual machines on the same physical machine and generating GUIDs on each?

在同一物理计算机上运行两个虚拟机并在每个虚拟机上生成GUID时,版本一GUID冲突的概率是多少?

High; if the GUIDs are generated in the same time slice then they have a high probability of colliding.

高;如果GUID是在同一时间片中生成的,那么它们很可能发生冲突。

If it hurts when you do that, don't do it.

如果你这样做会伤害,不要这样做。

The rest of the GUID algorithms do not depend on details of the machine.

其余的GUID算法不依赖于机器的细节。

What is the probability of a version three or five GUID colliding with another, given that the source strings are unique?

鉴于源字符串是唯一的,版本三或五GUID与另一个GUID冲突的概率是多少?

The probability is roughly the same as the probability of a version four GUID collision, so let's consider that.

概率与版本四GUID冲突的概率大致相同,所以让我们考虑一下。

What is the probability of a version four GUID collision?

版本四GUID冲突的概率是多少?

The probability that a given v4 GUID will collide with any v4 GUID in a set of n unique v4 GUIDs is n divided by 2122.

给定v4 GUID将与一组n个唯一v4 GUID中的任何v4 GUID冲突的概率为n除以2122。

The probability that given a set of n v4 GUIDs will contain at least one collision is harder to express but it is extremely small as long as n is significantly smaller than 261.

给定一组n v4 GUID将包含至少一个碰撞的概率更难以表达,但只要n明显小于261,它就非常小。

#1


17  

I guess I wanted to know the probability of conflicts when generated on the same machine vs different machines, if c#'s Guid.NewGuid(), SQL Server's newid(), newsequentialid() functions or any other functions by different applications

我想我想知道在同一台机器和不同的机器上生成冲突的可能性,如果c#的Guid.NewGuid(),SQL Server的newid(),newsequentialid()函数或不同应用程序的任何其他函数

That's an impossibly broad question. "any other function in a different application" is not something we can reason about.

这是一个不可思议的广泛问题。 “不同应用程序中的任何其他功能”不是我们可以推理的。

Instead let's ask an answerable question and then answer it.

相反,让我们问一个可回答的问题,然后回答它。

What are the different mechanisms for generating GUIDs?

生成GUID的不同机制有哪些?

Version one GUIDs combine the MAC address of the current machine, the current time, and a few more bits of implementation-specific origin. They are therefore unique in time and space.

版本1 GUID组合了当前机器的MAC地址,当前时间以及一些特定于实现的原点。因此它们在时间和空间上是独一无二的。

Version three and five GUIDs use a crypto-strength hash of a unique string. Their probability of collision is based on the probability of a hash collision.

版本3和五个GUID使用唯一字符串的加密强度哈希。它们的碰撞概率基于哈希冲突的概率。

Version four GUIDs use a pseudorandom number generator. Their probability of collision is based on the probability of the PRNG generating a collision.

版本四GUID使用伪随机数生成器。它们的碰撞概率基于PRN*生碰撞的概率。

What is the probability of a version one GUID collision on two different machines, given the assumption that machines have unique MAC addresses?

假设机器具有唯一的MAC地址,在两台不同的机器上发生第一版GUID冲突的概率是多少?

Zero.

What is the probability of a version one GUID collision when running two virtual machines on the same physical machine and generating GUIDs on each?

在同一物理计算机上运行两个虚拟机并在每个虚拟机上生成GUID时,版本一GUID冲突的概率是多少?

High; if the GUIDs are generated in the same time slice then they have a high probability of colliding.

高;如果GUID是在同一时间片中生成的,那么它们很可能发生冲突。

If it hurts when you do that, don't do it.

如果你这样做会伤害,不要这样做。

The rest of the GUID algorithms do not depend on details of the machine.

其余的GUID算法不依赖于机器的细节。

What is the probability of a version three or five GUID colliding with another, given that the source strings are unique?

鉴于源字符串是唯一的,版本三或五GUID与另一个GUID冲突的概率是多少?

The probability is roughly the same as the probability of a version four GUID collision, so let's consider that.

概率与版本四GUID冲突的概率大致相同,所以让我们考虑一下。

What is the probability of a version four GUID collision?

版本四GUID冲突的概率是多少?

The probability that a given v4 GUID will collide with any v4 GUID in a set of n unique v4 GUIDs is n divided by 2122.

给定v4 GUID将与一组n个唯一v4 GUID中的任何v4 GUID冲突的概率为n除以2122。

The probability that given a set of n v4 GUIDs will contain at least one collision is harder to express but it is extremely small as long as n is significantly smaller than 261.

给定一组n v4 GUID将包含至少一个碰撞的概率更难以表达,但只要n明显小于261,它就非常小。