question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

chunk iterator support for results

See original GitHub issue

It 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:closed
  • Created 4 years ago
  • Comments:18 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
zzzeekcommented, Nov 2, 2019

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().

0reactions
sumaucommented, May 2, 2020

Very clear 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found