0
0
Flaskframework~20 mins

Lazy loading vs eager loading in Flask - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lazy vs Eager Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding lazy loading behavior in Flask-SQLAlchemy
In Flask-SQLAlchemy, what happens when you access a related object configured with lazy='select' for the first time?
AThe related object is loaded only when you access the attribute for the first time.
BThe related object is loaded immediately when the parent object is queried.
CThe related object is never loaded automatically and must be queried manually.
DThe related object is loaded in a separate thread asynchronously.
Attempts:
2 left
💡 Hint
Think about when the database query for the related object happens.
component_behavior
intermediate
2:00remaining
Eager loading effect on query count
Given a Flask-SQLAlchemy model with a relationship set to eager loading using joined, what is the effect on the number of database queries when fetching parent objects?
AEach parent object triggers a separate query for its related objects.
BNo related objects are fetched unless accessed later.
CAll parent objects and their related objects are fetched in a single query using a JOIN.
DRelated objects are fetched in multiple queries but batched together.
Attempts:
2 left
💡 Hint
Eager loading tries to reduce the number of queries by fetching related data upfront.
state_output
advanced
2:00remaining
Output of query count with lazy loading
Consider this Flask-SQLAlchemy code snippet where lazy='select' is used. What will be the total number of SQL queries executed when iterating over 3 parent objects and accessing their related child objects?
Flask
parents = Parent.query.all()
for p in parents:
    print(p.child.name)
A4 queries total: one for parents and one for each child's lazy load.
B3 queries total: one for parents and two for children.
C1 query total: one for parents and children together.
DNo queries executed because data is cached.
Attempts:
2 left
💡 Hint
Remember lazy loading triggers a query each time a related attribute is accessed if not already loaded.
🔧 Debug
advanced
2:00remaining
Fixing N+1 query problem with eager loading
You notice your Flask app runs many queries when accessing related objects, causing slow response. Which change fixes this N+1 query problem?
Flask
parents = Parent.query.all()
for p in parents:
    print(p.child.name)
AChange relationship to lazy='dynamic' to delay loading.
BUse joined eager loading: Parent.query.options(joinedload(Parent.child)).all()
CRemove the relationship and query children separately.
DAdd .limit(1) to the query to reduce data.
Attempts:
2 left
💡 Hint
Eager loading can reduce queries by fetching related data upfront.
📝 Syntax
expert
2:00remaining
Correct syntax for eager loading with subquery in Flask-SQLAlchemy
Which option correctly applies subquery eager loading to a relationship named items in a Flask-SQLAlchemy query?
Flask
results = Order.query.options(_____).all()
Ajoinedload('items')
Beagerload(Order.items)
Clazy='subquery'
Dsubqueryload(Order.items)
Attempts:
2 left
💡 Hint
Look for the correct function to pass inside options() for subquery loading.