python測試mysql寫入性能完整實例
本文主要研究的是python測試mysql寫入性能,分享了一則完整代碼,具體介紹如下。
測試環(huán)境:
(1) 阿里云服務器centos 6.5
(2) 2G內存
(3) 普通硬盤
(4) mysql 5.1.73 數據庫存儲引擎為 InnoDB
(5) python 2.7
(6) 客戶端模塊 mysql.connector
測試方法:
(1) 普通寫入
(2) 批量寫入
(3) 事務加批量寫入
普通寫入:
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條數據
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))
#并不是元組里的數據越多越好
for i in range(loop):
cur.executemany(sql, stus)
事務加批量寫入,每次批量寫入20條數據,每20個批量寫入作為一次事務提交
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))
#并不是元組里的數據越多越好
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()
實驗結果如下
數量 普通寫入 many寫入 事務加many寫入 1萬 26.7s 1.7s 0.5s 10萬 266s 19s 5s 100萬 2553s 165s 49s
批量寫入,相比于普通的多次寫入,減少了網絡傳輸次數,因而寫入速度加快。
不論是單次寫入還是批量寫入,數據庫內部都要開啟一個事務以保證寫入動作的完整,如果在應用層,我們自己開啟事物,那么就可以避免每一次寫入數據庫自己都開啟事務的開銷,從而提升寫入速度。
事務加批量寫入速度大概是批量寫入速度的3倍,是普通寫入的50倍。
完整的測試代碼如下:
#coding=utf-8
'''''
采用三種方法測試mysql.connector對mysql的寫入性能,其他的例如mysqldb和pymysql客戶端庫的寫入性能應該和mysql.connector一致
采用批量寫入時,由于減少了網絡傳輸的次數因而速度加快
開啟事務,多次寫入后再提交事務,其寫入速度也會顯著提升,這是由于單次的insert,數據庫內部也會開啟事務以保證一次寫入的完整性
如果開啟事務,在事務內執(zhí)行多次寫入操作,那么就避免了每一次寫入都開啟事務,因而也會節(jié)省時間
從測試效果來看,事務加批量寫入的速度大概是批量寫入的3倍,是普通寫入的50倍
數量 普通寫入 many寫入 事務加many寫入
1萬 26.7s 1.7s 0.5s
10萬 266s 19s 5s
100萬 2553s 165s 49s
將autocommit設置為true,執(zhí)行insert時會直接寫入數據庫,否則在execute 插入命令時,默認開啟事物,必須在最后commit,這樣操作實際上減慢插入速度
此外還需要注意的是mysql的數據庫存儲引擎如果是MyISAM,那么是不支持事務的,InnoDB 則支持事務
'''
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}函數每{count}條數數據寫入耗時{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))
#并不是元組里的數據越多越好
for i in range(loop):
cur.executemany(sql, stus)
#事務加批量
@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))
#并不是元組里的數據越多越好
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'參數錯誤'
總結
以上就是本文關于python測試mysql寫入性能完整實例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
詳解python __init__.py 和 __all__作用
導入文件夾包的時候,會運行寫在該文件夾包下的__init__.py文件,這主要是__init__.py的作用,本文結合示例代碼介紹了python __init__.py 和 __all__作用,感興趣的朋友一起看看吧2023-02-02
python高手之路python處理excel文件(方法匯總)
用python來自動生成excel數據文件。python處理excel文件主要是第三方模塊庫xlrd、xlwt、xluntils和pyExcelerator,除此之外,python處理excel還可以用win32com和openpyxl模塊2016-01-01
使用Python和GDAL給圖片加坐標系的實現思路(坐標投影轉換)
這篇文章主要介紹了使用Python和GDAL給圖片加坐標系的實現思路(坐標投影轉換),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

