python执行命令并返回结果集_使用大型数据集调用cursor.fetchall()时,python脚本会挂起...

我有一个返回超过125K行的查询.

目标是编写迭代遍历行的脚本,并为每个行填充第二个表,其中包含从查询结果处理的数据.

为了开发脚本,我用一小部分数据创建了一个重复的数据库(4126行)

在小型数据库上,以下代码有效:

import os

import sys

import random

import mysql.connector

cnx = mysql.connector.connect(user='dbuser', password='thePassword',

host='127.0.0.1',

database='db')

cnx_out = mysql.connector.connect(user='dbuser', password='thePassword',

host='127.0.0.1',

database='db')

ins_curs = cnx_out.cursor()

curs = cnx.cursor(dictionary=True)

#curs = cnx.cursor(dictionary=True,buffered=True) #fail

with open('sql\\getRawData.sql') as fh:

sql = fh.read()

curs.execute(sql, params=None, multi=False)

result = curs.fetchall() #<=== script stops at this point

print len(result) #<=== this line never executes

print curs.column_names

curs.close()

cnx.close()

cnx_out.close()

sys.exit()

行大小curs.execute(sql,params = None,multi = False)在大型和小型数据库上都成功.

如果我在循环中使用curs.fetchone(),我可以读取所有记录.

如果我改变了这条线:

curs = cnx.cursor(dictionary=True)

阅读:

curs = cnx.cursor(dictionary=True,buffered=True)

该脚本挂起在curs.execute(sql,params = None,multi = False).

我找不到有关fetchall()的任何限制的文档,也没有找到任何方法来增加缓冲区大小,也无法分辨我甚至需要多大的缓冲区.

没有例外.

我该如何解决这个问题?

你可能感兴趣的:(python执行命令并返回结果集_使用大型数据集调用cursor.fetchall()时,python脚本会挂起...)