哪种方法生成的GUID的是最适合确保GUID是真正独一无二的?的是、最适合、哪种、无二

2023-09-04 13:14:58 作者:两袖风生

我一直用这个了很长很长的时间,但我无法找到它的时候,不能保证独特的明确的解释。这里是我的意见,请大家指正,你认为合适。

值被保证是它被创建在计算机上独特(LY创建)。因此,通过一台机器上的.NET应用程序生成的GUID将与那些由SQL Server在同一台机器上创建从来没有发生冲突。

在所有其他情况下也不能保证。从理论上讲它始终是可能的,如果有多个创作的来源(比如在不同计算机上的应用程序)在不同计算机上的数据库的唯一标识符字段,也可以是将被插入重复。

修改:道歉不成型的问题正确。我想我想知道在同一台机器VS不同的机器上时产生冲突的可能性,如果C#的 Guid.NewGuid(),SQL Server的 NEWID () NEWSEQUENTIALID()函数或任何其他职能由不同的应用程序中使用。在已回答环节都有回复的地方说,他居然在这里遇到发生的情况之一。使你不知道怎么经常可能发生。

word如何将目录单独生成一页

如果我有一个唯一标识符字段作为PK的表,我应该担心通过创建一个新的值或者通过不同的应用程序或SQL Server的功能,每一个我做的插入时间来检查的独特性?

解决方案   

我想我想知道冲突的几率在同一台机器VS不同的机器上生成的时候,如果C#的 Guid.NewGuid(),SQL Server的 NEWID() NEWSEQUENTIALID()函数或任何其他职能由不同的应用程序

这是一个不可能广泛的问题。 在不同的应用程序的其他功能,是不是我们可以推理。

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

  

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

版1的guid结合当前机器的MAC地址,当前时间,以及具体实现的原点几个比特。因此,它们在时间和空间是唯一的。

版本三,五的GUID采用了独特的字符串加密强度的哈希值。碰撞的概率是基于散列冲突的概率。

版4的guid使用伪随机数发生器。碰撞的概率是基于PRNG的产生碰撞的概率。

  

这是在两个不同的机器的一个版本1的GUID冲突的概率,给出假定机器有唯一的MAC地址

零。

  

在同一台物理机器上运行两个虚拟机,并​​在每个生成的GUID时,什么是第一版的GUID碰撞的概率是多少?

高;如果在同一个时间片产生该guid那么他们有碰撞的可能性高。

如果它伤害,当你这样做,不这样做。

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

  

什么是一个版本,三个或五个GUID与另一个发生碰撞的可能性,因为源字符串是独一无二的?

的概率大致相同版本4 GUID冲突的可能性,所以让我们考虑这一点。

  

什么是版本4 GUID碰撞的概率是多少?

这给定v4的GUID将与任何v4的GUID碰撞在一组n个独特v4的GUID的概率为n除以2到122

这给出一组n个4版的GUID将含有至少一个冲突的概率是更难的前preSS但它是非常小的,只要n是大于2显著小到61

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.

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.

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?

解决方案

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

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?

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.

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.

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

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

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?

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

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.

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

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

What is the probability of a version four GUID collision?

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 2 to the 122.

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 2 to the 61.