0
0
Spring Bootframework~20 mins

N+1 query problem in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
N+1 Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output count of queries executed?
Given a Spring Boot JPA repository fetching a list of 5 authors, each with 3 books, and the code uses a simple findAll() on authors without fetch join, how many SQL queries will be executed when accessing each author's books in a loop?
Spring Boot
List<Author> authors = authorRepository.findAll();
for (Author author : authors) {
    System.out.println(author.getBooks().size());
}
A1 query
B5 queries
C6 queries
D15 queries
Attempts:
2 left
💡 Hint
Think about how JPA loads collections lazily by default and when queries are triggered.
📝 Syntax
intermediate
2:00remaining
Which JPQL query avoids the N+1 problem by fetching books eagerly?
Select the JPQL query that fetches authors and their books in a single query to avoid the N+1 problem.
ASELECT a FROM Author a JOIN FETCH a.books
BSELECT a FROM Author a JOIN a.books
CSELECT a FROM Author a LEFT JOIN a.books
DSELECT a FROM Author a WHERE a.books IS NOT NULL
Attempts:
2 left
💡 Hint
Look for the keyword that forces eager loading of the collection.
🔧 Debug
advanced
2:00remaining
Why does this code cause an N+1 problem despite using @EntityGraph?
Consider this repository method: @EntityGraph(attributePaths = {"books"}) List findAll(); Why might this still cause multiple queries when accessing books?
AThe @EntityGraph only works with native queries, not JPQL
BThe @EntityGraph is not applied because the method is overridden elsewhere
CThe @EntityGraph is ignored because the fetch type of books is EAGER
DThe @EntityGraph is applied but the transaction is closed before accessing books
Attempts:
2 left
💡 Hint
Think about when lazy loading triggers queries and transaction scope.
🧠 Conceptual
advanced
2:00remaining
What is the main cause of the N+1 query problem in ORM frameworks?
Choose the best explanation for why the N+1 query problem occurs in ORM frameworks like JPA.
ABecause lazy loading triggers a separate query for each related entity when accessed
BBecause ORM frameworks always load all related entities eagerly by default
CBecause ORM frameworks do not support joins in queries
DBecause the database does not support complex queries
Attempts:
2 left
💡 Hint
Consider how lazy loading works when accessing related data.
state_output
expert
2:00remaining
What is the number of SQL queries executed with this code using batch fetching?
Given this Spring Boot JPA configuration with batch fetching enabled for books, and fetching 10 authors each with 5 books, how many SQL queries are executed when accessing all books of all authors? Assume batch size is 5. Code: List authors = authorRepository.findAll(); for (Author author : authors) { author.getBooks().size(); }
A1 query
B3 queries
C11 queries
D50 queries
Attempts:
2 left
💡 Hint
Batch fetching loads collections in groups instead of one by one.