How to efficiently use MySQLDB SScursor?(如何高效使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何高效使用 MySQLDB SScursor?


基础教程推荐
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01