How To Quickly Reproduce The N+1 Performance Issue
Description: The N+1 is an issue of lazy fetching (but, eager is not exempt). This application reproduce the N+1 behavior.
Key points:
- define two entities,
Author
andBook
in a lazy bidirectional@OneToMany
association - fetch all
Book
lazy, so withoutAuthor
(results in 1 query) - loop the fetched
Book
collection and for each entry fetch the correspondingAuthor
(results N queries) - or, fetch all
Author
lazy, so withoutBook
(results in 1 query) - loop the fetched
Author
collection and for each entry fetch the correspondingBook
(results N queries)