Bird
0
0

You have a Flask app with a User model and a related Post model. You want to display all users with their posts efficiently. Which approach best balances performance and resource use?

hard📝 Application Q15 of 15
Flask - Performance Optimization
You have a Flask app with a User model and a related Post model. You want to display all users with their posts efficiently. Which approach best balances performance and resource use?
AUse eager loading with <code>joinedload</code> to load users and posts in one query.
BUse lazy loading and access posts inside a loop, causing many queries.
CLoad posts separately after loading all users, doubling queries.
DAvoid loading posts to save resources, showing empty post lists.
Step-by-Step Solution
Solution:
  1. Step 1: Consider performance needs

    Loading users and posts in one query reduces database hits and improves speed.
  2. Step 2: Compare loading strategies

    Lazy loading inside a loop causes many queries (N+1 problem), and loading posts separately doubles queries.
  3. Step 3: Evaluate resource use

    Eager loading with joinedload balances resource use and query count efficiently.
  4. Final Answer:

    Use eager loading with joinedload to load users and posts in one query. -> Option A
  5. Quick Check:

    Eager loading with joinedload = efficient queries [OK]
Quick Trick: Eager loading with joinedload avoids many queries [OK]
Common Mistakes:
MISTAKES
  • Using lazy loading causing many queries
  • Loading posts separately doubling queries
  • Skipping posts causing empty data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes