简体   繁体   English

SQL Server:NEWID()总是提供唯一的ID吗?

[英]SQL Server : does NEWID() always gives a unique ID?

Does the NEWID() function never give me the same ID as it already did? NEWID()函数是否永远不会给我相同的ID? Let's say I select a NEWID() and it returns '1' (just as an example). 假设我选择一个NEWID()并返回'1'(仅作为示例)。 Will it never return '1' again? 它永远不会再回归'1'吗? Is it impossible? 这不可能吗?

Both NEWID() and NEWSEQUENTIALID() give globally unique values of type uniqueidentifier . NEWID()NEWSEQUENTIALID()给出uniqueidentifier类型的全局唯一值。

NEWID() involves random activity, thus the next value is unpredictable, and it's slower to execute. NEWID()涉及随机活动,因此下一个值是不可预测的,执行起来较慢。

NEWSEQUENTIALID() doesn't involve random activity, thus the next generated value can be predicted (not easily!) and executes faster than NEWID() . NEWSEQUENTIALID()不涉及随机活动,因此可以预测下一个生成的值 (不容易!)并且比NEWID()执行得更快。

So, if you're not concerned about the next value being predicted (for security reasons), you can use NEWSEQUENTIALID() . 因此,如果您不关心预测的下一个值(出于安全原因),您可以使用NEWSEQUENTIALID() If you're concerned about predictability or you don't mind the tiny performance penalty you can use NEWID() . 如果你担心可预测性,或者你不介意微小的性能损失,你可以使用NEWID()

However, in strict sense, there are still negligible chances that GUIDs generated by different machines have the same value. 但是,从严格意义上讲,不同机器生成的GUID具有相同值的机会仍然可以忽略不计。 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? 如果您想了解更多信息,请阅读: 生成GUID的哪种方法最适合确保GUID真正唯一?

Note NEWID() complies RFC 4122 . 注意NEWID()符合RFC 4122 And the other function uses a Microsoft's algorithm for generating the value. 另一个函数使用Microsoft的算法来生成值。

If you're running NEWID() on the same machine then the return value will always be unique because it incorporates the current time stamp in its calculation. 如果您在同一台计算机上运行NEWID() ,则返回值将始终是唯一的,因为它在计算中包含当前时间戳。

On separate machines/systems, however, you could technically get the same id but the probability of that happening is so low that today's SQL DB community has essentially accepted that it IS impossible. 但是,在不同的机器/系统上,您可以从技术上获得相同的ID,但发生这种情况的可能性非常低,以至于今天的SQL DB社区基本上已经接受了它是不可能的。 Microsoft has more or less banked their reputation on it. 微软或多或少地在其上树立了声誉。

Related 有关

I had the same question, so I ran this simple query to see how unique the newid () could be, as you'll see there is no repeated IDs even in the same milisecond: 我有同样的问题,所以我运行这个简单的查询来看看newid()有多独特,因为你会看到即使在相同的毫秒内也没有重复的ID:

DECLARE @TRIES BIGINT, @ID UNIQUEIDENTIFIER, @REPEATED_ID UNIQUEIDENTIFIER
SET @TRIES = 1
SET @REPEATED_ID=NEWID()
WHILE @TRIES <= 1000
BEGIN
    SET @ID=NEWID() 
    IF @REPEATED_ID=@ID
        PRINT 'SAME -> ID '+CONVERT(NVARCHAR(MAX),@TRIES)+': '+ CONVERT(CHAR(36),@ID)
    ELSE
        PRINT 'DISTINCT -> ID '+CONVERT(NVARCHAR(MAX),@TRIES)+': '+ CONVERT(CHAR(36),@ID) + ' AT ' + CONVERT(VARCHAR,CAST(GETDATE() AS DATETIME2(3)) 
)
    SET @TRIES += 1
    SET @REPEATED_ID=@ID
END

You can define @TRIES as you wish. 您可以根据需要定义@TRIES。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM