The N+1 Query Problem

Queries Executed
0

Component Inspector

Click on any component to understand how lazy loading triggers the N+1 problem.

Simulation Legend

The "1" Query (Fetch Parents)
The "N" Queries (Fetch Children lazily)
Optimized JOIN FETCH Query
Req
Client App
Requests List of Authors & Books
Application Layer
AuthorService
List<Author> authors = repo.findAll();
for(Author a : authors) {
  a.getBooks().size(); // Danger!
}
Hibernate (JPA ORM)
Translates method calls to SQL
Network Latency Boundary
PostgreSQL Database
Author Table
Book Table