Result Streaming
When fetching thousands of records, loading them all into memory withget_multi can be inefficient. Instead, use stream_multi to process records one by one as they are fetched from the database.
Batch Writing
For high-performance inserts, use thebatch_writer context manager. It automatically batches create operations and flushes them to the database.
Eager Loading
To prevent N+1 query problems, you can eagerly load related data using SQLAlchemy options.Usage
All read methods (get, get_multi, stream_multi) accept an options argument.
DataLoader vs Selectinload
- DataLoaders (built-in): Best for resolving relationships after the initial query, especially in GraphQL or highly nested REST endpoints. They batch separate queries.
- Eager Loading (
selectinload): Best when you know you need the related data immediately. It optimizes the initial query (or issues a single secondary query) to fetch everything at once.
options are provided, giving you full control over the fetching strategy.
