python測(cè)試mysql寫入性能完整實(shí)例
本文主要研究的是python測(cè)試mysql寫入性能,分享了一則完整代碼,具體介紹如下。
測(cè)試環(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
測(cè)試方法:
(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倍。
完整的測(cè)試代碼如下:
#coding=utf-8
'''''
采用三種方法測(cè)試mysql.connector對(duì)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í)間
從測(cè)試效果來看,事務(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測(cè)試mysql寫入性能完整實(shí)例的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- python 寫一個(gè)性能測(cè)試工具(一)
- Python如何給你的程序做性能測(cè)試
- 通過python調(diào)用adb命令對(duì)App進(jìn)行性能測(cè)試方式
- Python內(nèi)置數(shù)據(jù)類型list各方法的性能測(cè)試過程解析
- Python實(shí)現(xiàn)性能自動(dòng)化測(cè)試竟然如此簡(jiǎn)單
- 如何使用Python標(biāo)準(zhǔn)庫進(jìn)行性能測(cè)試
- Python 3.6 性能測(cè)試框架Locust安裝及使用方法(詳解)
- 在Python中使用異步Socket編程性能測(cè)試
- python 字典(dict)遍歷的四種方法性能測(cè)試報(bào)告
- Python性能測(cè)試工具Locust安裝及使用
相關(guān)文章
詳解python __init__.py 和 __all__作用
導(dǎo)入文件夾包的時(shí)候,會(huì)運(yùn)行寫在該文件夾包下的__init__.py文件,這主要是__init__.py的作用,本文結(jié)合示例代碼介紹了python __init__.py 和 __all__作用,感興趣的朋友一起看看吧2023-02-02
Python利用 SVM 算法實(shí)現(xiàn)識(shí)別手寫數(shù)字
支持向量機(jī) (Support Vector Machine, SVM) 是一種監(jiān)督學(xué)習(xí)技術(shù),它通過根據(jù)指定的類對(duì)訓(xùn)練數(shù)據(jù)進(jìn)行最佳分離,從而在高維空間中構(gòu)建一個(gè)或一組超平面。本文將介紹通過SVM算法實(shí)現(xiàn)手寫數(shù)字的識(shí)別,需要的可以了解一下2021-12-12
python高手之路python處理excel文件(方法匯總)
用python來自動(dòng)生成excel數(shù)據(jù)文件。python處理excel文件主要是第三方模塊庫xlrd、xlwt、xluntils和pyExcelerator,除此之外,python處理excel還可以用win32com和openpyxl模塊2016-01-01
Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之基本搜索詳解
這篇文章主要介紹了Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之基本搜索,詳細(xì)分析了Python順序搜索、二分搜索的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
python自動(dòng)導(dǎo)入包的實(shí)現(xiàn)
本文主要介紹了python自動(dòng)導(dǎo)入包的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
使用Python和GDAL給圖片加坐標(biāo)系的實(shí)現(xiàn)思路(坐標(biāo)投影轉(zhuǎn)換)
這篇文章主要介紹了使用Python和GDAL給圖片加坐標(biāo)系的實(shí)現(xiàn)思路(坐標(biāo)投影轉(zhuǎn)換),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Python使用random模塊生成隨機(jī)數(shù)操作實(shí)例詳解
這篇文章主要介紹了Python使用random模塊生成隨機(jī)數(shù)操作,結(jié)合具體實(shí)例形式詳細(xì)分析了random模塊生成隨機(jī)數(shù)的各種常用技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09

