有一个每小时运行一次的进程必须插入到table1中,但是当下面的查询正在运行时,插入会被阻止。谁能解释为什么?我意识到锁提示就是这样,提示和SQL可能会选择忽略它们。 table1有300m行,我需要更新ColumnA中的值。我正在做大块的事情来帮助回滚,以防止进程被停止,因此while循环。此查询为什么阻止插入?
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
go
declare @start bigint
declare @end bigint
declare @max bigint
declare @step int
set @step = 50000
set @start = 17953095
set @end = @start + @step
set @max = @start + 2000000
while (@end < @max)
begin
waitfor delay '00:00:10'
begin transaction
update [table1] with (ROWLOCK)
set [table1].[ColumnA] = [table2].[ColumnA]
from [table2] (nolock)
where [table2].[ColumnB] = [table1].[ColumnB]
and [table1].ID >= @start
and [table1].ID < @end
commit transaction
print @end
if @end >= @max
begin
break
end
set @start = @end
set @end = @end + @step
end
print @end
我可以这样做另一种方式,因此它不会阻止插入或其他更新?除了上面的查询外,其他任何内容都不需要使用ColumnA。
所以你认为减少到5K的步骤会有帮助吗?我能否看到桌上的桌子锁是否被取出? – ILovePaperTowels
+1正确 - 在桌子上进行超过5000次的更新,并且您获得了独占(X)表锁! –
或者我应该改变锁升级? – ILovePaperTowels