Last active
February 26, 2016 01:07
-
-
Save jdunck/8f9772bde4866c27732e to your computer and use it in GitHub Desktop.
Delete from a large table in chunks
This file contains hidden or 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
delete from x where id < 1; | |
select sleep(.25); | |
delete from x where id < 50001; | |
select sleep(.25); | |
delete from x where id < 100001; | |
select sleep(.25); |
This file contains hidden or 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 sys | |
table = sys.argv[1] | |
start_id = int(sys.argv[2]) | |
total_size = int(sys.argv[3]) | |
CHUNK = 50000 | |
for offset in range(1 + (total_size // CHUNK)): | |
print "delete from %s where id < %s;\n" % (table, start_id + (offset * CHUNK)) | |
print "select sleep(.25);\n" |
This file contains hidden or 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
python generate_sql.py your_table start_id max_id > statements.sql | |
mysql -h x.com -u y -p < statements.sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment