0
0
Spring Bootframework~8 mins

Join fetch for optimization in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: Join fetch for optimization
HIGH IMPACT
This affects the database query performance and the time it takes to load related data in the application, impacting page load speed and responsiveness.
Loading an entity with its related entities efficiently
Spring Boot
List<Order> orders = entityManager.createQuery("SELECT o FROM Order o JOIN FETCH o.items", Order.class).getResultList();
This fetches orders and their items in a single query, reducing database calls.
📈 Performance GainSingle query reduces database round-trips, improving load speed and reducing LCP
Loading an entity with its related entities efficiently
Spring Boot
List<Order> orders = entityManager.createQuery("SELECT o FROM Order o", Order.class).getResultList();
for (Order order : orders) {
  order.getItems().size(); // triggers separate query per order
}
This triggers N+1 queries: one for orders and one for each order's items, causing many database round-trips.
📉 Performance CostTriggers N+1 queries causing multiple database round-trips and slower page load
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Without join fetch (N+1 queries)N+1 database queriesMultiple server response delaysSlower initial paint due to delayed data[X] Bad
With join fetch (single query)Single database queryFaster server responseFaster initial paint with data ready[OK] Good
Rendering Pipeline
Join fetch optimizes data retrieval by reducing database queries, which speeds up server response and reduces time before content is sent to the browser.
Data Fetching
Server Response
Rendering
⚠️ BottleneckDatabase query count and latency
Core Web Vital Affected
LCP
This affects the database query performance and the time it takes to load related data in the application, impacting page load speed and responsiveness.
Optimization Tips
1Use join fetch to load related entities in one database query.
2Avoid triggering multiple queries for related data (N+1 problem).
3Fewer database queries lead to faster server response and better LCP.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using join fetch in Spring Boot?
AIt caches all data on the client side to avoid server calls.
BIt reduces the number of database queries by fetching related entities in one query.
CIt delays data loading until user interaction.
DIt compresses the data sent over the network.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page, and observe the number of API/database calls made to fetch data.
What to look for: Fewer and faster API calls indicate good join fetch usage; many repeated calls indicate N+1 problem.