Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the N+1 query problem in Spring Boot?
It is a performance issue where one query fetches a list of N items, and then an additional query is run for each of the N items to fetch related data, causing N+1 database calls.
Click to reveal answer
beginner
Why does the N+1 query problem happen with JPA/Hibernate in Spring Boot?
Because lazy loading fetches related entities one by one when accessed, triggering a separate query for each item instead of fetching all related data in one go.
Click to reveal answer
intermediate
How can you fix the N+1 query problem in Spring Boot using JPA?
Use eager fetching or JPQL fetch joins to load related entities in a single query, reducing the number of database calls.
Click to reveal answer
intermediate
What is a fetch join in JPQL and how does it help with the N+1 problem?
A fetch join tells JPA to fetch related entities together with the main entity in one query, avoiding multiple queries for each related item.
Click to reveal answer
beginner
What is lazy loading and how does it relate to the N+1 query problem?
Lazy loading delays loading related data until it is accessed, which can cause many queries if accessed repeatedly, leading to the N+1 problem.
Click to reveal answer
What causes the N+1 query problem in Spring Boot with JPA?
AUsing native SQL queries
BEager loading fetching all data at once
CLazy loading fetching related entities one by one
DCaching data in memory
✗ Incorrect
Lazy loading triggers a separate query for each related entity, causing the N+1 problem.
Which JPQL feature helps solve the N+1 query problem?
AOrder by
BGroup by
CSubquery
DFetch join
✗ Incorrect
Fetch join loads related entities in one query, avoiding multiple queries.
What does the '+1' represent in the N+1 query problem?
AOne query for the whole dataset
BThe initial query to fetch the list of N items
COne query for caching
DOne query for logging
✗ Incorrect
The '+1' represents the initial query to fetch the list of N items, while 'N' represents the additional queries (one per item) for related data.
Which loading strategy can cause the N+1 problem?
ALazy loading
BEager loading
CStatic loading
DBatch loading
✗ Incorrect
Lazy loading delays fetching related data, causing many queries.
How can you check for N+1 queries in your Spring Boot app?
AUse logging or database query monitoring tools
BIgnore performance warnings
CDisable database access
DUse only native queries
✗ Incorrect
Logging SQL queries or using monitoring tools helps detect N+1 problems.
Explain the N+1 query problem and why it affects performance in Spring Boot applications.
Think about how fetching related data one by one causes many database calls.
You got /3 concepts.
Describe two ways to fix the N+1 query problem using JPA in Spring Boot.
Consider how to load related data in fewer queries.
You got /3 concepts.
Practice
(1/5)
1. What is the N+1 query problem in Spring Boot applications?
easy
A. Not using any database queries at all
B. Making only one query to fetch all data including related entities
C. Using incorrect SQL syntax in queries
D. Making one query to fetch a list, then one query per item to fetch related data
Solution
Step 1: Understand the query pattern
The N+1 problem occurs when the app first fetches a list (1 query), then fetches related data for each item separately (N queries).
Step 2: Identify the problem impact
This causes many queries, slowing down the app and wasting resources.
Final Answer:
Making one query to fetch a list, then one query per item to fetch related data -> Option D
Quick Check:
N+1 query problem = multiple queries instead of one [OK]
Hint: N+1 means 1 query + N queries for related data [OK]
Common Mistakes:
Thinking N+1 means only one query is made
Confusing it with syntax errors
Assuming it is about missing queries
2. Which of the following is the correct way to use JOIN FETCH in a Spring Data JPA query to avoid the N+1 problem?
easy
A. @Query("SELECT o FROM Order o JOIN FETCH o.items")
B. @Query("SELECT o FROM Order o JOIN o.items")
C. @Query("SELECT o FROM Order o LEFT JOIN o.items")
D. @Query("SELECT o FROM Order o WHERE o.items IS NOT NULL")
Solution
Step 1: Understand JOIN FETCH usage
JOIN FETCH tells JPA to fetch related entities eagerly in one query, avoiding multiple queries.
Step 2: Identify correct syntax
@Query("SELECT o FROM Order o JOIN FETCH o.items") uses JOIN FETCH correctly to fetch orders with their items in one query.
Final Answer:
@Query("SELECT o FROM Order o JOIN FETCH o.items") -> Option A
Quick Check:
JOIN FETCH = eager fetch to avoid N+1 [OK]
Hint: Use JOIN FETCH to load related data in one query [OK]
Common Mistakes:
Using JOIN without FETCH causes lazy loading
Using WHERE instead of JOIN FETCH
Missing FETCH keyword
3. Given this Spring Data JPA repository method:
@Query("SELECT c FROM Customer c")
List<Customer> findAllCustomers();
And assuming Customer has a lazy-loaded orders collection, what happens when you call findAllCustomers() and then access orders for each customer?
medium
A. One query to get customers, then one query per customer to get orders (N+1 problem)
B. One query to get customers and all orders in one go
C. No queries are made until orders are accessed
D. An error occurs because orders are not fetched
Solution
Step 1: Analyze the query and lazy loading
The query fetches customers only; orders are lazy-loaded, so not fetched initially.
Step 2: Accessing orders triggers queries
Accessing orders for each customer triggers one query per customer, causing N+1 queries total.
Final Answer:
One query to get customers, then one query per customer to get orders (N+1 problem) -> Option A
Quick Check:
Lazy loading causes N+1 queries [OK]
Hint: Lazy loading causes one query per item when accessed [OK]
Common Mistakes:
Assuming all data loads in one query
Thinking no queries run until orders accessed
Confusing lazy and eager loading
4. You have this code snippet causing N+1 queries:
List<Author> authors = authorRepository.findAll();
for (Author a : authors) {
System.out.println(a.getBooks().size());
}
How can you fix it to avoid the N+1 problem?
medium
A. Add @Transactional annotation to the method
B. Call getBooks() inside a separate thread
C. Change repository method to use @Query("SELECT a FROM Author a JOIN FETCH a.books")
D. Remove the loop and print authors only
Solution
Step 1: Identify cause of N+1
Calling getBooks() inside loop triggers one query per author due to lazy loading.
Step 2: Use JOIN FETCH to load books eagerly
Changing repository query to use JOIN FETCH loads authors and books in one query, avoiding N+1.
Final Answer:
Change repository method to use @Query("SELECT a FROM Author a JOIN FETCH a.books") -> Option C
Quick Check:
JOIN FETCH fixes N+1 by eager loading [OK]
Hint: Use JOIN FETCH in query to load related data eagerly [OK]
Common Mistakes:
Adding @Transactional does not fix N+1
Using threads does not solve query count
Removing loop hides problem but does not fix it
5. You have entities Post and Comment with a one-to-many lazy relationship. You want to fetch all posts with their comments efficiently. Which approach best avoids the N+1 problem and handles posts with no comments?
hard
A. Use native SQL without JOIN FETCH and map manually
B. @Query("SELECT p FROM Post p LEFT JOIN FETCH p.comments") to fetch posts and comments in one query
C. Fetch posts first, then fetch comments in a separate query for each post
D. Fetch posts only and ignore comments to reduce queries
Solution
Step 1: Understand lazy loading and N+1
Lazy loading comments causes one query per post when accessed, causing N+1 problem.
Step 2: Use LEFT JOIN FETCH to include posts without comments
LEFT JOIN FETCH fetches posts and their comments in one query, including posts with no comments.
Final Answer:
@Query("SELECT p FROM Post p LEFT JOIN FETCH p.comments") to fetch posts and comments in one query -> Option B
Quick Check:
LEFT JOIN FETCH avoids N+1 and includes empty collections [OK]
Hint: Use LEFT JOIN FETCH to include all posts and comments [OK]
Common Mistakes:
Using INNER JOIN FETCH excludes posts without comments