python测试mysql数据库性能(二)
一,普通写入数据库
二,批量写入数据库
三,普通写入数据库添加事务
config = {
'host': 'localhost',
'port': 3306,
'database': 'test',
'user': 'root',
'password': '1234qwer',
'charset': 'utf8'
}
conn = pymysql.connect(**config)
cur = conn.cursor()
def timer(fn):
def _wrapper(count):
start = time.time()
fn(count)
seconds = time.time() - start
print(u"{func}函数每 {count} 条数数据写入耗时 {sec}秒".format(func=fn, count=count, sec=seconds))
return _wrapper
# 普通写入
@timer
def ordinary_insert(count):
sql = "insert into students1 (name, age, sex,id,cellphone,address,score) values ('tom666', '66', 'boy', '10066', '13900000066', 'shanghai', '66')"
for i in range(count):
cur.execute(sql)
# 批量处理
@timer
def many_insert(count):
sql = "insert into students1 (name, age, sex,id,cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
loop = count / 20
stus = (('tom666','','boy','','','shanghai',''),('tom666','','boy','','','shanghai',''),
('tom666', '', 'boy', '', '', 'shanghai', ''),('tom666','','boy','','','shanghai',''),
('tom666', '', 'boy', '', '', 'shanghai', ''),('tom666','','boy','','','shanghai',''),
('tom666', '', 'boy', '', '', 'shanghai', ''),('tom666','','boy','','','shanghai',''),
('tom666', '', 'boy', '', '', 'shanghai', ''),('tom666','','boy','','','shanghai',''),
('tom666', '', 'boy', '', '', 'shanghai', ''),('tom666','','boy','','','shanghai',''),
('tom666', '', 'boy', '', '', 'shanghai', ''),('tom666','','boy','','','shanghai',''),
('tom666', '', 'boy', '', '', 'shanghai', ''),('tom666','','boy','','','shanghai',''),
('tom666', '', 'boy', '', '', 'shanghai', ''),('tom666','','boy','','','shanghai',''),
('tom666', '', 'boy', '', '', 'shanghai', ''),('tom666','','boy','','','shanghai',''))
for i in range(int(loop)):
cur.executemany(sql, stus)
# 事务处理
@timer
def transaction_insert(count):
sql = "insert into students1 (name, age, sex,id,cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
stus = ('tom666', '', 'boy', '', '', 'shanghai', '')
if count > 0:
try:
for i in range(count):
cur.execute(sql, stus)
except Exception as e:
conn.rollback() # 事务回滚
print('事务处理失败', e)
else:
conn.commit() # 事务提交
print('事务处理成功, 关闭连接', cur.rowcount)
cur.close()
conn.close()
else:
print("输入的count有问题,无法执行数据库操作!")
def test_insert(count):
ordinary_insert(count)
many_insert(count)
transaction_insert(count)
test_insert(20)
输出结果:
E:\python_projects\practises\venv\Scripts\python.exe E:/python_projects/practises/practise20191116/p20191208.py
<function ordinary_insert at 0x0000026994A7BC18>函数每20条数数据写入耗时0.003995656967163086秒
<function many_insert at 0x0000026994A7B8B8>函数每20条数数据写入耗时0.0009996891021728516秒
事务处理成功, 关闭连接 1
<function transaction_insert at 0x0000026994A7BA68>函数每20条数数据写入耗时0.007994651794433594秒 Process finished with exit code 0
python测试mysql数据库性能(二)的更多相关文章
- python测试mysql写入性能完整实例
这篇文章主要介绍了python测试mysql写入性能完整实例,具有一定借鉴价值,需要的朋友可以参考下 本文主要研究的是python测试mysql写入性能,分享了一则完整代码,具体介绍如下. 测试环境: ...
- Python操作MySQL数据库(二)
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 下载安装: pip install pymysql 1.执行SQL语句 #!/usr/bin/env pytho ...
- 使用Python对MySQL数据库插入二十万条数据
1.当我们测试的时候需要大量的数据的时候,往往需要我们自己造数据,一条一条的加是不现实的,这时候就需要使用脚本来批量生成数据了. import pymysql import random import ...
- python + docker, 实现天气数据 从FTP获取以及持久化(二)-- python操作MySQL数据库
前言 在这一节中,我们主要介绍如何使用python操作MySQL数据库. 准备 MySQL数据库使用的是上一节中的docker容器 “test-mysql”. Python 操作 MySQL 我们使用 ...
- Python学习(二十五)—— Python连接MySql数据库
转载自http://www.cnblogs.com/liwenzhou/p/8032238.html 一.Python3连接MySQL PyMySQL 是在 Python3.x 版本中用于连接 MyS ...
- Python操作Mysql数据库时SQL语句的格式问题
一.概述 近日使用Python对Mysql数据库进行操作,遇到SQL语句死活出问题的情况.由于最初没有将异常打印出来,一直不知道原因.随后,将异常打印出来之后,通过异常信息,对代码进行修改.最终,成功 ...
- python使用mysql数据库
一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的l ...
- python专题-Mysql数据库(python2._+ Mysqldb)
Python使用MySQL数据库 MySQLdb驱动从2014年1月停止了维护. Python2 MySQLdb 一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安 ...
- Python连接MySQL数据库的多种方式
上篇文章分享了windows下载mysql5.7压缩包配置安装mysql 后续可以选择 ①在本地创建一个数据库,使用navicat工具导出远程测试服务器的数据库至本地,用于学习操作,且不影响测试服务器 ...
随机推荐
- mac php Swoole入门
一. swoole 扩展安装 安装前必须保证系统已经安装了下列软件 php-7.0 或更高版本 gcc-4.8 或更高版本 make autoconf pcre (CentOS系统可以执行命令:yum ...
- Win10下免安装版MySQL8.0.16的安装和配置
1.MySQL8.0.16解压 其中dada文件夹和my.ini配置文件是解压后手动加入的,如下图所示 2.新建配置文件my.ini放在D:\Free\mysql-8.0.16-winx64目录下 [ ...
- Letsencrypt.org CA免费证书生成
Letsencrypt.org CA免费证书使用 Let's 支持多中客户端,这里使用acme.sh客户端配置免费证书. acme.sh优点: github官方地址 纯用Shell(Unix外壳)语言 ...
- ansible之yum模块
> YUM (/usr/lib/python2.7/site-packages/ansible/modules/packaging/os/yum.py) Installs, upgrade, d ...
- httprunner学习22-正则表达式提取(extract)与校验(validate)
前言 有些接口返回的并不是json格式的内容,返回的是html或者xml格式的内容,这种就不能用前面的 content.的方法去提取了. httprunner 支持正则表达式提取(extract)参数 ...
- Bean property ‘mapperHelper’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
spring boot 报错: Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property ...
- 三大框架整合(hibernate-spring-struts2)
三大框架整合说明 一.导包---41个包 二.单独配置spring容器---applicationContext.xml (1)导入约束(beans|context|aop|tx) (2)写数据库读取 ...
- Ad-hoc--拉丁文--for this purpose only
Ad-hoc这个词来源于拉丁语,在百度上解释为“for this purpose only”, 在wiki上解释为“for this”,其中文在wiki上被解释成包含“特设的.特定目的的(地).即席的 ...
- mysql关键字--设计表时要避开,否则回报语法错误
https://dev.mysql.com/doc/refman/5.7/en/keywords.html#keywords-in-current-series Keywords and Reserv ...
- yolov2
在这篇文章中,作者首先在YOLOv1的基础上提出了改进的YOLOv2,然后提出了一种检测与分类联合训练方法,使用这种联合训练方法在COCO检测数据集(用于检测)和ImageNet分类数据集(用于分类) ...