SQLServer 数据批量更新注意 数据量大小

本文讲述了在处理约60万条数据的批量更新时遇到的问题,初始尝试通过一次更新3万条记录的方式导致锁表超时,影响业务。随后,通过逐条执行更新语句的方法解决了锁表问题,避免了对业务的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近,数据库有写数据需要运维一下,批量更新,数据量大约在60W左右

一开始偷懒了,想着图省事,批量更新,看看效果,一次更新3W条数据

drop table #temp
select top 30000 ID  into #temp  from SAP_ChargeBillSync_201909 with(nolock)
where  EndTime<'2019-09-12' and  DataState in('0','3') and SAPEnabled='0'

update  SAP_ChargeBillSync_201909 set DataState='1',DataStateReason='SAPEnabled为0或空',LastModifier='man20190921',LastModifyTime=GETDATE()
where id in(select * from #temp)

 结果问题就来了,对表加锁时间太长,锁表超时,影响业务数据写入了

所以更换了一种写法,update数据的尽可能的逐条执行,这样不会锁表。

declare @ID varchar(36)
declare @sqltxt varchar(250)

declare curtbls cursor for 
	select top 50000 ID  from SAP_ChargeBillSync_201909 with(nolock)
	where  EndTime<'2019-09-12' and  DataState in('0','3') and SAPEnabled='0'

	open curtbls 
	fetch next from curtbls into @ID 

	while @@FETCH_STATUS =0
	begin 
	select @sqltxt ='update  SAP_ChargeBillSync_201909 set DataState=''1'',DataStateReason=''SAPEnabled为0或空'',LastModifier=''man20190921'',LastModifyTime=GETDATE()
	                 where id ='+''''+@ID +'''';
    exec (@sqltxt);
	fetch next from curtbls into @ID
	end 
	close curtbls
	deallocate curtbls

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值