更快的Python MySQL [英] Faster Python MySQL
问题描述
我使用mysql.connector(MySQLdb Python实现?)访问MySQL.从游标从select语句进行的传输不是那么快.
I use mysql.connector (MySQLdb Python implementation?) to access MySQL. The transfer from a select statement from a cursor isn't that fast.
有没有办法加快代码的速度?
Is there a way to speed up the code?
也许是另一个图书馆?哪个? (我有Windows和Python 3.1) 也许行检索与遍历游标不同?
Maybe another library? Which? (I have Windows and Python 3.1) Maybe a row retrieval different from iterating over the cursor?
推荐答案
默认的MySQLdb
游标可一次从服务器获取整个查询结果.将此数据转换为Python元组列表会消耗大量内存和时间.
The default MySQLdb
cursor fetches the entire query result at once from the server. Conversion of this data to a Python list of tuples can consume a lot of memory and time.
当您要进行大量查询并使用 MySQLdb.cursors.SSCursor 时,请使用 MySQLdb.cursors.SSCursor .
一次从服务器提取结果.但是请注意,使用SSCursor时,其他查询都不能在connection
上完成,直到获取整个结果集为止.
Use MySQLdb.cursors.SSCursor when you want to make a huge query and
pull results from the server one at a time. Note, however, that when using SSCursor, no other query can be made on the connection
until the entire result set has been fetched.
import MySQLdb
import MySQLdb.cursors as cursors
connection = MySQLdb.connect(
...
cursorclass = cursors.SSCursor)
cursor = connection.cursor()
cursor.execute(query)
for row in cursor:
...
或者,使用 oursql (MySQL的另一种Python驱动程序). oursql的功能之一是它延迟获取行.
Or, use oursql, an alternative Python driver for MySQL. One of the features of oursql is that it fetchs rows lazily.
这篇关于更快的Python MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!