Bird
0
0

You want to optimize a Flask app to reduce the number of queries when displaying users and their posts. Which approach best applies eager loading?

hard📝 component behavior Q8 of 15
Flask - Performance Optimization
You want to optimize a Flask app to reduce the number of queries when displaying users and their posts. Which approach best applies eager loading?
ADefine the posts relationship with lazy='select' and access posts later.
BSet lazy='dynamic' on posts and filter posts after loading users.
CDisable relationship loading and query posts separately.
DUse <code>joinedload(User.posts)</code> in the query to load posts with users.
Step-by-Step Solution
Solution:
  1. Step 1: Identify eager loading method

    Using joinedload() in the query loads related posts with users in one query.
  2. Step 2: Compare other options

    lazy='select' delays loading, lazy='dynamic' returns query, disabling loading causes extra queries.
  3. Final Answer:

    Use joinedload(User.posts) in the query to load posts with users. -> Option D
  4. Quick Check:

    joinedload() optimizes queries with eager loading [OK]
Quick Trick: Use joinedload() in query for eager loading [OK]
Common Mistakes:
MISTAKES
  • Relying on lazy='select' for optimization
  • Using lazy='dynamic' without eager loading
  • Querying posts separately causing N+1 queries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes