简体   繁体   English

使用标识列或自行开发的序列表的性能影响

[英]Performance implications for using an identity column or a homegrown sequence table

I have a legacy system that is using a table for sequencing numbers. 我有一个遗留系统,使用表格来排序数字。 The table has the following definition: 该表具有以下定义:

dbo.id_table
(
    table_name char(64) NOT NULL,
    id_type char(5) NOT NULL,
    data_type char(5) NOT NULL,
    next_id_number int NOT NULL,
    next_id_max char(15) NOT NULL
)
PRIMARY KEY CLUSTERED 
(
    table_name ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

This table is functionally equivalent for an identity column. 此表在功能上等同于标识列。

Here is how the table is used: -A stored procedure runs to get the next value of the id in the table. 以下是表的使用方法: - 运行存储过程以获取表中id的下一个值。 eg, 例如,

exec id_proc 'bryans_table', @some_int_value output

I am looking for an expert to answer the following question: 我正在寻找专家来回答以下问题:

What are the performance implications (as specific as possible) for using a design like this with SQL Server 2008 R2 (currently running in compatibility mode, but plans to use full 2008 R2 at some point in the future) vs. using a regular identity column? 对于使用SQL Server 2008 R2这样的设计(当前在兼容模式下运行,但计划在将来的某个时间点使用完整的2008 R2)与使用常规标识列相比,性能影响(尽可能具体) ? Does this scale at all? 这种规模是否完全?

We are seeing a lot of contention is this table and I want to know if the tables were switched to identity columns what type of performance gains might be had (or lost)? 我们看到这个表存在很多争议,我想知道表是否切换到标识列可能会有哪些性能增益(或丢失)? At this point the source of contention is unclear. 在这一点上,争论的来源尚不清楚。

(I do not know why an identity column was not included in the original design -- this is a legacy database) (我不知道为什么标识列没有包含在原始设计中 - 这是一个遗留数据库)

By definition a design like this implies at most one transaction can generate a new sequence for a table (because of the X lock on the record being incremented). 根据定义,这样的设计意味着最多一个事务可以为表生成一个新序列(因为记录上的X锁定递增)。 In other words, all INSERTs are serialized (no new INSERT can proceed until the first one commits ). 换句话说,所有INSERT都是序列化的(在第一个INSERT 提交之前,没有新的INSERT可以继续)。 Performance tanks. 表演坦克。

IDENTITY on the other hands is capable of generating the sequences concurrently. 另一方面,IDENTITY能够同时生成序列。

If you're stuck with the sequence table you could generate the new IDs on a separate transaction, requiring a separate connection to the server, and commit immediately the increment. 如果您坚持使用序列表,则可以在单独的事务上生成新ID,需要单独连接到服务器,并立即提交增量。 Or generate in batches (increment +1000) and handle the allocated batch in your app code. 或者批量生成(增量+1000)并在应用代码中处理分配的批次。 The later solution works great at alleviating contention. 后一种解决方案可以很好地缓解争用。 But you loose transactional consistency, the increment occurs on a separate transaction from the INSERT and thus you will see gaps, missing sequences etc. Truth in advertising though: IDENTITY has the same issues (much for the same reasons...) 但是你失去了事务的一致性,增量发生在与INSERT不同的事务中,因此你会看到缺口,缺失序列等。但广告中的真相:IDENTITY有同样的问题(很多原因相同......)

You can use a generator table to get sequence id's without having the problem of lock contention 您可以使用生成器表来获取序列ID,而不会出现锁争用问题

-- the shadow table - 影子表

create table dummy_id (
  id int identity not null,
  dummy int not null
)

-- proc showing usage - proc显示用法

create proc dummy_id_gen(@newid int output) as
begin

begin tran
insert dbo.dummy_id (dummy) values (0)
select @newid = scope_identity()
rollback tran

end

-- calling the proc to test it - 调用proc来测试它

declare @newid int
exec dummy_id_gen @newid = @newid output
select @newid as newid

This still has the problem of only 1 row at time for the generated ids 对于生成的id,这仍然只有一行的问题

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

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