riad
riad

Reputation: 7194

How SQL Server creates uniqueidentifier using NEWID()

I have a user_info table in five different locations. Now I need to integrate all the rows into a central user_info table. To do this I need a unique id for each row in source tables. Because when all the rows come into the central table, then each user must have a unique ID.

Now my questions are:

  1. if I use uniqueidentifier NEWID() for each source table then is it will be unique for globally & life time or have any chance to be duplicate?

  2. How does SQL Server create the NEWID()? I need to know the key generation structure.

Upvotes: 6

Views: 22699

Answers (1)

richardtallent
richardtallent

Reputation: 35374

Yes, there is no chance of a duplicate between machines.

NEWID() is based on a combination of a pseudo random number (from the clock) and the MAC address of the primary NIC.

However, inserting random numbers like this as the clustered key on a table is terrible for performance. You should consider either NEWSEQUENTIALID() or a COMB-type function for generating GUIDs that still offer the collision-avoidance benefits of NEWID() while still maintaining acceptable INSERT performance.

Upvotes: 8

Related Questions