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?


- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- SQL 临时表问题 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01