Lazy Loading
ELI5 — The Vibe Check
Lazy loading waits until you actually access related data before fetching it. Access post.author and only then does it query the database. Convenient, but if you do it in a loop it creates the N+1 problem automatically. Great for single records, terrible for lists.
Real Talk
Lazy loading defers the loading of related objects until they are explicitly accessed. It is the default behavior in many ORMs. While it reduces initial query complexity, accessing lazily-loaded relations inside a loop generates N+1 queries. It is appropriate when related data is rarely needed and the dataset is small.
When You'll Hear This
"Lazy loading is convenient but caused N+1 queries in the list view." / "Switch from lazy to eager loading when rendering lists with related data."
Related Terms
Eager Loading
Eager loading fetches all the related data you need upfront in one or two queries.
N+1 Query
N+1 is when your code runs 1 query to get a list of things, then runs 1 more query for EACH thing on the list.
Query Optimization
Query optimization is the art of making slow database queries fast. Add an index here, rewrite that subquery as a JOIN, fetch only the columns you need.