设为首页 收藏本站
查看: 397|回复: 0

[经验分享] Simulating Sequence Objects in SQL Server

[复制链接]

尚未签到

发表于 2016-11-2 01:09:10 | 显示全部楼层 |阅读模式
Many applications need sequentially incremental number as unique/primary key of records.  SQL Server 2005 today supports identity column as the primary mean to general sequence number, which generates the sequence number upon the execution of DML (insert) or bulk insert.  The value of the insertion is known (when using insertion DML), post the execution of the DML.  In some implementations, the sequence numbers (or next unique incremental number) need to be generated prior to the DML operation.  For example, if sequence number can be retrieved prior to the DML operation, it can be easily implemented in situations where multiple DML (inserts, or even updates) using the same sequence number can be performed, and data can be insert into different tables, for example, parent-child, or based on the sequence value, data can be insert into different tables.

SQL Server 2005 Migration Assistant (SSMA) provides a different mechanism for sequence number generation.  SSMA’s approach is to use a single table to hold all the sequence numbers.  Each sequence number object is represented by a single row of record, which holds the property of the sequence, e.g. sequence name, current value, increments…etc.  The generation of the next value is invoked by an extended stored procedure in order to minimize the impact on application concurrency – the execution of extended procedure is not within the transaction scope of the DML statement, and it holds very short duration locks on the row that represents the sequence number.

This BLOG describes two more methods of sequence number generation.

Option 1
This option is easy to use and maintain, but it is suitable only with lower volume invocations.  It is also one of the widely used approaches, and often misused.  This method uses a single table to hold all the sequences needed for the application, and each sequence is represented by one single record, thus serializes operation requesting next value of the same sequence number.


1.      Create the following objects

Table AllSequences holds the information of all the sequences.  As a simple example, the table and subsequent stored procedure is kept quite simple.  Further enhancement is certainly possible based on the needs – for example, additional information of sequence such as maximum range, minimum range, loopback(yes/no) can be added.

·         SeqName – name of the sequence
·         Seed – seed value of the sequence
·         Incr – incremental value of the sequence
·         Currval – current value of the sequence


Create table AllSequences (
      SeqName nvarchar(255) primary key, -- name of the sequence
      Seed int not null default(1), -- seed value
      Incr int not null default(1), -- incremental
      Currval int
)
Go

The following stored procedure is used to create a sequence.  The following input parameter is expected:
·         SeqName – name of the sequence
·         Seed – seed value of the sequence
·         Incr – incremental value of the sequence


create procedure usp_CreateNewSeq
      @SeqName nvarchar(255),
      @seed int = 0,
      @incr int = 1
as
begin
      declare @currval int
      if exists (
            select 1 from AllSequences
            where SeqName = @SeqName )
      begin
            print 'Sequence already exists.'
            return 1   
      end

      if @seed is null set @seed = 1
      if @incr is null set @incr = 1
      set @currval = @seed

      insert into AllSequences (SeqName, Seed, Incr, CurrVal)
      values (@SeqName, @Seed, @Incr, @CurrVal)
     
end
go


The following stored procedure is used to generate the next value of the specified sequence through input parameter SeqName.

create procedure usp_GetNewSeqVal
      @SeqName nvarchar(255)
as
begin
      declare @NewSeqVal int
      set NOCOUNT ON
      update AllSequences
      set @NewSeqVal = CurrVal = CurrVal+Incr
      where SeqName = @SeqName
     
      if @@rowcount = 0 begin
print 'Sequence does not exist'
            return
      end

      return @NewSeqVal
end
go

   
2.      To create the new sequence, for example:
usp_CreateNewSeq N'TestSeq'

3.      To retrieving the new sequence value, for example for sequence “TestSeq”
usp_GetNewSeqVal N'TestSeq'

One thing to be careful is to keep the execution of the stored procedure usp_CreateNewSeq outside the scope of subsequent transaction which uses the sequence number generated.  This can minimize impact of blocking.

Typical usage
Declare @NewSeqVal int
Execute @NewSeqVal=usp_GetNewSeqVal @seqname=N'TestSeq'
print @NewSeqVal

Option 2
This method is intended for higher-throughput environments – such as thousands of transactions per second.  A table with identity column is created for every sequence.  Since the generation of identity value is lightweight, and not within the transaction scope of subsequent DML, this method can significantly improve the concurrency over the first method.  In addition, it also consumes less resource than extended procedure.


1.      Create one table with the following structure for each sequence, assuming seed 1, with increment of 1).
create table <tablename> (
      SeqID int identity(1,1) primary key,
      SeqVal varchar(1)
)

2.      Create the following stored procedure.  A delete operation is performed to remove the rows in the sequence table to reduce maintenance.  This obviously adds some load to the transaction however it should be quite minimal.  As an alternative, for higher throughput, the delete statement can be removed and a maintenance job can be scheduled during off-hour to purge the records in the table (truncate should not be used since it resets the identity value).
create procedure GetNewSeqVal_<tablename>
as
begin
      declare @NewSeqValue int
      set NOCOUNT ON
      insert into <tablename> (SeqVal) values ('a')
     
      set @NewSeqValue = scope_identity()
     
      delete from <tablename> WITH (READPAST)
return @NewSeqValue
end

3.      To retrieve the new value in a result set, simply execute the following:
GetNewSeqVal_<tablename>


Typical usage
            Declare @NewSeqVal int
            Exec @NewSeqVal =  GetNewSeqVal_<tablename>
           

Among the two options described above, Option 1 is widely used in different variations but prone to blocking/deadlocking.  Option 2 improves concurrency significantly with a simple approach.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-294260-1-1.html 上篇帖子: SQL Server 索引结构及其使用(4) 下篇帖子: VB.NET DMO SQL SERVER备份恢复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表