Created
November 2, 2020 11:26
-
-
Save tudormunteanu/3df78d255e46f6a15b1f89a119ab26f6 to your computer and use it in GitHub Desktop.
Batch delete database rows with transactions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin; | |
\timing on | |
DELETE FROM | |
account AS a | |
USING | |
member AS m | |
WHERE | |
m.territory='XY' AND | |
m.member_id >= (:v1 + 0) * 1000 AND | |
m.member_id < (:v1 + 1) * 1000 AND | |
a.member_id = m.member_id | |
; | |
\timing off | |
commit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
URL='postgres://...' | |
SQL_FILE='sample.sql' | |
# Batches of 1000 | |
for i in {0..100000}; do | |
id_=$((i*1000)) | |
echo "=== Batch #${i}. IDs > ${id_}." | |
psql -v v1="${i}" -f $SQL_FILE $URL | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment