Performance: Lazy loading vs eager loading
MEDIUM IMPACT
This concept affects how quickly data is fetched and rendered on the page, impacting initial load time and responsiveness.
from sqlalchemy.orm import joinedload posts = Post.query.options(joinedload(Post.author)).all() for post in posts: print(post.author.name)
posts = Post.query.all() for post in posts: print(post.author.name)
| Pattern | Database Queries | Initial Load Time | Network Overhead | Verdict |
|---|---|---|---|---|
| Lazy Loading | Multiple (N+1 queries) | Lower if few accessed | Lower initially, higher if accessed later | [!] OK |
| Eager Loading | Single batched query | Higher upfront | Higher upfront, lower later | [OK] Good |