SQL Server 2008 newSequentionId()问题
我在SQL Server Management Studio中有NewSequentionId()学习问题。创建一个具有唯一识别列列“ uniqueId”的表,并将默认设置为newSequentionId()。
步骤1。保存设计:
'table_1'表格 - 错误验证列“ uniqueId”列的默认值。
无论如何保存。
步骤2。查看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
看起来合理。
步骤3。添加一些行:
1 test 72b48f77-0e26-de11-acd4-001bfc39ff92
2 test2 92f0fc8f-0e26-de11-acd4-001bfc39ff92
3 test3 122aa19b-0e26-de11-acd4-001bfc39ff92
它们看起来不太顺序。 ?
编辑:如果插入一次都完成,我已经可以在某种程度上工作,那么唯一的ID是顺序的。在以后的插入物上,SQL Server似乎忘记了最后一个顺序ID,并启动了一个新序列。
在SSMS中运行此过程会导致挤压指南:
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');
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');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
NewSequentionID主要是为了解决您的表由唯一识别符聚集时的页面碎片问题。您的表由整数列聚集。我设置了两个测试表,其中一个是newSequentionID列是主要键,一个不是(像您一样),而在主键中,GUID始终是顺序的。另一方面,他们不是。
我不知道内部质量/技术原因为什么它是这种行为,但是很明显,只有当您的表被它聚集时,newSequentionId()才真正的顺序。否则,它的行为似乎与newid() / rowguid相似。
另外,我很好奇为什么您不必在不需要的情况下使用newSequentionId()。它具有许多弊端,而newid()也没有,也没有一个好处 - 最大的是newid()实际上是可以预测的,而newSequentionId()是。如果您不担心分裂,那有什么意义?
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?
这些值实际上是按 newSequentialId()的定义 :
它并不是说GUID中没有任何差距,只是任何新的GUID都应该比上一个GUID大。
尝试以下操作:
如您所见,记录是按预期订购的1、2、3。
Those values are actually "sequential" as per the definition of NEWSEQUENTIALID():
It doesn't say there can't be any gaps in the GUIDs, it's just that any new GUID should be greater than the previous one.
Try this:
As you can see, the records are ordered 1, 2, 3 which is as expected.
他们是顺序的!
77< 8f< 9b !!!您必须看到最高值的BYET,而不是最低(从右到左)
THEY ARE SEQUENTIAL!
77 < 8f < 9b !!! You have to see the highest value byets, not the lowest (from right to left)
对于我称为newid()的唯一识别符类型,我不熟悉newSequentionId()。
I'm not familiar with newsequentialid(), for uniqueidentifier types I call newid().
在
newSequentionId()
序列中肯定会有差距 - 我发现以下原因是差距:(2和3) 例如,与Identity()在这方面类似)
,例如,使用
newSequentialId()
给定2个表,此外,在失败的插入情况下,NewSequentialId()S不会返回序列,例如
There definitely can be gaps in
NewSequentialId()
sequences - I've found the following causes gaps:(2 and 3 are similar to identity() in this respect)
For example, given 2 tables using
NewSequentialId()
Also, NewSequentialId()s aren't returned to the sequence in the case of a failed insert, e.g.
newSequinentialGuid(按照保证其顺序生成的每个指南)包括通过时间戳计算的指南的一部分。因此,如果您在不同的时间运行插入物,您会看到一些差距。
但是重要的是,该GUID被以不引起页面拆分的方式“订购”(如果在索引中使用了GUID),这就是使用新的顺序GUID时会发生什么。
NEWSEQUENTIALGUID (as every guid generated in a way that warrant their sequence) includes a part of the Guid calculated via a time stamp. So if you run the inserts at different time you'll see some gaps.
But the important part is that the Guid are "ordered" in a way that do not cause page splits (if the Guid is used in a index) and this is what happens when using the new sequential guid.