欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python測試mysql寫入性能完整實(shí)例

 更新時(shí)間:2018年01月18日 10:13:34   作者:kwsy2008  
這篇文章主要介紹了python測試mysql寫入性能完整實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下

本文主要研究的是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)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • Python機(jī)器學(xué)習(xí)NLP自然語言處理Word2vec電影影評建模

    Python機(jī)器學(xué)習(xí)NLP自然語言處理Word2vec電影影評建模

    本文是Python機(jī)器學(xué)習(xí)NLP自然語言處理系列文章,帶大家開啟一段學(xué)習(xí)自然語言處理 (NLP) 的旅程. 本篇文章主要學(xué)習(xí)NLP自然語言處理基本操作Word2vec電影影評建模
    2021-09-09
  • python實(shí)現(xiàn)excel讀寫數(shù)據(jù)

    python實(shí)現(xiàn)excel讀寫數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了python操作EXCEL讀數(shù)據(jù)、寫數(shù)據(jù)的實(shí)例源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python自動(dòng)發(fā)郵件腳本

    Python自動(dòng)發(fā)郵件腳本

    本文主要介紹了Python自動(dòng)發(fā)郵件腳本的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-03-03
  • NumPy數(shù)組復(fù)制與視圖詳解

    NumPy數(shù)組復(fù)制與視圖詳解

    NumPy 數(shù)組的復(fù)制和視圖是兩種不同的方式來創(chuàng)建新數(shù)組,它們之間存在著重要的區(qū)別,本文將給大家詳細(xì)介紹一下NumPy數(shù)組復(fù)制與視圖,并通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Python編程中用close()方法關(guān)閉文件的教程

    Python編程中用close()方法關(guān)閉文件的教程

    這篇文章主要介紹了Python編程中用close()方法關(guān)閉文件的教程,是Python編程入門中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • 詳解Python中的Dict

    詳解Python中的Dict

    這篇文章主要為大家介紹了Python中的Dict,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • python pandas 組內(nèi)排序、單組排序、標(biāo)號(hào)的實(shí)例

    python pandas 組內(nèi)排序、單組排序、標(biāo)號(hào)的實(shí)例

    下面小編就為大家分享一篇python pandas 組內(nèi)排序、單組排序、標(biāo)號(hào)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python使用Socket實(shí)現(xiàn)簡單聊天程序

    Python使用Socket實(shí)現(xiàn)簡單聊天程序

    這篇文章主要介紹了Python使用Socket實(shí)現(xiàn)簡單聊天程序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Python使用defaultdict解決字典默認(rèn)值

    Python使用defaultdict解決字典默認(rèn)值

    本文主要介紹了Python使用defaultdict解決字典默認(rèn)值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • python程序?qū)崿F(xiàn)BTC(比特幣)挖礦的完整代碼

    python程序?qū)崿F(xiàn)BTC(比特幣)挖礦的完整代碼

    這篇文章主要介紹了python程序?qū)崿F(xiàn)BTC(比特幣)挖礦的完整代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01

最新評論