Created
December 3, 2018 10:05
-
-
Save tsauerwein/ffb159d1ab95d7fd91ef43b9609c471d to your computer and use it in GitHub Desktop.
Bulk delete a larger number of rows without locking up the table
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
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