The Problem
Fetching a list of posts and then fetching the author for each post in a loop causes the “N+1 Problem”:The Solution
FluxCRUD integrates the DataLoader pattern to batch these requests automatically.Usage
Userepo.get_many_by_ids():
Solving the N+1 select problem
posts = await post_repo.get_multi(session) # 1 Query
for post in posts:
# N Queries!
author = await user_repo.get(session, post.user_id)
repo.get_many_by_ids():
user_ids = [p.user_id for p in posts]
# 1 Query (SELECT ... WHERE id IN (...))
authors = await user_repo.get_many_by_ids(user_ids)