views:

116

answers:

1

I need a shell script that deletes 1000 rows from a massive database until there is no more rows left

It is very simple, but i am very weak in shell scripting, and the many tutorials online offer extremely similar, but different nuances in syntax

UPDATE:

  • Would it be possible to get some sample code?
  • I need to delete 1000 rows at a time
  • I have 120 Million rows to delete in total, and deleting them all at once will lock up the DB
A: 

you can call the mysql command line tool:

mysql -u username -p password < statements.txt

or

echo "your statement here" | mysql -u username -p password

in statements.txt put:

delete from `table` limit 1000;

although i do not understand why you only want to delete 1k at a time

a full script for sh would look like this:

#!/bin/sh
echo 'DELETE FROM `table` LIMIT 1000;' | mysql -u username -p password
knittl
Thanks knittl! I was wondering more about how i can construct the loop? I just could not get the full script to work.I have 120 Million rows to delete in total, and deleting them all at once will lock up the DB
ming yeow
@ming: have you considered using `TRUNCATE TABLE`? it's much faster than using the delete statement, when you have to delete all rows from a table
knittl