首页 > 解决方案 > MySQL SSDictCursor fetchone() 花费太多时间

问题描述

我正在处理大量数据,我正在优化我的处理循环,但仍然需要太多时间。所以我注意到我的数据集中每 1000-2000 行都使用 fetchone() 获取 1-5 秒!而所有其他人需要 10-30 纳秒(!)秒

db = MySQLdb.connect(host="localhost", user="****", passwd="****", db="****", charset='utf8',
                                 cursorclass=MySQLdb.cursors.SSDictCursor)
    c = db.cursor()
    c.execute(sql)

    while True:
     event = c.fetchone()
     if not event:
       return
     print(event)

我正在打印最长的 fetchone() 函数调用:

(row index : time in sec)
    1898 : 5.86757670 
    3783 : 6.63812080 
    5667 : 5.00934410 
    7559 : 5.21368210 
    9425 : 5.49990800 
    11312 : 4.47716010

但在较小的数据集上,峰值也较小,但它们发生的频率更高:

(row index : time in sec)
    561 : 0.00169070
    714 : 1.43662880 
    1416 : 2.07252320
    2106 : 1.77626870
    2811 : 1.78253030

PS我想这可能是范围优化的问题,因为我里面有一个很大的“id in ( ID's LIST )”表达式。但我对 NO_RANGE_OPTIMIZATION 提示有疑问。

标签: pythonmysqldatabasepython-3.xmysql-python

解决方案


推荐阅读