Python的MySQL驱动pymysql与mysqlclient性能对比

2018-08-21T02:43:56

Python版本3.6

测试语句:

select * from FOO;

 

mysql终端直接执行:

46410 rows in set (0.10 sec)

 

 

python程序需安装profilehooks进行调用耗时分析

 

pymysql驱动测试程序:

# 安装:pip install pymysql

from profilehooks import profile
import pymysql.cursors
import pymysql
connection = pymysql.connect(host='localhost', user='root', db='foo')
c = connection.cursor()

@profile(immediate=True)
def read_by_pymysql():
    c.execute("select * from FOO;")
    res = c.fetchall()



read_by_pymysql()

分析结果:耗时2.4s,与原始mysql读取差一个数量级

 

 

mysqlclient驱动测试程序:

# 安装:pip install mysqlclient 或 pip install git+https://github.com/PyMySQL/mysqlclient-python.git

from profilehooks import profile
import MySQLdb

connection = MySQLdb.connect(host='localhost', user='root', db='foo')
c = connection.cursor()

@profile(immediate=True)
def read_by_mysqlclient():
    c.execute("select * from FOO;")
    res = c.fetchall()



read_by_mysqlclient()

 

分析结果:耗时0.4s, 和mysql直接执行基本在同一量级

 

 

综上,mysqlclient的性能要好于pymsql

via。https://my.oschina.net/sukai/blog/1930092

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »
因本文不是用Markdown格式的编辑器书写的,转换的页面可能不符合MIP标准。