日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql百万数据删除_【MySQL】删除大量数据的具体实现

發(fā)布時間:2025/3/19 数据库 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql百万数据删除_【MySQL】删除大量数据的具体实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

ourmysql博客中提供了 《大表刪除數(shù)據(jù)的思路》,對于大表依據(jù)主鍵刪除的思路是必須的,刪除幾千萬的數(shù)據(jù)還算是比較簡單的,如果你的數(shù)據(jù)庫中的表高達數(shù)百億條記錄 ,刪除其中的幾十億,就需要考慮可用性的問題了。上述文中的 利用生成的文本方式有些不妥。

我的方法是利用存儲過程,游標,先根據(jù)條件獲取要刪除的主鍵,然后依據(jù)主鍵刪除,考慮到刪除50億條記錄耗費將近7天的時間(事后得出),必須后臺執(zhí)行。使用python 工具寫一個腳本,可以針對多個服務器進行并行操作。

1 在各個服務器上創(chuàng)建存過!

delimiter //

CREATE ?PROCEDURE `proc_del_tab`(in com_num int , in push_time datetime )

begin

declare curid bigint ;

DECLARE rowid bigint ;

declare no_more_departments int ;

declare curs cursor for

select id

from

tab

WHERE

v3 < push_time ;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_departments = 1;

SET no_more_departments=0;

set rowid = 1 ;

set autocommit = 0 ;

open curs ;

REPEAT

fetch curs into curid ;

delete from tab where id = curid ;

set rowid = rowid + 1 ;

if rowid % com_num = 0

then

commit;

end if ;

UNTIL no_more_departments

END REPEAT;

commit ;

close curs ;

end;

//

delimiter ;

2 部署python 腳本:

#!/usr/bin/env python

from MySQLdb import *

import sys

import threading

import time

import os

def now() :

#return str('2011-01-31 00:00:00')

return str( time.strftime( '%Y-%m-%d %H:%M:%S' , time.localtime() ) )

def log( strs , logs ) :

f = file( logs , 'a' , 0 )

f.write( now() + ' ' + str(strs) + '\n' )

f.close()

def delining( cur , logs ) :

sql = "SET SQL_LOG_BIN=0"

try :

cur['dsn'].execute( sql )

except Exception , e :

log( 'Set SQL_LOG_BIN OFF' + str(e) , logs )

sql = "call proc_del_tab_yang( 3000 , '%s' )" % ('2011-01-31 00:00:00')

log( 'starting process %s' % ( cur['addr'] ) , logs )

try :

cur['dsn'].execute( sql )

except Exception , e :

log( 'Execute Procedure ' + str(e) , logs )

sql = "SET SQL_LOG_BIN=1"

try :

cur['dsn'].execute( sql )

except Exception , e :

log( 'Set SQL_LOG_BIN ON' + str(e) , logs )

log( 'process %s End' % ( cur['addr'] ) , logs )

def main() :

logs = "/root/yangql/python/del_test_tab.log"

server_list=['10.250.7.110']

luser="yang"

lpasswd="yang"

con = []

for addr in server_list :

cons = None

try :

cons = connect( host = addr , user = luser , passwd = lpasswd , port = 3307 , db = 'newcloudapp' )

except Exception , e :

log( 'On Connect %s ' % ( addr ) + str(e) , logs )

continue

con.append( ?{ 'dsn':cons , 'addr':addr } )

cur = []

for cons in con :

try :

cur.append( { 'dsn':cons['dsn'].cursor( cursorclass = cursors.DictCursor ) , 'addr':cons['addr'] } )

except Exception , e :

log( 'On Cusros %s ' % ( cons['addr'] ) + str(e) ?, logs )

continue

thpool = []

for curs in cur :

th = threading.Thread(target = delining ,args=( curs , logs ) )

thpool.append( th )

for th in thpool :

th.start()

for th in thpool :

threading.Thread.join( th )

while True :

if threading.activeCount() < 2 :

break

else :

time.sleep(1)

continue

for curs in cur :

try :

curs['dsn'].close()

except Exception , e :

log( 'On Close Cusros %s ' % ( curs['addr'] ) + str(e) ?, logs )

continue

for cons in con :

try :

cons['dsn'].close()

except Exception , e :

log( 'On Close Connect %s ' % ( str(e) ?) , logs )

continue

if __name__ == '__main__' :

main()

歡迎大家提出更好的方法。。

總結

以上是生活随笔為你收集整理的mysql百万数据删除_【MySQL】删除大量数据的具体实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。