这个问题一直被很多人关注,基本上得到的答案是两种倾向,一种是锁一种是给记录打标记(也就是update)。对于应用来说,我并不提倡人为给记录加锁,这样会惹来很多麻烦,况且锁并不能解决所有问题,如果你有这方面好的经验我们可以进一步交流。 而update的过
这个问题一直被很多人关注,基本上得到的答案是两种倾向,一种是锁一种是给记录打标记(也就是update)。对于应用来说,我并不提倡人为给记录加锁,这样会惹来很多麻烦,况且锁并不能解决所有问题,如果你有这方面好的经验我们可以进一步交流。
聚彩手机商城系统,是一款专业于手机销售的独立手机网店系统,他拥有众多的手机参数选项,以及傻瓜式的设置选项,让您可以在5分钟内建立起专业而强大的手机销售网站。他拥有多套模版可以实时切换,前台拥有新闻中心、手机中心、配件中心、软件下载、手机报价、发货查询、保修查询、分店查询、产品的对比功能,代理与加盟的申请等功能,他拥有完善的会员中心,会员等级设置等,集成在线支付接口,超强SEO,可以设置所有页面的t
而update的过程会自动加锁,这个给我们带来和极大便利,但该方法一直不被人们认可的原因是效率:比如打标记怎么打,是否需要每个终端给数据印上各自不同的标志才能避免冲突;是否需要嵌套查询等等..
为了充分提高并发效率,我们可以使用rowlock和readpast来降低锁争,使用top子句来直接更新当前任务执行者需要的数据,使用output子句直接输出结果来代替嵌套查询。必要的情况下可以在表中加适当的索引。
我们来看一下具体代码
set
nocount
on
use
tempdb
go
if
(object_id
('tb'
) is
not
null
)
drop
table
tb
go
create
table
tb
(id
int
identity
(1
,
1
),
name
varchar
(10
),
tag
int
default
0
)
insert
into
tb
(name
) select
'a'
insert
into
tb
(name
) select
'b'
insert
into
tb
(name
) select
'c'
insert
into
tb
(name
) select
'd'
insert
into
tb
(name
) select
'e'
go
update
top
(2
) tb
with
(rowlock
,
readpast
) set
tag
=
1
output
inserted
.
id
,
inserted
.
name
where
tag
=
0
go
update
top
(2
) tb
with
(rowlock
,
readpast
) set
tag
=
1
output
inserted
.
id
,
inserted
.
name
where
tag
=
0
go
update
top
(2
) tb
with
(rowlock
,
readpast
) set
tag
=
1
output
inserted
.
id
,
inserted
.
name
where
tag
=
0
go
set
nocount
off
/*
id name
----------- ----------
1 a
2 b
id name
----------- ----------
3 c
4 d
id name
----------- ----------
5 e
*/
如果你有更好的建议,我们不妨探讨一下。









