分批次删除大表数据的shell脚本
#!/bin/bash # 分别是主机名,端口,用户,密码,数据库,表名称,字段名称 readonly HOST="XXX" readonly PORT="3306" readonly USERNAME="tmp" readonly PASSWD="tmp" readonly DATABASE_NAME="YYY" readonly TABLE_NAME="ZZZ" readonly COLUMN_NAME="ts" # 分别是 开始时间,结束时间,时间间隔,休息时间,每次删除数据的行数 readonly START_TIME="1525788660" readonly END_TIME="1573394400" readonly SETP_TIME="86400" readonly SLEPP_TIME="20" readonly STEP_DELETE="10000" # shellcheck disable=SC1009 function delete_data() { for ((i=0; i < 10000; i++)); do echo "$i" num=$(mysql -h${HOST} -P${PORT} -e "use YYY;delete from ZZZ where ts<1573394400 limit 10000;select row_count() t;") num=${num:1:20} num=$(echo $num |sed 's/ //g') # 删除变量两端的空格 echo "############已经删除行数:" echo $num echo "#######################" if [ "${num}" = "0" ];then break fi sleep 2 done } delete_data
igoodful@qq.com