关于锁的问题.

yfcdsy 2021-02-19 05:42:04
Begin Tran

select top 1 @Amount=Amount from [Sells] where ID=@ID and key=1 order by ID Desc

业务逻辑(处理比较复杂,大概0.3秒用时)
--更新字段
update sells set key=1
Commit Tran


由于大并发, 还没有等到改值, 其他用户也读取了 这条记录. 导致业务处理错误.
...全文
144 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2021-02-22
  • 打赏
  • 举报
回复
请问Sells表的主键是什么? 数据量? 有哪些索引? 业务处理错误具体是什么情况?
yfcdsy 2021-02-21
  • 打赏
  • 举报
回复
引用 4 楼 卖水果的net 的回复:
update 的条件是什么 ? 前面取到那个 top 1 又有什么用?
有 where id=@id 不好意思.
yfcdsy 2021-02-21
  • 打赏
  • 举报
回复
引用 3 楼 唐诗三百首 的回复:
"update sells set key=1" --> 没有where子句? 此处需要全表更新?
有 where id=@id
yfcdsy 2021-02-21
  • 打赏
  • 举报
回复
有 where id=@id
卖水果的net 版主 2021-02-21
  • 打赏
  • 举报
回复 1
引用 7 楼 yfcdsy 的回复:
[quote=引用 4 楼 卖水果的net 的回复:]update 的条件是什么 ? 前面取到那个 top 1 又有什么用?
有 where id=@id 不好意思. [/quote]


create database test 
go
use test
go
create table test(id int primary key, name varchar(10), flag int default 0)
go
insert into test(id, name) 
values(1, 'xxx'),(2, 'yyy'),(3, 'zzz'),(4, 'ppp')
go
-- 这段可以执行多次,看一下效果。
declare @id int 
update test set @id = id, name = 'xxxA', flag =1 
where id = (select top 1 id from test where flag = 0 order by id)
print @id
go
select * from test
go
use master
go
drop database test
go

  • 打赏
  • 举报
回复
大并发 牵涉更新的 应用端可以选择走队列处理
卖水果的net 版主 2021-02-20
  • 打赏
  • 举报
回复
update 的条件是什么 ? 前面取到那个 top 1 又有什么用?
薛定谔的DBA 2021-02-20
  • 打赏
  • 举报
回复
Begin Tran select top 1 @Amount=Amount from [Sells] with(UPDLOCK,HOLDLOCK) where ID=@ID and key=1 order by ID Desc 业务逻辑(处理比较复杂,大概0.3秒用时) --更新字段 update sells set key=1 Commit Tran 以上可以避免脏读,为了加快速度,则需要建立相应索引。 sells (key)为聚集唯一索引(主键)
唐诗三百首 2021-02-20
  • 打赏
  • 举报
回复
"update sells set key=1" --> 没有where子句? 此处需要全表更新?

34,589

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧