0
0
Flaskframework~10 mins

Lazy loading vs eager loading in Flask - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Lazy loading vs eager loading
Start: Query Object Created
Eager Loading?
YesLoad Related Data Immediately
Return Full Data
Lazy Loading
Access Related Data?
YesLoad Related Data Now
Return Partial Data
Return Partial Data
The flow shows how eager loading fetches all related data upfront, while lazy loading waits until related data is accessed.
Execution Sample
Flask
user = User.query.options(joinedload(User.posts)).first()
print(user.name)
print(user.posts[0].title)
This code uses eager loading to fetch a user and their posts together, then prints the user's name and the first post's title.
Execution Table
StepActionData LoadedOutput
1Create query with joinedload(User.posts)User and posts data prepared for loadingNo output yet
2Execute query with eager loadingUser and all posts loaded from DBNo output yet
3Access user.nameNo new data loadedPrint user name
4Access user.posts[0].titlePosts already loaded, no DB hitPrint first post title
5End of codeAll needed data loadedProgram ends
💡 All data loaded eagerly at query execution, no lazy loading triggered
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
userNoneUser object with posts loadedUser object with posts loadedUser object with posts loadedUser object with posts loaded
user.nameN/AN/AUser's name stringUser's name stringUser's name string
user.postsN/AList of posts loadedList of posts loadedList of posts loadedList of posts loaded
user.posts[0].titleN/AN/AN/ATitle of first postTitle of first post
Key Moments - 2 Insights
Why does accessing user.posts[0].title not trigger a new database query?
Because eager loading fetched all posts when the query ran (see execution_table step 2), so posts are already in memory.
What happens if we don't use joinedload and access user.posts?
Lazy loading triggers a new database query at access time to fetch posts (not shown here, but would happen at step 4 if no eager loading).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the related posts data loaded?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Check the 'Data Loaded' column in execution_table row for step 2
According to variable_tracker, what is the value of user.posts after step 3?
AList of posts loaded
BEmpty list
CNone
DNot yet loaded
💡 Hint
Look at user.posts value in variable_tracker after step 3
If we remove joinedload from the query, when will posts be loaded?
AAt query execution (step 2)
BWhen accessing user.posts (step 4)
CNever loaded
DAt program end (step 5)
💡 Hint
Refer to key_moments explanation about lazy loading triggering DB query on access
Concept Snapshot
Lazy loading delays fetching related data until accessed.
Eager loading fetches related data immediately with the main query.
In Flask-SQLAlchemy, use joinedload() for eager loading.
Lazy loading can cause extra queries later, eager loading reduces queries upfront.
Choose based on performance needs and data usage patterns.
Full Transcript
This visual execution shows how Flask-SQLAlchemy handles lazy and eager loading. When eager loading is used with joinedload(), related data like posts are fetched immediately with the main user query. This means accessing user.posts does not cause extra database queries. The execution table traces each step: query creation, execution with eager loading, accessing user name and posts, and program end. The variable tracker shows how variables like user and user.posts hold data after each step. Key moments clarify why eager loading avoids extra queries and when lazy loading triggers them. The quiz tests understanding of when data loads and variable states. Overall, eager loading fetches related data upfront, while lazy loading waits until access time, affecting performance and query count.