python 删除大表数据
2018-12-12 09:27:05来源:博客园 阅读 ()
#!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: del_tb_bigtable_statistic.py #@time: 2018/11/21 15:39 import pymysql import datetime import math import time #获取连接 def get_conn(): conn = None try: conn = pymysql.connect( host="192.168.1.2", port=3306, user="root", passwd="mysqlpassword", charset="utf8", ) except Exception as err: print(err) return conn #查询语句执行 def get_data(sql): conn = get_conn() cur = conn.cursor() cur.execute(sql) data = cur.fetchall() conn.close() return data #93天前的时间戳 # 2018-07-24 00:00:00 转成毫秒时间戳 def get_pdate_begin(xday): now_time = datetime.datetime.now() step_time = datetime.timedelta(days=xday) yes_time = now_time - step_time pdate = yes_time.strftime('%Y%m%d') print(pdate) return pdate #数据备份,放到tb_bigtable_statistic_hist表中 def data_bak(xday): print("开始时间:",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) conn = get_conn() cur = conn.cursor() cidlist = data_zk(xday) if cidlist == 0: print('当天无注单') else: for cids in cidlist: try: sql = '''insert into db_order.tb_bigtable_statistic_hist \ select * from db_order.tb_bigtable_statistic \ where cid in( %s )''' % cids cur.execute(sql) conn.commit() except: print('备份失败!!!') conn.rollback() conn.close() exit(99) conn.close() print("结束时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) #组装cid成in的条件(....),5000个cid为一组 def data_zk(xday): conn = get_conn() cur = conn.cursor() cid = get_cid(xday) var1 = "-999" i = 0 list = [] if cid.__len__() > 0: for one in cid: var1=var1+","+str(one[0]) i=i+1 if(i==2000): list.append(var1) var1 = "-999" i=0 list.append(var1) return list else: return 0 #获取该条件所有的cid def get_cid(xday): pdate = get_pdate_begin(xday) sql = '''SELECT cid FROM db_order.tb_bigtable_statistic WHERE pdate = %s limit 20000''' % (pdate) cid = get_data(sql) return cid #删除数据 def del_data(xday): print("删除开始时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) conn = get_conn() cur = conn.cursor() cidlist = data_zk(xday) if cidlist == 0: print('当天无注单') else: for cids in cidlist: try: sql = '''delete from db_order.tb_bigtable_statistic \ where cid in( %s )''' % cids cur.execute(sql) conn.commit() except: print('备份失败!!!') conn.rollback() conn.close() exit(99) conn.close() print("删除结束时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) def move_data(xday): data_bak(xday) del_data(xday) if __name__ == '__main__': move_data(93)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash