如何高效使用 MySQLDB SScursor?

2021-11-20 00:00:00 python optimization mysql cursor

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

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)

相关文章