0
0

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

はじめに

データを全件洗い変えるバッチを作ろうと思います。
処理は『全件削除 ⇒ 全件登録』のようにするのですが、全権削除をTRUNCATEにするかDELETEにするか調べてみました。

TRUNCATEとDELETEの比較

特徴 TRUNCATE DELETE
条件指定の可否 不可 可能
処理速度 速い 遅い
ロールバック 不可 可能
ログサイズ 大きくなりにくい 大きくなりやすい
トリガー 起動しない 起動する

トランザクションを張らない方が良いケース

1. 日中帯に長時間実行する場合

数百万件以上のデータを一括更新すると、コミットまでロックされます。
つまり、システムを長時間ロックするリスクがあります。

2. 失敗しても再実行すれば良い場合

失敗した場合に元のデータに戻す必要がないのであれば、トランザクションは不要です。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?