chunk iterator support for results
See original GitHub issueIt would be helpful to be able to ‘automatically’ iterate through fetchmany, similar to pd.read_sql(chunksize) or as suggested here: http://code.activestate.com/recipes/137270-use-generators-for-fetching-large-db-record-sets/
So for example instead of writing the following in this example:
with engine.connect() as conn:
result = conn.execution_options(stream_results=True).execute(
Customer.__table__.select().limit(n)
)
while True:
chunk = result.fetchmany(10000)
if not chunk:
break
for row in chunk:
row["id"], row["name"], row["description"]
you could write something like:
with engine.connect() as conn:
result = conn.execution_options(stream_results=True, arraysize=10000).execute(
Customer.__table__.select().limit(n)
)
for chunk in result:
for row in chunk:
row["id"], row["name"], row["description"]
I’m using arraysize to be in accordance with pep249 dbapi cursor.arraysize but perhaps chunksize might be better
Issue Analytics
- State:
- Created 4 years ago
- Comments:18 (16 by maintainers)
Top Results From Across the Web
Iterate an iterator by chunks (of n) in Python? - Stack Overflow
Show activity on this post. This works because [iter(iterable)]*n is a list containing the same iterator n times; zipping over that takes one ......
Read more >Iterator magic - Splitting an array into chunks
Iterators are a great alternative to the Javascript Array toolbox. Here's an Apps Script iterator example to split a large array into chunks....
Read more >itertools — Functions creating iterators for efficient looping ...
Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional func argument). If func is...
Read more >[Help] Splitting an Iterator Into Chunks : r/rust - Reddit
I have been trying to create a function that would accept a single iterator, and return a processed version of it.
Read more >itertools::structs::Chunks - Rust - Docs.rs
An iterator that yields the Chunk iterators. Iterator element type is Chunk . See .chunks() for more information. Trait Implementations ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
the activestate recipe is wrong. with the vast majority of DBAPIs, it doesn’t matter if you call fetchall() or fetchmany(), the rows are already fetched into memory fully after execute() is called. so I think this feature is kind of misleading since fetchmany() is already misleading. The option that should be considered here is stream_results: https://docs.sqlalchemy.org/en/13/core/connections.html?highlight=stream_results#sqlalchemy.engine.Connection.execution_options.params.stream_results only works with three DBAPis right now - without it, there are no memory savings to fetchmany().
Very clear 😃