We’re thinking about converting the constrain on an existing column in a table with billions of rows of data. The column is a uniqueidentifier serving as a nonclustered primary key; we’re planning on replacing the NEWID() constraint to a NEWSEQUENTIALID(). What is challenging me is my concern over primary key collisions; will the new default values eventually run into an existing primary key value?
I used the following script to test, and I was a little surprised at the results:
CREATE TABLE TestNewSeqID(ID uniqueidentifier CONSTRAINT DF1 DEFAULT newID(), Marker integer Identity(1,1)) GO DECLARE @x int SET @x= 1 WHILE @x <=10 BEGIN INSERT INTO TestNewSeqID DEFAULT VALUES SET @x = @x +1 END SELECT * FROM TestNewSeqID ORDER BY ID DESC ALTER TABLE TestNewSeqID DROP CONSTRAINT DF1; ALTER TABLE TestNewSeqID ADD CONSTRAINT DF1 DEFAULT newsequentialid() for ID; SET @x= 1 WHILE @x <=10 BEGIN INSERT INTO TestNewSeqID DEFAULT VALUES SET @x = @x +1 END SELECT * FROM TestNewSeqID ORDER BY ID DESC
My results looked something like the following:
ID Marker
F664E06A-51BF-4FC1-960E-C9E5E854ADAD 9
0746DBAC-8B14-463C-8C4E-9CC789794963 7
24217231-1865-4E2A-B8EF-944DEC9AC6A2 1
323C15B4-59DA-4B73-94E5-54A1A0860F3D 8
B0C1E7EE-3701-4916-A3E2-465FE8462965 2
BFE8C2AB-4788-4596-867E-221F75BAB338 10
C0BC2263-F5E6-41F1-83F6-14BD9D50A6DD 3
9FA2A683-FF02-458E-8703-10D1DCDEFA97 4
75445937-6B46-4B88-9525-0DD809A7BE4B 6
E93284BE-E93B-4009-B206-04CB2B107293 5
4390AD7A-CD7D-DE11-98B8-00196665566A 20
4290AD7A-CD7D-DE11-98B8-00196665566A 19
4190AD7A-CD7D-DE11-98B8-00196665566A 18
4090AD7A-CD7D-DE11-98B8-00196665566A 17
3F90AD7A-CD7D-DE11-98B8-00196665566A 16
3E90AD7A-CD7D-DE11-98B8-00196665566A 15
3D90AD7A-CD7D-DE11-98B8-00196665566A 14
3C90AD7A-CD7D-DE11-98B8-00196665566A 13
3B90AD7A-CD7D-DE11-98B8-00196665566A 12
3A90AD7A-CD7D-DE11-98B8-00196665566A 11
If I looped through several more times, the seed changed and the values got progressively higher, but they were still sorting lower than the original data set. I’d appreciate it if others would run this script and see if they got the same results.