国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Python批量刪除mysql中千萬級大量數(shù)據(jù)的腳本分享

瀏覽:46日期:2022-07-03 13:46:07

場景描述

線上mysql數(shù)據(jù)庫里面有張表保存有每天的統(tǒng)計結(jié)果,每天有1千多萬條,這是我們意想不到的,統(tǒng)計結(jié)果咋有這么多。運維找過來,磁盤占了200G,最后問了運營,可以只保留最近3天的,前面的數(shù)據(jù),只能刪了。刪,怎么刪?因為這是線上數(shù)據(jù)庫,里面存放有很多其它數(shù)據(jù)表,如果直接刪除這張表的數(shù)據(jù),肯定不行,可能會對其它表有影響。嘗試每次只刪除一天的數(shù)據(jù),還是卡頓的厲害,沒辦法,寫個Python腳本批量刪除吧。具體思路是:

每次只刪除一天的數(shù)據(jù); 刪除一天的數(shù)據(jù),每次刪除50000條; 一天的數(shù)據(jù)刪除完,開始刪除下一天的數(shù)據(jù);

Python代碼

# -*-coding:utf-8 -*-import sys# 這是我們內(nèi)部封裝的Python Modulesys.path.append(’/var/lib/hadoop-hdfs/scripts/python_module2’)import keguang.commons as commonsimport keguang.timedef as timedefimport keguang.sql.mysqlclient as mysqldef run(starttime, endtime, regx): tb_name = ’statistic_ad_image_final_count’ days = timedef.getDays(starttime,endtime,regx) # 遍歷刪除所有天的數(shù)據(jù) for day in days: print ’%s 數(shù)據(jù)刪除開始’%(day) mclient = getConn() sql = ’’’ select 1 from %s where date = ’%s’ limit 1 ’’’%(tb_name, day) print sql result = mclient.query(sql) # 如果查詢到了這一天的數(shù)據(jù),繼續(xù)刪除 while result is not (): sql = ’delete from %s where date = '%s' limit 50000’%(tb_name, day) print sql mclient.execute(sql) sql = ’’’ select 1 from %s where date = ’%s’ limit 1 ’’’%(tb_name, day) print sql result = mclient.query(sql) print ’%s 數(shù)據(jù)刪除完成’%(day) mclient.close()# 返回mysql 連接def getConn(): return mysql.MysqlClient(host = ’0.0.0.0’, user = ’test’, passwd = ’test’, db= ’statistic’)if __name__ == ’__main__’: regx = ’%Y-%m-%d’ yesday = timedef.getYes(regx, -1) starttime = ’2019-08-17’ endtime =’2019-08-30’ run(starttime, endtime, regx)

以上就是Python批量刪除mysql中千萬級大量數(shù)據(jù)的腳本的詳細(xì)內(nèi)容,更多關(guān)于python 刪除MySQL數(shù)據(jù)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 包头市| 兴隆县| 彭阳县| 洛隆县| 新和县| 临沭县| 宜兰市| 谷城县| 英吉沙县| 道真| 建平县| 济南市| 朔州市| 杭锦后旗| 麦盖提县| 榕江县| 海林市| 凉城县| 晋州市| 高尔夫| 渝北区| 石家庄市| 永清县| 文昌市| 施秉县| 信丰县| 双辽市| 镇江市| 金沙县| 绥德县| 印江| 德江县| 革吉县| 清丰县| 恩平市| 阳东县| 青田县| 拉萨市| 柘城县| 科技| 仙居县|