SQL Server 2008 newSequentionId()问题

发布于 2025-02-11 06:53:49 字数 1127 浏览 4 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

趴在窗边数星星i 2025-02-18 06:53:49

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?

囍笑 2025-02-18 06:53:49

这些值实际上是按 newSequentialId()的定义

创建一个GUID,比以前生成的任何GUID更大
自Windows启动以来,该功能在指定的计算机上。

它并不是说GUID中没有任何差距,只是任何新的GUID都应该比上一个GUID大。

尝试以下操作:

create table #test(id int, txt varchar(50), gid uniqueidentifier)

insert into #test
select 1    ,'test','72b48f77-0e26-de11-acd4-001bfc39ff92'
union select 2,    'test2', '92f0fc8f-0e26-de11-acd4-001bfc39ff92'
union select 3,    'test3', '122aa19b-0e26-de11-acd4-001bfc39ff92'

select * from #test
order by gid asc

如您所见,记录是按预期订购的1、2、3。

Those values are actually "sequential" as per the definition of NEWSEQUENTIALID():

Creates a GUID that is greater than any GUID previously generated by
this function on a specified computer since Windows was started.

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:

create table #test(id int, txt varchar(50), gid uniqueidentifier)

insert into #test
select 1    ,'test','72b48f77-0e26-de11-acd4-001bfc39ff92'
union select 2,    'test2', '92f0fc8f-0e26-de11-acd4-001bfc39ff92'
union select 3,    'test3', '122aa19b-0e26-de11-acd4-001bfc39ff92'

select * from #test
order by gid asc

As you can see, the records are ordered 1, 2, 3 which is as expected.

白色秋天 2025-02-18 06:53:49

他们是顺序的!

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

77< 8f< 9b !!!您必须看到最高值的BYET,而不是最低(从右到左)

THEY ARE SEQUENTIAL!

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

77 < 8f < 9b !!! You have to see the highest value byets, not the lowest (from right to left)

心作怪 2025-02-18 06:53:49

对于我称为newid()的唯一识别符类型,我不熟悉newSequentionId()。

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

夜光 2025-02-18 06:53:49

newSequentionId()序列中肯定会有差距 - 我发现以下原因是差距:

  1. 一旦另一个表由另一个表进行了newSequentionId()
  2. 失败的插入
  3. 回滚

(2和3) 例如,与Identity()在这方面类似)

,例如,使用newSequentialId()给定2个表,

create table XXX(someGuid uniqueidentifier DEFAULT NEWSEQUENTIALID(), x INT)
create table YYY(someGuid uniqueidentifier DEFAULT NEWSEQUENTIALID(), y DateTime)
GO

insert into XXX(x) values(1)
insert into XXX(x) values(2)
insert into XXX(x) values(3)
GO
insert into YYY(y) values(current_timestamp)
insert into YYY(y) values(current_timestamp)
insert into YYY(y) values(current_timestamp)
GO
insert into XXX(x) values(4)
insert into XXX(x) values(5)
insert into XXX(x) values(6)
GO

SELECT * FROM XXX
6A6E85CB-CCA3-E111-9E8E-005056C00008    1
6B6E85CB-CCA3-E111-9E8E-005056C00008    2
6C6E85CB-CCA3-E111-9E8E-005056C00008    3
**CCEA7AF2-CCA3-E111-9E8E-005056C00008  4** Gap here because we 'switched' to y
CDEA7AF2-CCA3-E111-9E8E-005056C00008    5
CEEA7AF2-CCA3-E111-9E8E-005056C00008    6

SELECT * FROM YYY
8F9438E1-CCA3-E111-9E8E-005056C00008    2012-05-22 07:13:35.503
909438E1-CCA3-E111-9E8E-005056C00008    2012-05-22 07:13:41.210
919438E1-CCA3-E111-9E8E-005056C00008    2012-05-22 07:13:41.220

此外,在失败的插入情况下,NewSequentialId()S不会返回序列,例如

insert into XXX(x) values(1)
insert into XXX(x) values(2)
BEGIN TRAN
insert into XXX(x) values(3)
insert into XXX(x) values(4)
ROLLBACK TRAN
insert into XXX(x) values(5)
insert into XXX(x) values(6)
GO

686EFE5B-CDA3-E111-9E8E-005056C00008
696EFE5B-CDA3-E111-9E8E-005056C00008
6C6EFE5B-CDA3-E111-9E8E-005056C00008
6D6EFE5B-CDA3-E111-9E8E-005056C00008

i.e. a Gap of 2 Guids rolled back

and

insert into XXX(x) values(1)
insert into XXX(x) values(2)
insert into XXX(x) values(3)
GO
insert into XXX(x) values(99999999999999) -- overflow
GO
insert into XXX(x) values(4)
insert into XXX(x) values(5)
insert into XXX(x) values(6)
go

select * from xxx
AC613611-CFA3-E111-9E8E-005056C00008    1
AD613611-CFA3-E111-9E8E-005056C00008    2
AE613611-CFA3-E111-9E8E-005056C00008    3
**B0613611-CFA3-E111-9E8E-005056C00008  4** Gap of 1 - overflow failure
B1613611-CFA3-E111-9E8E-005056C00008    5
B2613611-CFA3-E111-9E8E-005056C00008    6

There definitely can be gaps in NewSequentialId() sequences - I've found the following causes gaps:

  1. As soon as another call is made by another table needing a NewSequentialId()
  2. Failed inserts
  3. Rollbacks

(2 and 3 are similar to identity() in this respect)

For example, given 2 tables using NewSequentialId()

create table XXX(someGuid uniqueidentifier DEFAULT NEWSEQUENTIALID(), x INT)
create table YYY(someGuid uniqueidentifier DEFAULT NEWSEQUENTIALID(), y DateTime)
GO

insert into XXX(x) values(1)
insert into XXX(x) values(2)
insert into XXX(x) values(3)
GO
insert into YYY(y) values(current_timestamp)
insert into YYY(y) values(current_timestamp)
insert into YYY(y) values(current_timestamp)
GO
insert into XXX(x) values(4)
insert into XXX(x) values(5)
insert into XXX(x) values(6)
GO

SELECT * FROM XXX
6A6E85CB-CCA3-E111-9E8E-005056C00008    1
6B6E85CB-CCA3-E111-9E8E-005056C00008    2
6C6E85CB-CCA3-E111-9E8E-005056C00008    3
**CCEA7AF2-CCA3-E111-9E8E-005056C00008  4** Gap here because we 'switched' to y
CDEA7AF2-CCA3-E111-9E8E-005056C00008    5
CEEA7AF2-CCA3-E111-9E8E-005056C00008    6

SELECT * FROM YYY
8F9438E1-CCA3-E111-9E8E-005056C00008    2012-05-22 07:13:35.503
909438E1-CCA3-E111-9E8E-005056C00008    2012-05-22 07:13:41.210
919438E1-CCA3-E111-9E8E-005056C00008    2012-05-22 07:13:41.220

Also, NewSequentialId()s aren't returned to the sequence in the case of a failed insert, e.g.

insert into XXX(x) values(1)
insert into XXX(x) values(2)
BEGIN TRAN
insert into XXX(x) values(3)
insert into XXX(x) values(4)
ROLLBACK TRAN
insert into XXX(x) values(5)
insert into XXX(x) values(6)
GO

686EFE5B-CDA3-E111-9E8E-005056C00008
696EFE5B-CDA3-E111-9E8E-005056C00008
6C6EFE5B-CDA3-E111-9E8E-005056C00008
6D6EFE5B-CDA3-E111-9E8E-005056C00008

i.e. a Gap of 2 Guids rolled back

and

insert into XXX(x) values(1)
insert into XXX(x) values(2)
insert into XXX(x) values(3)
GO
insert into XXX(x) values(99999999999999) -- overflow
GO
insert into XXX(x) values(4)
insert into XXX(x) values(5)
insert into XXX(x) values(6)
go

select * from xxx
AC613611-CFA3-E111-9E8E-005056C00008    1
AD613611-CFA3-E111-9E8E-005056C00008    2
AE613611-CFA3-E111-9E8E-005056C00008    3
**B0613611-CFA3-E111-9E8E-005056C00008  4** Gap of 1 - overflow failure
B1613611-CFA3-E111-9E8E-005056C00008    5
B2613611-CFA3-E111-9E8E-005056C00008    6
雨后彩虹 2025-02-18 06:53:49

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文