1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

テーブルロックせずに大量データを削除する

Posted at

結論

5000行を超える行ロックを取得しようとするとロックエスカレーションによりテーブルロックがかかってしまい、テーブルへの読み書きができなくなってしまう。
アプリケーションに影響を与えずにログなどの大量のデータを削除するには、下記のように行数指定でDELETE文を発行する。

DECLARE @i int = 1
WHILE @i > 0
BEGIN
	DELETE TOP (4000)
	  FROM [dbo].[accesslog]
	  WHERE [REQUESTSTARTTIME] BETWEEN '2023-01-01' and '2024-01-01'
	SET @i = @@ROWCOUNT
END
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?