ORM-23: Add batched loading (load_for) to avoid N+1 queries #23
Labels
No labels
area:core
area:database
area:migrations
area:model
area:relations
area:schema
area:tests
breaking-change
size:M
size:S
tracking
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
NoctisWolfshadow/tessera-orm#23
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Depends on: #19, #20, #21
Context
Looping over users and calling
user.posts().all()runs one query per user. This answers the "how does this work with joins?" TODO inquery.rs: instead of hydrating joined rows into several models, run one batched follow-up query and group the results in a map. Related data is not stored on the structs.Tasks
HasMany::load_for(&[P]) -> Result<HashMap<i64, Vec<C>>>,HasOne::load_for -> HashMap<i64, C>,BelongsTo::load_for(&[C]) -> HashMap<i64, P>andBelongsToMany::load_for(&[A]) -> HashMap<i64, Vec<B>>.IN (..)query per chunk ofMAX_BOUND_IDS = 500ids (SQLite limits bound parameters).hydrate, then read the key withrow.get(B::COLUMNS.len()).#[doc(hidden)]or atest-utilfeature).Example
Acceptance criteria