日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

InnoDB delete from xxx速度暴慢原因 - Tech - JavaEye...

 BigGuo 2010-03-15





step1,一個(gè)簡(jiǎn)單的聯(lián)系人表 
Java代碼 復(fù)制代碼
  1. CREATE TABLE `contact784` (   
  2.     `cid` bigint AUTO_INCREMENT NOT NULL,   
  3.     `uid` bigint NOT NULL,   
  4.     `email` varchar(128) NOT NULL,   
  5.     `name` varchar(64) NOT NULL,   
  6.     `mobile` varchar(16)  NULL,   
  7.     `atime` timestamp NULL,   
  8.     `type` enum('BLACK','WHITE','NORMAL') NOT NULl default 'NORMAL',   
  9.     `info` text NULL,   
  10.     `memo` varchar(1024)  NULL,   
  11.      PRIMARY key(`cid`)   
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT = 100;   
  13. ALTER TABLE `contact784` ADD UNIQUE INDEX uniq_uid_email(`uid`,`email`);  


step2,插入了100W數(shù)據(jù): 
Java代碼 復(fù)制代碼
  1. # -*- coding: utf-8 -*-     
  2. #@author python.han@gmail.com   
  3.   
  4. import MySQLdb   
  5. import random   
  6. import string   
  7. import threading   
  8. import time   
  9.   
  10. domains = ['org','com.cn','qq.com','yahoo.com','163.com','com','cn','sina.cn','sina.com']   
  11. host = "localhost"  
  12. user = "xx"  
  13. pwd = "xx"  
  14. db = "t3"  
  15.   
  16. def getRandomValue():   
  17.   email = ""  
  18.   s = ""  
  19.   for x in range(random.randint(1,10)):   
  20.     s += random.choice(string.letters)   
  21.   b = list(s)   
  22.   domain = ''.join(b)+"."+random.choice(domains)   
  23.   email = s+"@"+domain   
  24.   return email,s   
  25.   
  26.   
  27. def insert(count):   
  28.   conn=MySQLdb.connect(host=host,user=user,passwd=pwd,db=db)    
  29.   cursor=conn.cursor()   
  30.   for cid in xrange(count):   
  31.     uid = random.randint(1000000000,9999999999)   
  32.     email,name = getRandomValue()   
  33.     sql = "insert into contact784(uid,email,name) values (%d,'%s', '%s')" %(uid,email,name)   
  34.     n=cursor.execute(sql)    
  35.   cursor.close()   
  36.   conn.commit ()   
  37.   conn.close()   
  38.   
  39.   
  40. if __name__=='__main__':   
  41.   
  42.   start = time.clock()   
  43.   for i in range(100):   
  44.     worker = threading.Thread(target = insert(10000))   
  45.     worker.start()   
  46.   end = time.clock()   
  47.   print "elsaped:%s" %(end-start)  


step3,要重新單線程插入,需要把數(shù)據(jù)清空. 
因?yàn)閜ython多線程由于GIL的關(guān)系,實(shí)際上上面的100個(gè)線程只產(chǎn)生了一個(gè)連接,需要測(cè)試一下純單線程插入是不是要快些:) 

執(zhí)行:delete from contact784 
半小時(shí)沒有執(zhí)行完畢! 

診斷方式: 
1,iostat ,top等查看磁盤io很大 
2,inotifywatch發(fā)現(xiàn)io的事件非常多 

原因:在大表上使用delete from 清空一個(gè)表是非常慢的。因?yàn)镮nnoDB必須處理表中的每一行,根據(jù)InnoDB的事務(wù)設(shè)計(jì)原則,首先需要把“刪除動(dòng)作”寫入“事務(wù)日志”,然后寫入實(shí)際的表。所以,清空大表的時(shí)候,最好直接drop table然后重建。 
注: 
在delete from 執(zhí)行的過程中: 
用:select count(*) from contact784;發(fā)現(xiàn)表的數(shù)據(jù)量一直是100行 
用:explain select count(*) from contact784;可以發(fā)現(xiàn)數(shù)量一直在減少,顯示當(dāng)前 

784是是因?yàn)榍懊孢@個(gè)文章的原因“ 
http://hanyh./blog/431323 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多