博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 删除大表数据
阅读量:5331 次
发布时间:2019-06-14

本文共 3048 字,大约阅读时间需要 10 分钟。

#!/usr/bin/env python# encoding: utf-8#@author: 东哥加油!#@file: del_tb_bigtable_statistic.py#@time: 2018/11/21 15:39import pymysqlimport datetimeimport mathimport 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#获取该条件所有的ciddef 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)

  

转载于:https://www.cnblogs.com/52shaidan/p/10106508.html

你可能感兴趣的文章
阿里巴巴面试之利用两个int值实现读写锁
查看>>
浅谈性能测试
查看>>
Winform 菜单和工具栏控件
查看>>
CDH版本大数据集群下搭建的Hue详细启动步骤(图文详解)
查看>>
巧用Win+R
查看>>
浅析原生js模仿addclass和removeclass
查看>>
Python中的greenlet包实现并发编程的入门教程
查看>>
java中遍历属性字段及值(常见方法)
查看>>
深入理解jQuery框架-框架结构
查看>>
YUI3自动加载树实现
查看>>
python知识思维导图
查看>>
当心JavaScript奇葩的逗号表达式
查看>>
App Store最新审核指南(2015年3月更新版)
查看>>
织梦MIP文章内容页图片适配百度MIP规范
查看>>
[Kali_BT]通过低版本SerialPort蓝牙渗透功能手机
查看>>
C语言学习总结(三) 复杂类型
查看>>
HNOI2018
查看>>
【理财】关于理财的网站
查看>>
Ubunt中文乱码
查看>>
《当幸福来敲门》读后
查看>>