python测试mysql写入性能完整实例


Posted in Python onJanuary 18, 2018

本文主要研究的是python测试mysql写入性能,分享了一则完整代码,具体介绍如下。

测试环境:

(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,数据库内部也会开启事务以保证一次写入的完整性 
如果开启事务,在事务内执行多次写入操作,那么就避免了每一次写入都开启事务,因而也会节省时间 
从测试效果来看,事务加批量写入的速度大概是批量写入的3倍,是普通写入的50倍 
数量  普通写入   many写入  事务加many写入 
1万  26.7s  1.7s    0.5s 
10万  266s   19s    5s 
100万 2553s   165s    49s 
 
将autocommit设置为true,执行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写入性能完整实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
精确查找PHP WEBSHELL木马的方法(1)
Apr 12 Python
python简单程序读取串口信息的方法
Mar 13 Python
Python使用Selenium+BeautifulSoup爬取淘宝搜索页
Feb 24 Python
python 反向输出字符串的方法
Jul 16 Python
Python读取txt内容写入xls格式excel中的方法
Oct 11 Python
python实现的登录与提交表单数据功能示例
Sep 25 Python
Python 中使用 PyMySQL模块操作数据库的方法
Nov 10 Python
python中pandas库中DataFrame对行和列的操作使用方法示例
Jun 14 Python
SpringBoot首页设置解析(推荐)
Feb 11 Python
python中Pexpect的工作流程实例讲解
Mar 02 Python
用gpu训练好的神经网络,用tensorflow-cpu跑出错的原因及解决方案
Mar 03 Python
Pandas-DataFrame知识点汇总
Mar 16 Python
浅谈flask截获所有访问及before/after_request修饰器
Jan 18 #Python
flask中主动抛出异常及统一异常处理代码示例
Jan 18 #Python
浅谈Django学习migrate和makemigrations的差别
Jan 18 #Python
Python机器学习logistic回归代码解析
Jan 17 #Python
酷! 程序员用Python带你玩转冲顶大会
Jan 17 #Python
Python建立Map写Excel表实例解析
Jan 17 #Python
Python冲顶大会 快来答题!
Jan 17 #Python
You might like
PHP COOKIE设置为浏览器进程
2009/06/21 PHP
在字符串指定位置插入一段字符串的php代码
2010/02/16 PHP
微信营销平台系统?刮刮乐的开发
2014/06/10 PHP
smarty实现多级分类的方法
2014/12/05 PHP
PHP脚本监控Nginx 502错误并自动重启php-fpm
2015/05/13 PHP
Yii2使用$this->context获取当前的Module、Controller(控制器)、Action等
2017/03/29 PHP
Extjs 4.x 得到form CheckBox 复选框的值
2014/05/04 Javascript
jQuery实现简单漂亮的Nav导航菜单效果
2017/03/29 jQuery
js获取css的各种样式并且设置他们的方法
2017/08/22 Javascript
详解AngularJS跨页面传值(ui-router)
2017/08/23 Javascript
微信小程序使用wx.request请求服务器json数据并渲染到页面操作示例
2019/03/30 Javascript
js实现随机div颜色位置 类似满天星效果
2019/10/24 Javascript
vue-dplayer 视频播放器实例代码
2019/11/08 Javascript
node.js使用stream模块实现自定义流示例
2020/02/13 Javascript
[01:38]DOTA2 2015国际邀请赛中国区预选赛 Showopen
2015/06/01 DOTA
[01:33]真香警告!DOTA2勇士令状不朽珍藏Ⅱ饰品欣赏
2018/06/26 DOTA
[53:23]Secret vs Liquid 2018国际邀请赛淘汰赛BO3 第二场 8.25
2018/08/29 DOTA
python里大整数相乘相关技巧指南
2014/09/12 Python
Python 闭包的使用方法
2017/09/07 Python
python pycurl验证basic和digest认证的方法
2018/05/02 Python
python中强大的format函数实例详解
2018/12/05 Python
PyCharm 创建指定版本的 Django(超详图解教程)
2019/06/18 Python
python pandas模块基础学习详解
2019/07/03 Python
Django Docker容器化部署之Django-Docker本地部署
2019/10/09 Python
Django项目中使用JWT的实现代码
2019/11/04 Python
HTML页面中添加Canvas标签示例
2015/01/01 HTML / CSS
简单的HTML5初步入门教程
2015/09/29 HTML / CSS
详解快速开发基于 HTML5 网络拓扑图应用
2018/01/08 HTML / CSS
Aerosoles爱柔仕官网:美国舒软女鞋品牌
2017/07/17 全球购物
英文翻译的自我评价语句
2013/10/04 职场文书
运动会通讯稿100字
2014/01/31 职场文书
小学“向国旗敬礼”网上签名寄语活动总结
2014/09/27 职场文书
教师党的群众路线教育实践活动剖析材料
2014/10/09 职场文书
铣工实训报告
2014/11/05 职场文书
世界水日宣传活动总结
2015/02/09 职场文书
《迟到》教学反思
2016/02/24 职场文书