迭代MySQL连接器游标时Python很慢

时间:2017-03-15 17:03:50

标签: python mysql cursor mysql-connector-python fetchall

我有以下代码运行速度非常慢(6.5秒用于迭代超过57,390行):

import mysql.connector

cnx=mysql.connector.connect(host=***, ***)
cursorSQL = cnx.cursor()

for dt in date_vect:
        cursorSQL.execute(query2, ((dt[0] + '-00:00:00'), (dt[0] + '-23:59:59'))) 
          #Takes about 0.25sec per iteration
        usr_list = list(cursorSQL.fetchall())  
          #takes about 6.20sec per iteration

根据此处的建议:Stackoverflow : Python is slow when...,我试过了:

  • usr_list= cursorSQL.fetchall()
  • usr_list= list(cursorSQL.fetchall())

正如@postoronnim建议的那样,我也尝试过:

  • usr_list= cursorSQL.fetchmany(57390)

没有成功。

但是,有一些缓存效果,因为在已经运行的迭代中第二次运行时,相同的迭代仅需0.5秒,然后减慢到6.5秒。

  1. 任何想法可能来自哪里?
  2. 您能否确认它与我的数据库无关,因为MySQL的所有导入都是在cursor.execute行完成的,而fetchall()缓慢只是由于列表处理?
  3. 你能解释为什么会有缓存效果吗?
  4. 感谢。

    规格:Python 3.5 | 8核i7 | 16go Ram

0 个答案:

没有答案
相关问题