5

我有以下运行非常缓慢的代码(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 在...时很慢,我试过了:

  • 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. 你能解释一下为什么会有缓存效果吗?

谢谢。

规格:Python 3.5 | 8核i7 | 16go 公羊

4

0 回答 0