tags:

views:

326

answers:

2

I'm having newsequentialid() learning problems in sql server management studio. Create a table with a uniqueidentifier column 'UniqueID', and set the default to newsequentialid().

Step 1. saving the design:

'Table_1' table - Error validating the default for column 'UniqueID'.

Save it anyway.

Step 2. view the sql:

CREATE TABLE [dbo].[Table_1](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [UniqueID] [uniqueidentifier] NOT NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Table_1] ADD  CONSTRAINT [DF_Table_1_UniqueID]  DEFAULT (newsequentialid()) FOR [UniqueID]
GO

Looks reasonable.

Step 3. add some rows:

1    test    72b48f77-0e26-de11-acd4-001bfc39ff92
2    test2    92f0fc8f-0e26-de11-acd4-001bfc39ff92
3    test3    122aa19b-0e26-de11-acd4-001bfc39ff92

They don't look very sequential. ??

Edit: I have gotten it to work somewhat if the inserts are all done at once, then the unique id is sequential. On later inserts, sql server seems to forget the last sequential id, and starts a new sequence.

Running this in ssms results in squential guids:

insert into Table_1 (Name) values('test13a');
insert into Table_1 (Name) values('test14a');
insert into Table_1 (Name) values('test15a');
insert into Table_1 (Name) values('test16a');
insert into Table_1 (Name) values('test17a');
A: 

I'm not familiar with newsequentialid(), for uniqueidentifier types I call newid().

SirDemon
newsequentialid is provided to solve the problem of page fragementation when clustering on a uniqueidentifier: http://msdn.microsoft.com/en-us/library/ms189786.aspx
Rex M
SQL Server 2008 Books Online NEWSEQUENTIALID() (Transact-SQL)
P a u l
Thanks for the info :). Down vote is a little harsh, though maybe this should have been a comment.
SirDemon
I wern't me, I don't downvote :+>
P a u l
I didn't think the answer added any value to this particular discussion, but since you made me feel guilty about it I'll pull it back :)
Rex M
Wasn't blaming those giving the info. Nor is there any blame, really. Everyone's entitled to their opinion, otherwise, what's the point of having a community?
SirDemon
Wow, I guess being born a Jew DOES give me the "Guilt Trip" power :P I though it was just a myth!
SirDemon
+2  A: 

newsequentialid is primarily to solve the issue of page fragmentation when your table is clustered by a uniqueidentifier. Your table is clustered by an integer column. I set up two test tables, one where the newsequentialid column is the primary key and one where it is not (like yours), and in the primary key the GUIDs were always sequential. In the other, they were not.

I do not know the internals/technical reasons why it behaves that way, but it seems clear that newsequentialid() is only truly sequential when your table is clustered by it. Otherwise, it seems to behave similarly to newid() / RowGuid.

Also, I'm curious as to why you would want to use newsequentialid() when you don't have to. It has many downsides which newid() does not, and none of the benefits - the biggest being that newid() is not practically predictable, whereas newsequentialid() is. If you are not worried about fragmentation, what's the point?

Rex M
Not 100% like newid(). If several inserts are done at once then the id is sequential.
P a u l
@Paul when I try several inserts at once using newsequentialid() on a non-clustered column, the IDs created are the same.
Rex M
My result also, I guess that's how it works.
P a u l
Even for the clustered index, the newsequentialid() doesn't really 100% follow the "sequantiality" - a few inserting batches might work, but eventually, it'll skip to a new set of GUID's. Try avoiding GUID's - especially as clustered key - by all means!
marc_s
@marc_s do you have any documentation on the "sequence" resetting?
Rex M
@Rex : i just tried it in two samples - table_1 from above, table_2 with a clustered index on the GUID column - the sequences are *NOT* really consecutive in either case.
marc_s
@marc_s The documentation on MSDN states that newsequentialguid() is sequential for that machine, and I've used it reliably in more than one application. I'm curious what other information you have that it may not be as reliable as Microsoft implies?
Rex M
I don't know about that - I just tested it. The first couple entries were 00FAF148-1526-DE11-9BA0-005056C00008, 01FAF148-1526-DE11-9BA0-005056C00008 and so forth - nice and consecutive. The third batch I inserted were D0F0AF50-1526-DE11-9BA0-005056C00008 and so on. Definitely not sequential...
marc_s