SQL Server:NEWID()是否总是提供唯一的ID? [英] SQL Server : does NEWID() always gives a unique ID?

查看:477
本文介绍了SQL Server:NEWID()是否总是提供唯一的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NEWID()函数是否从不提供与已经提供的ID相同的ID?假设我选择一个 NEWID(),它返回 1(仅作为示例)。它永远不会再返回 1吗?

解决方案

两者都是 NEWID() NEWSEQUENTIALID() 给出类型为 uniqueidentifier



NEWID()涉及随机活动,因此下一个值是不可预测的,执行起来较慢。



NEWSEQUENTIALID()不涉及随机活动,因此下一个生成的值可以预测(不容易!),并且执行速度要快于 NEWID()



因此,如果您不担心要预测下一个值(出于安全原因),则可以使用 NEWSEQUENTIALID()。如果您担心可预测性,或者不介意微小的性能损失,则可以使用 NEWID()



<但是,从严格意义上讲,由不同机器生成的GUID具有相同价值的机会仍然微不足道。在实践中,这被认为是不可能的。



如果您想了解更多信息,请阅读以下内容:哪种生成GUID的方法最能确保GUID真正独特?



注意 NEWID()符合 RFC 4122 。另一个函数使用Microsoft的算法来生成值。


Does the NEWID() function never give me the same ID as it already did? Let's say I select a NEWID() and it returns '1' (just as an example). Will it never return '1' again? Is it impossible?

解决方案

Both NEWID() and NEWSEQUENTIALID() give globally unique values of type uniqueidentifier.

NEWID() involves random activity, thus the next value is unpredictable, and it's slower to execute.

NEWSEQUENTIALID() doesn't involve random activity, thus the next generated value can be predicted (not easily!) and executes faster than NEWID().

So, if you're not concerned about the next value being predicted (for security reasons), you can use NEWSEQUENTIALID(). If you're concerned about predictability or you don't mind the tiny performance penalty you can use NEWID().

However, in strict sense, there are still negligible chances that GUIDs generated by different machines have the same value. In practice, it's considered as being impossible.

If you want further info, read this: Which method for generating GUIDs is best for ensuring the GUID is really unique?

Note NEWID() complies RFC 4122. And the other function uses a Microsoft's algorithm for generating the value.

这篇关于SQL Server:NEWID()是否总是提供唯一的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆