37

我必须处理一个大的结果集(可能是数十万行,有时更多)。
不幸的是,它们需要一次全部检索(在启动时)。

我试图通过使用尽可能少的内存来做到这一点。
通过查看 SO,我发现 usingSSCursor可能是我正在寻找的东西,但我仍然不知道如何准确地使用它们。

从基本游标或 SScursor执行fetchall()是否相同(就内存使用而言)?

我可以从 sscursor 中逐个(或逐个)“流式传输”我的行吗?如果可以,最有效的方法是什么?

4

3 回答 3

36

我同意 Otto Allmendinger 的回答,但要明确 Denis Otkidach 的评论,以下是如何在不使用 Otto 的 fetch() 函数的情况下迭代结果:

import MySQLdb.cursors
connection=MySQLdb.connect(
    host="thehost",user="theuser",
    passwd="thepassword",db="thedb",
    cursorclass = MySQLdb.cursors.SSCursor)
cursor=connection.cursor()
cursor.execute(query)
for row in cursor:
    print(row)
于 2009-11-27T12:13:11.393 回答
16

在获取大结果集时绝对使用 SSCursor。当我遇到类似的问题时,这对我产生了巨大的影响。你可以像这样使用它:

import MySQLdb
import MySQLdb.cursors

connection = MySQLdb.connect(
        host=host, port=port, user=username, passwd=password, db=database, 
        cursorclass=MySQLdb.cursors.SSCursor) # put the cursorclass here
cursor = connection.cursor()

现在您可以cursor.execute()使用游标执行查询并将其用作迭代器。

编辑:删除不必要的本土迭代器,谢谢丹尼斯!

于 2009-11-27T11:52:28.070 回答
2

或者,您可以SSCursor在连接对象之外使用(当您已经定义连接并且不希望所有连接都用作游标类时,这一点非常重要SSCursor)。

import MySQLdb
from MySQLdb.cursors import SSCursor # or you can use SSDictCursor

connection = MySQLdb.connect(
        host=host, port=port, user=username, passwd=password, db=database)
cursor = SSCursor(connection)
cursor.execute(query)
for row in cursor:
    print(row)   
于 2017-08-09T12:58:57.910 回答