Skip to content

Files

Latest commit

 

History

History

HibernateSpringBootSimulateNPlus1

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

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 and Book in a lazy bidirectional @OneToMany association
  • fetch all Book lazy, so without Author (results in 1 query)
  • loop the fetched Book collection and for each entry fetch the corresponding Author (results N queries)
  • or, fetch all Author lazy, so without Book (results in 1 query)
  • loop the fetched Author collection and for each entry fetch the corresponding Book (results N queries)

Output example:


If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices"If you need a hand of tips and illustrations of 100+ Java persistence performance issues then "Java Persistence Performance Illustrated Guide" is for you.