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?
基础教程推荐
- 是否可以执行按位分组功能? 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
