当前位置: 移动技术网 > IT编程>数据库>MSSQL > SQL Server并发处理存在就更新解决方案探讨

SQL Server并发处理存在就更新解决方案探讨

2017年09月07日  | 移动技术网IT编程  | 我要评论

股评王大伟,国模伊诺娃,十堰区号

前言

本节我们来讲讲并发中最常见的情况存在即更新,在并发中若未存在行记录则插入,此时未处理好极容易出现插入重复键情况,本文我们来介绍对并发中存在就更新行记录的七种方案并且我们来综合分析最合适的解决方案。

探讨存在就更新七种方案

首先我们来创建测试表

if object_id('test') is not null
 drop table test

create table test
(
 id int,
 name nchar(100),
 [counter] int,primary key (id),
 unique (name)
);
go

解决方案一(开启事务)

我们统一创建存储过程通过来sqlquerystress来测试并发情况,我们来看第一种情况。

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 
 begin transaction
 if exists ( select 1
    from test
    where id = @id )
  update test
  set  [counter] = [counter] + 1
  where id = @id;
 else
  insert test
    ( id, name, [counter] )
  values ( @id, @name, 1 );
 commit
go

同时开启100个线程和200个线程出现插入重复键的几率比较少还是存在。

解决方案二(降低隔离级别为最低隔离级别uncommited)

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 
 set transaction isolation level read uncommitted
 begin transaction
 if exists ( select 1
    from test
    where id = @id )
  update test
  set  [counter] = [counter] + 1
  where id = @id;
 else
  insert test
    ( id, name, [counter] )
  values ( @id, @name, 1 );
 commit
go

此时问题依旧和解决方案一无异(如果降低级别为最低隔离级别,如果行记录为空,前一事务如果未进行提交,当前事务也能读取到该行记录为空,如果当前事务插入进去并进行提交,此时前一事务再进行提交此时就会出现插入重复键问题)

解决方案三(提升隔离级别为最高级别serializable)

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 
 set transaction isolation level serializable
 begin transaction
 if exists ( select 1
    from dbo.test
    where id = @id )
  update dbo.test
  set  [counter] = [counter] + 1
  where id = @id;
 else
  insert dbo.test
    ( id, name, [counter] )
  values ( @id, @name, 1 );
 commit
go

在这种情况下更加糟糕,直接到会导致死锁

此时将隔离级别提升为最高隔离级别会解决插入重复键问题,但是对于更新来获取排它锁而未提交,而此时另外一个进程进行查询获取共享锁此时将造成进程间相互阻塞从而造成死锁,所以从此知最高隔离级别有时候能够解决并发问题但是也会带来死锁问题。

解决方案四(提升隔离级别+良好的锁)

此时我们再来在添加最高隔离级别的基础上增添更新锁,如下:

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 
 set transaction isolation level serializable
 begin transaction
 if exists ( select 1
    from dbo.test with(updlock)
    where id = @id )
  update dbo.test
  set  [counter] = [counter] + 1
  where id = @id;
 else
  insert dbo.test
    ( id, name, [counter] )
  values ( @id, @name, 1 );
 commit
go

运行多次均未发现出现什么异常,通过查询数据时使用更新锁而非共享锁,这样的话一来可以读取数据但不阻塞其他事务,二来还确保自上次读取数据后数据未被更改,这样就解决了死锁问题。貌似这样的方案是可行得,如果是高并发不知是否可行。

解决方案五(提升隔离级别为行版本控制snapshot)

alter database upserttestdatabase
set allow_snapshot_isolation on
 
alter database upserttestdatabase
set read_committed_snapshot on
go 

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 
 begin transaction
 if exists ( select 1
    from dbo.test
    where id = @id )
  update dbo.test
  set  [counter] = [counter] + 1
  where id = @id;
 else
  insert dbo.test
    ( id, name, [counter] )
  values ( @id, @name, 1 );
 commit
go

上述解决方案也会出现插入重复键问题不可取。

解决方案六(提升隔离级别+表变量)

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 declare @updated table ( i int );
 
 set transaction isolation level serializable;
 begin transaction
 update test
 set  [counter] = [counter] + 1
 output deleted.id
   into @updated
 where id = @id;
 
 if not exists ( select i
     from @updated )
  insert into test
    ( id, name, counter )
  values ( @id, @name, 1 );
 commit
go

经过多次认证也是零错误,貌似通过表变量形式实现可行。

解决方案七(提升隔离级别+merge)

通过merge关键来实现存在即更新否则则插入,同时我们应该注意设置隔离级别为serializable否则会出现插入重复键问题,代码如下:

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 set tran isolation level serializable 
 begin transaction
 merge test as [target]
 using
  ( select @id as id
  ) as source
 on source.id = [target].id
 when matched then
  update set
    [counter] = [target].[counter] + 1
 when not matched then
  insert ( id, name, [counter] )
  values ( @id, @name, 1 );
 commit
go

多次认证无论是并发100个线程还是并发200个线程依然没有异常信息。

总结

本节我们详细讨论了在并发中如何处理存在即更新,否则即插入问题的解决方案,目前来讲以上三种方案可行。

解决方案一(最高隔离级别 + 更新锁)

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 
 begin transaction;
 
 update dbo.test with ( updlock, holdlock )
 set  [counter] = [counter] + 1
 where id = @id;
 
 if ( @@rowcount = 0 )
  begin
   insert dbo.test
     ( id, name, [counter] )
   values ( @id, @name, 1 );
  end
 
 commit
go

暂时只能想到这三种解决方案,个人比较推荐方案一和方案三, 请问您有何高见,请留下您的评论若可行,我将进行后续补充。

解决方案二(最高隔离级别 + 表变量)

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 declare @updated table ( i int );
 
 set transaction isolation level serializable;
 begin transaction
 update test
 set  [counter] = [counter] + 1
 output deleted.id
   into @updated
 where id = @id;
 
 if not exists ( select i
     from @updated )
  insert into test
    ( id, name, counter )
  values ( @id, @name, 1 );
 commit
go


解决方案三(最高隔离级别 + merge)

if object_id('testpro') is not null
 drop procedure testpro;
go
 
create procedure testpro ( @id int )
as
 declare @name nchar(100) = cast(@id as nchar(100))
 set tran isolation level serializable 
 begin transaction
 merge test as [target]
 using
  ( select @id as id
  ) as source
 on source.id = [target].id
 when matched then
  update set
    [counter] = [target].[counter] + 1
 when not matched then
  insert ( id, name, [counter] )
  values ( @id, @name, 1 );
 commit
go

暂时只能想到这三种解决方案,个人比较推荐方案一和方案三, 请问您有何高见,请留下您的评论若可行,我将进行后续补充。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网