Skip to content

Instantly share code, notes, and snippets.

@jdunck
Last active February 26, 2016 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdunck/8f9772bde4866c27732e to your computer and use it in GitHub Desktop.
Save jdunck/8f9772bde4866c27732e to your computer and use it in GitHub Desktop.
Delete from a large table in chunks
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);
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"
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