删除千万级mysql数据的脚本(python)
嗯,一个小需求所以写了一个脚本
#!/usr/bin/env python # -*- encoding: utf-8 -*- # Python 3.9.0 # pip install pymysql ''' @File : deletedate.py @Time : 2021/11/18 16:05:31 @Author : Li Ruilong @Version : 1.0 @Contact : 1224965096@qq.com @Desc : 用于删除千万级mysql数据的脚本, ''' # here put the import lib import pymysql.cursors import time # 连接MySQL数据库 # 删除的数据量 sqlselect = "select count(*) as sum from oc_ob_task where SP_ID=999;" # 每次删除的条数 sqldelete = "delete from oc_ob_task where SP_ID=999 limit 10000 " host='192.168.50.187' port=3306 user='PT90' password='OY******8edNY' db='cloud' # 通过cursor创建游标 try: connection = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) cursor = connection.cursor() print("数据库连接成功============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================") except: print("数据库连接异常============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================") def select(sqlselect): cursor.execute(sqlselect) connection.commit() global result result = cursor.fetchone().get("sum") print("清理的数据条数", result) def delete(result, sqldelete): print(result, "开始清理============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================") while result > 10: try: cursor.execute(sqldelete) connection.commit() result = cursor.fetchone() except: cursor.rollback() cursor.execute(sqlselect) connection.commit() result = cursor.fetchone().get("sum") print("剩余数据量:", result, "=================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=======================") if __name__ == '__main__': while True: print(''' ========================== | 1.自定义配置清理 | | | | 2.默认配置清理 | | | | 3.数据量查询 | |========================| ''') i = input("请选择:\n") if i == '1': try: host = input("请输入host:\n ") port = int(input("请输入port: \n")) user = input("请输入db用户名user:\n ") password = input("请输入db用户名password:\n ") db = input("请输入数据库: \n") sqlselect = input( "查询数据量SQL:例:select count(*) as sum from oc_ob_task where SP_ID=999;\n") sqldelete = input( "批量删除SQL:例:delete from oc_ob_task where SP_ID=999 limit 10000\n ") print("清理SQL============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================") except: print("port必须为数字!") continue try: connection = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) cursor = connection.cursor() print("数据库连接成功============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================") except: print("数据库连接异常============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================") time.sleep(5) break elif i != '2' : if i == '3': select(sqlselect) continue print("\n") print("连接参数host|port|user|password|db:\n",host,"\n",port,"\n",user,"\n",password,"\n",db,"\n") print("查询数据量SQL:", sqlselect, "\n ") print("批量删除SQL:", sqldelete, "\n ") boo = input("确实是否清理!(y/n):\n") if boo == 'y': boo = input("确实是否清理!(y/n):\n") if boo == 'y': boo = input("确实是否清理!(y/n):\n") if boo == 'y': try: select(sqlselect) delete(result, sqldelete) except: print("数据库连接异常============================================", time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), "=========================") break else: print("退出成功") continue else: print("退出成功") continue else: print("退出成功") continue
加油,愿被这世界温柔以待 ^_^
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构