简体   繁体   English

序列与身份

[英]Sequence vs identity

SQL Server 2012 introduced Sequence as a new feature, same as in Oracle and Postgres. SQL Server 2012 引入了Sequence作为一项新功能,与 Oracle 和 Postgres 相同。 Where sequences are preferred over identities?序列优先于身份的地方在哪里? And why do we need sequences?为什么我们需要序列?

I think you will find your answer here我想你会在这里找到你的答案

Using the identity attribute for a column, you can easily generate auto-incrementing numbers (which as often used as a primary key).使用列的标识属性,您可以轻松生成自动递增的数字(通常用作主键)。 With Sequence, it will be a different object which you can attach to a table column while inserting.对于序列,它将是一个不同的 object,您可以在插入时将其附加到表列。 Unlike identity, the next number for the column value will be retrieved from memory rather than from the disk – this makes Sequence significantly faster than Identity.与 identity 不同,列值的下一个数字将从 memory 而不是从磁盘中检索——这使得 Sequence 比 Identity 快得多。 We will see this in coming examples.我们将在接下来的示例中看到这一点。

And here : 在这里

Sequences: Sequences have been requested by the SQL Server community for years, and it's included in this release.序列:SQL 服务器社区多年来一直要求序列,它包含在此版本中。 Sequence is a user defined object that generates a sequence of a number.序列是用户定义的 object,它生成一个数字序列。 Here is an example using Sequence.下面是一个使用序列的例子。

and here as well:还有这里

A SQL Server sequence object generates sequence of numbers just like an identity column in sql tables. SQL 服务器序列 object 生成数字序列,就像 sql 表中的标识列一样。 But the advantage of sequence numbers is the sequence number object is not limited with single sql table.但是序号的好处是序号object不限于单个sql表。

and on msdn you can also read more about usage and why we need it ( here ):在 msdn 上,您还可以阅读更多有关用法以及我们需要它的原因(此处):

A sequence is a user-defined schema-bound object that generates a sequence of numeric values according to the specification with which the sequence was created.序列是用户定义的模式绑定 object,它根据创建序列的规范生成数值序列。 The sequence of numeric values is generated in an ascending or descending order at a defined interval and may cycle (repeat) as requested.数值序列以定义的间隔按升序或降序生成,并且可以按要求循环(重复)。 Sequences, unlike identity columns, are not associated with tables.与标识列不同,序列不与表相关联。 An application refers to a sequence object to receive its next value.应用程序引用序列 object 以接收其下一个值。 The relationship between sequences and tables is controlled by the application.序列和表之间的关系由应用程序控制。 User applications can reference a sequence object and coordinate the values keys across multiple rows and tables.用户应用程序可以引用序列 object 并跨多个行和表协调值键。

A sequence is created independently of the tables by using the CREATE SEQUENCE statement.使用 CREATE SEQUENCE 语句独立于表创建序列。 Options enable you to control the increment, maximum and minimum values, starting point, automatic restarting capability, and caching to improve performance.选项使您能够控制增量、最大值和最小值、起点、自动重启功能和缓存以提高性能。 For information about the options, see CREATE SEQUENCE.有关选项的信息,请参阅创建序列。

Unlike identity column values, which are generated when rows are inserted, an application can obtain the next sequence number before inserting the row by calling the NEXT VALUE FOR function. The sequence number is allocated when NEXT VALUE FOR is called even if the number is never inserted into a table.与插入行时生成的标识列值不同,应用程序可以在插入行之前通过调用 NEXT VALUE FOR function 获取下一个序列号。即使从未调用 NEXT VALUE FOR,也会在调用 NEXT VALUE FOR 时分配序列号插入到一个表中。 The NEXT VALUE FOR function can be used as the default value for a column in a table definition. NEXT VALUE FOR function 可用作表定义中列的默认值。 Use sp_sequence_get_range to get a range of multiple sequence numbers at once.使用 sp_sequence_get_range 一次获取多个序列号的范围。

A sequence can be defined as any integer data type.序列可以定义为任何 integer 数据类型。 If the data type is not specified, a sequence defaults to bigint.如果未指定数据类型,则序列默认为 bigint。

Sequence and identity both used to generate auto number but the major difference is Identity is a table dependant and Sequence is independent from table. Sequence 和 identity 都用于生成自动编号,但主要区别在于 Identity 是依赖于表的,而 Sequence 是独立于表的。

If you have a scenario where you need to maintain an auto number globally (in multiple tables), also you need to restart your interval after particular number and you need to cache it also for performance, here is the place where we need sequence and not identity.如果您有需要在全局(在多个表中)维护自动编号的场景,还需要在特定编号后重新启动间隔,并且还需要缓存它以提高性能,这里是我们需要序列而不是序列的地方身份。

Although sequences provide more flexibility than identity columns, I didn't find they had any performance benefits.虽然序列比标识列提供更多的灵活性,但我没有发现它们有任何性能优势。

I found performance using identity was consistently 3x faster than using sequence for batch inserts.我发现使用标识的性能始终比使用序列进行批量插入快 3 倍。

I inserted approx 1.5M rows and performance was:我插入了大约 150 万行,性能为:

  • 14 seconds for identity 14秒身份
  • 45 seconds for sequence序列 45 秒

I inserted the rows into a table which used sequence object via a table default:我通过表默认将行插入到使用序列 object 的表中:

NEXT VALUE for <seq> for <col_name>

and also tried specifying sequence value in select statement:并尝试在 select 语句中指定序列值:

SELECT NEXT VALUE for <seq>, <other columns> from <table>

Both were the same factor slower than the identity method.两者都比身份方法慢相同的因素。 I used the default cache option for the sequence.我为序列使用了默认缓存选项。

The article referenced in Arion's first link shows performance for row-by-row insert and difference between identity and sequence was 16.6 seconds to 14.3 seconds for 10,000 inserts. Arion 的第一个链接中引用的文章显示了逐行插入的性能,对于 10,000 次插入,同一性和序列之间的差异为 16.6 秒到 14.3 秒。

The Caching option has a big impact on performance, but identity is faster for higher volumes (+1M rows)缓存选项对性能有很大影响,但对于更高的卷(+100 万行),身份识别速度更快

See this link for an indepth analysis as per utly4life's comment.请参阅此链接以根据 utly4life 的评论进行深入分析。

I know this is a little old, but wanted to add an observation that bit me.我知道这有点老了,但想补充一点让我恼火的观察结果。

I switched from identity to sequence to have my indexes in order.我从身份切换到序列以使我的索引有序。 I later found out that sequence doesn't transfer with replication.后来我发现序列不会随着复制而转移。 I started getting key violations after I setup replication between two databases since the sequences were not in sync.由于序列不同步,我在两个数据库之间设置复制后开始出现密钥冲突。 just something to watch out for before you make a decision.在你做出决定之前需要注意的事情。

I find the best use of Sequences is not to replace an identity column but to create a "Order Number" type of field.我发现序列的最佳用途不是替换标识列,而是创建“订单号”类型的字段。

In other words, an Order Number is exposed to the end user and may have business rules along with it.换句话说,订单号向最终用户公开,并且可能具有业务规则。 You want it to be unique, but just using an Identity Column isn't really correct either.您希望它是唯一的,但仅使用标识列也不是真正正确的。

For example, different order types might require a different sequence, so you might have a sequence for Inte.net Order, as opposed to In-house orders.例如,不同的订单类型可能需要不同的顺序,因此您可能有一个用于 Inte.net 订单的顺序,而不是内部订单。

In other words, don't think of a Sequence as simple a replacement for identity, think of it as being useful in cases where an identity does not fit the business requirements.换句话说,不要将序列视为身份的简单替代品,而应将其视为在身份不符合业务要求的情况下很有用。

Recently was bit by something to consider for identity vs sequence.最近有点想考虑身份与序列。 Seems MSFT now suggests sequence if you may want to keep identity without gaps.似乎 MSFT 现在建议顺序,如果你想保持身份没有差距。 We had an issue where there were huge gaps in the identity, but based on this statement highlighted would explain our issue that SQL cached the identity and after reboot we lost those numbers.我们遇到了一个身份存在巨大差距的问题,但是根据这个突出显示的声明可以解释我们的问题,即 SQL 缓存了身份并且在重新启动后我们丢失了这些数字。

https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-2017 https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-2017

Consecutive values after server restart or other failures – SQL Server might cache identity values for performance reasons and some of the assigned values can be lost during a database failure or server restart.服务器重启或其他故障后的连续值 – SQL 服务器可能出于性能原因缓存标识值,并且在数据库故障或服务器重启期间可能会丢失一些分配的值。 This can result in gaps in the identity value upon insert.这可能会导致在插入时标识值出现间隙。 If gaps are not acceptable then the application should use its own mechanism to generate key values.如果间隙不可接受,则应用程序应使用其自己的机制来生成键值。 Using a sequence generator with the NOCACHE option can limit the gaps to transactions that are never committed.使用带有 NOCACHE 选项的序列生成器可以将间隙限制为从未提交的事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM