python測試mysql寫入性能完整實(shí)例
本文主要研究的是python測試mysql寫入性能,分享了一則完整代碼,具體介紹如下。
測試環(huán)境:
(1) 阿里云服務(wù)器centos 6.5
(2) 2G內(nèi)存
(3) 普通硬盤
(4) mysql 5.1.73 數(shù)據(jù)庫存儲(chǔ)引擎為 InnoDB
(5) python 2.7
(6) 客戶端模塊 mysql.connector
測試方法:
(1) 普通寫入
(2) 批量寫入
(3) 事務(wù)加批量寫入
普通寫入:
def ordinary_insert(count): sql = "insert into stu(name,age,class)values('test mysql insert',30,8)" for i in range(count): cur.execute(sql)
批量寫入,每次批量寫入20條數(shù)據(jù)
def many_insert(count): sql = "insert into stu(name,age,class)values(%s,%s,%s)" loop = count/20 stus = (('test mysql insert', 30, 30), ('test mysql insert', 30, 31), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 30), ('test mysql insert', 30, 31), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32)) #并不是元組里的數(shù)據(jù)越多越好 for i in range(loop): cur.executemany(sql, stus)
事務(wù)加批量寫入,每次批量寫入20條數(shù)據(jù),每20個(gè)批量寫入作為一次事務(wù)提交
def transaction_insert(count): sql = "insert into stu(name,age,class)values(%s,%s,%s)" insert_lst = [] loop = count/20 stus = (('test mysql insert', 30, 30), ('test mysql insert', 30, 31), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 30), ('test mysql insert', 30, 31), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32)) #并不是元組里的數(shù)據(jù)越多越好 for i in range(loop): insert_lst.append((sql,stus)) if len(insert_lst) == 20: conn.start_transaction() for item in insert_lst: cur.executemany(item[0], item[1]) conn.commit() print '0k' insert_lst = [] if len(insert_lst) > 0: conn.start_transaction() for item in insert_lst: cur.executemany(item[0], item[1]) conn.commit()
實(shí)驗(yàn)結(jié)果如下
數(shù)量 普通寫入 many寫入 事務(wù)加many寫入 1萬 26.7s 1.7s 0.5s 10萬 266s 19s 5s 100萬 2553s 165s 49s
批量寫入,相比于普通的多次寫入,減少了網(wǎng)絡(luò)傳輸次數(shù),因而寫入速度加快。
不論是單次寫入還是批量寫入,數(shù)據(jù)庫內(nèi)部都要開啟一個(gè)事務(wù)以保證寫入動(dòng)作的完整,如果在應(yīng)用層,我們自己開啟事物,那么就可以避免每一次寫入數(shù)據(jù)庫自己都開啟事務(wù)的開銷,從而提升寫入速度。
事務(wù)加批量寫入速度大概是批量寫入速度的3倍,是普通寫入的50倍。
完整的測試代碼如下:
#coding=utf-8 ''''' 采用三種方法測試mysql.connector對mysql的寫入性能,其他的例如mysqldb和pymysql客戶端庫的寫入性能應(yīng)該和mysql.connector一致 采用批量寫入時(shí),由于減少了網(wǎng)絡(luò)傳輸?shù)拇螖?shù)因而速度加快 開啟事務(wù),多次寫入后再提交事務(wù),其寫入速度也會(huì)顯著提升,這是由于單次的insert,數(shù)據(jù)庫內(nèi)部也會(huì)開啟事務(wù)以保證一次寫入的完整性 如果開啟事務(wù),在事務(wù)內(nèi)執(zhí)行多次寫入操作,那么就避免了每一次寫入都開啟事務(wù),因而也會(huì)節(jié)省時(shí)間 從測試效果來看,事務(wù)加批量寫入的速度大概是批量寫入的3倍,是普通寫入的50倍 數(shù)量 普通寫入 many寫入 事務(wù)加many寫入 1萬 26.7s 1.7s 0.5s 10萬 266s 19s 5s 100萬 2553s 165s 49s 將autocommit設(shè)置為true,執(zhí)行insert時(shí)會(huì)直接寫入數(shù)據(jù)庫,否則在execute 插入命令時(shí),默認(rèn)開啟事物,必須在最后commit,這樣操作實(shí)際上減慢插入速度 此外還需要注意的是mysql的數(shù)據(jù)庫存儲(chǔ)引擎如果是MyISAM,那么是不支持事務(wù)的,InnoDB 則支持事務(wù) ''' import time import sys import mysql.connector reload(sys) sys.setdefaultencoding('utf-8') config = { 'host': '127.0.0.1', 'port': 3306, 'database': 'testsql', 'user': 'root', 'password': 'sheng', 'charset': 'utf8', 'use_unicode': True, 'get_warnings': True, 'autocommit':True } conn = mysql.connector.connect(**config) cur = conn.cursor() def time_me(fn): def _wrapper(*args, **kwargs): start = time.time() fn(*args, **kwargs) seconds = time.time() - start print u"{func}函數(shù)每{count}條數(shù)數(shù)據(jù)寫入耗時(shí){sec}秒".format(func = fn.func_name,count=args[0],sec=seconds) return _wrapper #普通寫入 @time_me def ordinary_insert(count): sql = "insert into stu(name,age,class)values('test mysql insert',30,8)" for i in range(count): cur.execute(sql) #批量 @time_me def many_insert(count): sql = "insert into stu(name,age,class)values(%s,%s,%s)" loop = count/20 stus = (('test mysql insert', 30, 30), ('test mysql insert', 30, 31), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 30), ('test mysql insert', 30, 31), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32)) #并不是元組里的數(shù)據(jù)越多越好 for i in range(loop): cur.executemany(sql, stus) #事務(wù)加批量 @time_me def transaction_insert(count): sql = "insert into stu(name,age,class)values(%s,%s,%s)" insert_lst = [] loop = count/20 stus = (('test mysql insert', 30, 30), ('test mysql insert', 30, 31), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 30), ('test mysql insert', 30, 31), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32) ,('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32), ('test mysql insert', 30, 32)) #并不是元組里的數(shù)據(jù)越多越好 for i in range(loop): insert_lst.append((sql,stus)) if len(insert_lst) == 20: conn.start_transaction() for item in insert_lst: cur.executemany(item[0], item[1]) conn.commit() print '0k' insert_lst = [] if len(insert_lst) > 0: conn.start_transaction() for item in insert_lst: cur.executemany(item[0], item[1]) conn.commit() def test_insert(count): ordinary_insert(count) many_insert(count) transaction_insert(count) if __name__ == '__main__': if len(sys.argv) == 2: loop = int(sys.argv[1]) test_insert(loop) else: print u'參數(shù)錯(cuò)誤'
總結(jié)
以上就是本文關(guān)于python測試mysql寫入性能完整實(shí)例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
- python 寫一個(gè)性能測試工具(一)
- Python如何給你的程序做性能測試
- 通過python調(diào)用adb命令對App進(jìn)行性能測試方式
- Python內(nèi)置數(shù)據(jù)類型list各方法的性能測試過程解析
- Python實(shí)現(xiàn)性能自動(dòng)化測試竟然如此簡單
- 如何使用Python標(biāo)準(zhǔn)庫進(jìn)行性能測試
- Python 3.6 性能測試框架Locust安裝及使用方法(詳解)
- 在Python中使用異步Socket編程性能測試
- python 字典(dict)遍歷的四種方法性能測試報(bào)告
- Python性能測試工具Locust安裝及使用
相關(guān)文章
Python機(jī)器學(xué)習(xí)NLP自然語言處理Word2vec電影影評建模
本文是Python機(jī)器學(xué)習(xí)NLP自然語言處理系列文章,帶大家開啟一段學(xué)習(xí)自然語言處理 (NLP) 的旅程. 本篇文章主要學(xué)習(xí)NLP自然語言處理基本操作Word2vec電影影評建模2021-09-09python實(shí)現(xiàn)excel讀寫數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了python操作EXCEL讀數(shù)據(jù)、寫數(shù)據(jù)的實(shí)例源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Python編程中用close()方法關(guān)閉文件的教程
這篇文章主要介紹了Python編程中用close()方法關(guān)閉文件的教程,是Python編程入門中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05python pandas 組內(nèi)排序、單組排序、標(biāo)號(hào)的實(shí)例
下面小編就為大家分享一篇python pandas 組內(nèi)排序、單組排序、標(biāo)號(hào)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python使用Socket實(shí)現(xiàn)簡單聊天程序
這篇文章主要介紹了Python使用Socket實(shí)現(xiàn)簡單聊天程序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Python使用defaultdict解決字典默認(rèn)值
本文主要介紹了Python使用defaultdict解決字典默認(rèn)值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04python程序?qū)崿F(xiàn)BTC(比特幣)挖礦的完整代碼
這篇文章主要介紹了python程序?qū)崿F(xiàn)BTC(比特幣)挖礦的完整代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01