import MySQLdb
import time
from DBUtils.PooledDB import PooledDB
db_host="ali-trade-01.mysql.rds.aliyuncs.com"
db_name='storemisc'
sql='''delete from logistics_info_logs where create_time >= '2019-03-01' and create_time < '2019-05-01' limit 3000'''
try:
out_pool = PooledDB(MySQLdb, 5, host=db_host, user='admin', passwd='123456', db=db_name, port=3306)
out_db = out_pool.connection()
out_cursor = out_db.cursor()
recoder_num=1
while recoder_num>0:
out_db.begin()
recoder_num=out_cursor.execute(sql)
recoder_num=int(recoder_num)
out_db.commit()
print recoder_num
time.sleep(20)
except MySQLdb.Error as e:
ok_msg="Error %d: %s" % (e.args[0], e.args[1])
print ok_msg
对执行时间没有要求的可以用以上脚本,如果想要又快速又不产生延时的话,后期可以修改成多线程执行,并主动去检查主从延时的时间和相关指标,如果大于设定的阈值,则暂停或减少线程执行