Skip to content

Instantly share code, notes, and snippets.

@tsauerwein
Created December 3, 2018 10:05
Show Gist options
  • Save tsauerwein/ffb159d1ab95d7fd91ef43b9609c471d to your computer and use it in GitHub Desktop.
Save tsauerwein/ffb159d1ab95d7fd91ef43b9609c471d to your computer and use it in GitHub Desktop.
Bulk delete a larger number of rows without locking up the table
import subprocess
import re
import time
def delete():
delete_stmt = [
'mysql',
'-h', '...',
'-u', '...',
'--password=...',
'table_abc',
'--skip-column-names',
'-e', "delete from ... where ...;",
'-vvv'
]
result = subprocess.run(delete_stmt, stdout=subprocess.PIPE)
output = result.stdout.decode('utf-8')
print(output)
match = re.search( r'(\d*) rows affected', output)
affected_rows = int(match.group(1))
return affected_rows
affected_rows = 1000
while affected_rows > 0:
affected_rows = delete()
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment