使用 rowcount 删除查询时陷入死锁
Set rowcount 50000
declare @i int
select @i = 1
WHILE ( @i > 0 )
BEGIN
DELETE table1
FROM table1 (index index1)
WHERE
HIST_Timestamp < '2011/11/26'
select @i = @@rowcount
END
查询有时会遇到死锁情况并终止。无法弄清楚出了什么问题。请帮助我!
Set rowcount 50000
declare @i int
select @i = 1
WHILE ( @i > 0 )
BEGIN
DELETE table1
FROM table1 (index index1)
WHERE
HIST_Timestamp < '2011/11/26'
select @i = @@rowcount
END
The query sometimes encounters a deadlock situation and terminates.. Not able to figure out what is going wrong .. Please help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当事务A锁定一条记录然后必须等待事务B解锁一条记录,而事务B正在等待事务A已经锁定的记录时,就会发生死锁。
如果你真的想知道为什么会发生死锁,你可以这样做用这个命令:
sp_configure "print deadlock information", 1
为查询创建有用的
索引
允许delete
语句使用页锁或行锁,从而提高并发访问到桌子上。如果无法为删除事务创建索引,可以在游标中执行操作,并频繁使用提交事务语句来减少页锁的数量。A deadlock occurs when transaction A locks a record then has to wait for transaction B to unlock a record, while transaction B is waiting on a record already locked by transaction A.
If you really want to know why the deadlock is happening, you can do it with this command:
sp_configure "print deadlock information", 1
Creating a useful
index
for the query allows thedelete
statement to use page or row locks, improving concurrent access to the table. If creating anindex
for the delete transaction is not possible, you can perform the operation in a cursor, with frequent commit transaction statements to reduce the number of page locks.