0
0
Flaskframework~5 mins

Lazy loading vs eager loading in Flask

Choose your learning style9 modes available
Introduction

Lazy loading and eager loading help control when data is fetched from the database. This makes your app faster and uses less memory.

When you want to load related data only if it is needed to save time and memory.
When you know you will need all related data upfront and want to get it in one go.
When working with large datasets and want to avoid loading everything at once.
When optimizing database queries to reduce the number of calls.
When building web pages that show related information together.
Syntax
Flask
from sqlalchemy.orm import joinedload

class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    children = db.relationship('Child', lazy='select')  # lazy loading

# For eager loading:
parent = Parent.query.options(joinedload(Parent.children)).first()

lazy='select' means children are loaded only when accessed.

joinedload() fetches related data in the same query (eager loading).

Examples
This is lazy loading. Children load only when you ask for them.
Flask
children = db.relationship('Child', lazy='select')
This is eager loading. Children load immediately with the parent.
Flask
children = db.relationship('Child', lazy='joined')
Use this to eagerly load children when querying the parent.
Flask
parent = Parent.query.options(joinedload(Parent.children)).first()
Sample Program

This example shows lazy loading where children load only when accessed, and eager loading where children load with the parent query.

Flask
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import joinedload

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
db = SQLAlchemy(app)

class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    children = db.relationship('Child', lazy='select')  # lazy loading

class Child(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'))

with app.app_context():
    db.create_all()
    p = Parent(name='Parent1')
    c1 = Child(name='Child1', parent=p)
    c2 = Child(name='Child2', parent=p)
    db.session.add_all([p, c1, c2])
    db.session.commit()

    # Lazy loading: children are not loaded until accessed
    parent = Parent.query.first()
    print('Parent:', parent.name)
    print('Children:', [child.name for child in parent.children])

    # Eager loading: load children with parent in one query
    parent_eager = Parent.query.options(joinedload(Parent.children)).first()
    print('Parent eager:', parent_eager.name)
    print('Children eager:', [child.name for child in parent_eager.children])
OutputSuccess
Important Notes

Lazy loading saves memory but can cause extra database queries later.

Eager loading uses more memory upfront but reduces the number of queries.

Choose based on your app's needs and data size.

Summary

Lazy loading waits to load related data until you ask for it.

Eager loading loads related data immediately with the main data.

Use lazy loading to save resources and eager loading to reduce queries.