删除表中的行会导致 LOCKS

编程入门 行业动态 更新时间:2024-10-27 06:23:46
本文介绍了删除表中的行会导致 LOCKS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在运行以下命令从一个大表(1.5 亿行)中批量删除行:

I am running the following command to delete rows in batches out of a large table (150 million rows):

DECLARE @RowCount int
WHILE 1=1
    BEGIN
        DELETE TOP (10000) t1
        FROM table t1
        INNER JOIN table2 t2 ON t2.PrimaryKey = t1.PrimaryKey
        WHERE t1.YearProcessed <= 2007

        SET @RowCount = @@ROWCOUNT

        IF (@RowCount < 10000) BREAK
    END

这张表被广泛使用.但是,它在删除记录的同时,也会导致某些记录被锁定,从而向用户抛出错误(这在我们所处的环境中是不可接受的).

This table is HIGHLY used. However, it is deleting records, but it is also causing locking on some records, thus throwing errors to the user (which is not acceptable in the environment we're in).

如何在不导致锁定的情况下删除旧记录?我应该将批次的大小从 10000 条记录减少到 1000 条吗?这将如何影响日志大小(我们几乎没有剩余的硬盘空间可用于大量日志增长).

How can I delete older records without causing locks? Should I reduce the size of the batch from 10000 records to 1000? How will this effect log sizes (we have very little hard drive space left for large log growth).

有什么建议吗?

推荐答案

我在过去看到过类似的零星问题,即使在小批量 0f 5000 条记录中,锁定仍然会发生.在我们的例子中,每个删除/更新都包含在它自己的 Begin Tran...Commit 循环中.纠正这个问题,

I have seen similar sporadic problems in the past where even in small batches 0f 5000 records, locking would still happen. In our case, each delete/update was contained in its own Begin Tran...Commit loop. To correct the problem, the logic of

等待延迟'00:00:00:01'

WaitFor DELAY '00:00:00:01'

被放置在每个循环的顶部并纠正了问题.

was placed at the top of each loop through and that corrected the problem.

这篇关于删除表中的行会导致 LOCKS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 21:33:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1195960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:行会   LOCKS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!