Use raw SQLite executemany for bulk database writes#268
Merged
Conversation
Replace Peewee ORM insert_many() calls in _efficient_write_many_data with raw cursor.executemany(), bypassing per-row ORM overhead. The old approach batched in groups of 125 to stay under SQLite's 999-variable limit; executemany uses a single prepared statement executed repeatedly, so no such limit applies and the full dataset can be flushed in one call. New functions insert_many_activities() and insert_many_exchanges() in schema.py handle the raw insertion. _efficient_write_dataset() no longer returns lists or flushes mid-loop; _efficient_write_many_data() calls the new functions once after iterating all datasets. Co-authored-by: Raphael Jolivet <contact@raphael-jolivet.name>
SQLite's raw executemany cannot bind Python tuples to TEXT columns,
but locations like ("foo", "bar") are valid in brightway. Coerce
location to str() before binding, matching Peewee's TextField behavior.
cmutel
added a commit
that referenced
this pull request
May 13, 2026
cmutel
added a commit
that referenced
this pull request
May 14, 2026
cmutel
added a commit
that referenced
this pull request
May 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extracted from #255 by Raphael Jolivet — isolating just the
executemanybulk-insert optimization without the schema normalization, pickle→JSON, ordrop_metadatachanges from that PR.insert_many_activities()andinsert_many_exchanges()toschema.py, each usingcursor.executemany()on the raw SQLite connection to bypass Peewee ORM overhead per row_efficient_write_dataset()no longer flushes batches of 125 mid-loop (that limit existed because Peewee'sinsert_manybuilds one largeINSERT ... VALUES (?, ?...), (?, ?...)statement that hits SQLite's 999-variable limit;executemanyuses a single prepared statement executed repeatedly, so no such limit applies)_efficient_write_many_data()collects all datasets in one pass, then calls the two new functions onceCo-authored-by: Raphael Jolivet contact@raphael-jolivet.name
Test plan