如何有效地使用MySQLDB SScursor? [英] How to efficiently use MySQLDB SScursor?

查看:659
本文介绍了如何有效地使用MySQLDB SScursor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须处理一个大的结果集(可能是数百万行,有时更多)。

他们不幸的是需要一次检索所有(在启动时)。

I have to deal with a large result set (could be hundreds thousands of rows, sometimes more).
They unfortunately need to be retrieved all at once (on start up).

我试图通过使用尽可能少的内存来实现。

通过查看SO,我发现使用 SSCursor 可能是我正在寻找的,但我还是不知道如何使用它们。

I'm trying to do that by using as less memory as possible.
By looking on SO I've found that using SSCursor might be what I'm looking for, but I still don't really know how to exactly use them.

从基本游标或者SScursor做一个 fetchall()相同的(根据内存使用)?

我可以从sscursor我的行一个一个(或几个几个)如果是,

什么是最好的方法这样做?

Is doing a fetchall() from a base cursor or a SScursor the same (in term of memory usage)?
Can I 'stream' from the sscursor my rows one by one (or a few by a few) and if yes,
what is the best way to do so?

推荐答案

我同意Otto Allmendinger的答案,但为了明确Denis Otkidach的意见,这里是如何迭代结果,而不使用Otto的fetch()函数:

I am in agreement with Otto Allmendinger's answer, but to make explicit Denis Otkidach's comment, here is how you can iterate over the results without using Otto's fetch() function:

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)

这篇关于如何有效地使用MySQLDB SScursor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆