0
0
Flaskframework~20 mins

Database query optimization in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Database Query Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding ORM Lazy Loading
In Flask with SQLAlchemy, what is the main effect of using lazy='dynamic' on a relationship?
AIt disables loading of related items completely.
BIt loads all related items immediately when the parent object is loaded.
CIt caches related items in memory permanently after first load.
DIt delays loading related items until explicitly queried, returning a query object instead of a list.
Attempts:
2 left
💡 Hint

Think about when related data is fetched from the database.

component_behavior
intermediate
2:00remaining
Effect of N+1 Query Problem
Given a Flask app using SQLAlchemy, what happens if you access a related attribute in a loop without eager loading?
Flask
users = User.query.all()
for user in users:
    print(user.posts)
AOnly one query runs for users and posts separately, then cached.
BOne query for users, then one query per user to load posts, causing many queries.
CNo queries run until posts are accessed outside the loop.
DOne single query joins users and posts, loading all data at once.
Attempts:
2 left
💡 Hint

Consider how many database queries happen inside the loop.

📝 Syntax
advanced
2:00remaining
Correct Use of joinedload for Eager Loading
Which option correctly applies eager loading with joinedload in Flask SQLAlchemy to avoid N+1 queries?
Flask
from sqlalchemy.orm import joinedload

users = User.query.options(_____).all()
Ajoinedload(User.posts)
Bjoinedload('User.posts')
Cjoinedload(posts)
Djoinedload('posts')
Attempts:
2 left
💡 Hint

Check how to reference the relationship attribute correctly.

🔧 Debug
advanced
2:00remaining
Diagnosing Slow Query in Flask App
A Flask app runs slowly when listing users and their posts. The code is: users = User.query.all() for user in users: print(len(user.posts)) What is the main cause of the slowness?
AThe code triggers one query for users plus one query per user to load posts (N+1 problem).
BThe query uses a join that returns duplicate users causing slow processing.
CThe posts attribute is not defined, causing repeated errors slowing the app.
DThe database connection is closed before queries run, causing retries.
Attempts:
2 left
💡 Hint

Think about how many queries happen inside the loop.

state_output
expert
3:00remaining
Output of Query with Subqueryload and Filtering
Consider this Flask SQLAlchemy code: posts = Post.query.options(subqueryload(Post.comments)).filter(Post.title.like('%Flask%')).all() What is true about the loaded comments for each post?
AComments are loaded lazily one by one when accessed, causing many queries.
BOnly comments containing 'Flask' are loaded due to the filter on posts.
CAll comments for each post are loaded in one additional query using subqueryload.
DNo comments are loaded because subqueryload does not work with filters.
Attempts:
2 left
💡 Hint

Remember what subqueryload does with related collections.