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
5
  • Do you need a shell script that runs an SQL statement, or are you planning on using the shell script for logic? Commented May 14, 2010 at 19:01
  • Is it not possible to just write an SQL query that deletes all 1000 rows? Commented May 14, 2010 at 19:01
  • Are you just trying to erase the table? Commented May 14, 2010 at 19:02
  • 1
    What is the question? How to write a shell script or how to execute a mysql-query from a shell script or how to delete rows? Commented May 14, 2010 at 19:33
  • Updated again - i need to delete 1000 at a time because i have 120M rows in total, and i need the DB to be up and running Commented May 14, 2010 at 20:29

3 Answers 3

2

You could also do this using just mysql:


#!/bin/sh
mysql -D <database> -u <user> -p<password> -e 'DELETE FROM <table> WHERE <condition> LIMIT 10000;'

-e - accepts a query as an argument

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

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: 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
1

Just hope my script maybe help someone who search similar questions

#!/bin/sh
# mysql_delta_del.sh 
# -----------------------------

db_user="root"
db_host="127.0.0.1"
db_passwd="yourpass"
db="yourdb"
db_table="mytable"
keep_records=10000000
## start id, you can change according your db
start_id=31000000

# mysql bin's path
MYSQL="$(which mysql)"


maxid=`$MYSQL -u $db_user -h $db_host -p$db_passwd $db -e 'select max(id) from $db_table'`

maxid=`echo $maxid |cut -d" " -f2`

#keep last 10 million records
delto=`echo $maxid - $keep_records  |bc`

echo "target id: $delto"


i=$start_id 
while [ $i -lt $delto ]
do
 echo "delete to $i "

 echo "DELETE FROM $db_table where id<$i LIMIT 1000 " | mysql -u $db_user -p$db_passwd -h $db_host $db
 true $(( i=i+1000 ))

done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.