问题描述
NEWID()
函数是否从不提供与已经提供的ID相同的ID?假设我选择一个 NEWID()
,它返回 1(仅作为示例)。它永远不会再返回 1吗?
两者都是和给出类型为。
NEWID()
涉及随机活动,因此下一个值是不可预测的,执行起来较慢。
NEWSEQUENTIALID()
不涉及随机活动,因此(不容易!),并且执行速度要快于 NEWID()
。
因此,如果您不担心要预测下一个值(出于安全原因),则可以使用 NEWSEQUENTIALID()
。如果您担心可预测性,或者不介意微小的性能损失,则可以使用 NEWID()
。
如果您想了解更多信息,请阅读以下内容:
注意 NEWID()
符合。另一个函数使用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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!