Warning: Hints lock you into a plan; revisit when stats change.
3. Implementing Batch Fetching
Mechanism
Detail
Hibernate @BatchSize
Group lazy loads into IN clause
JDBC setFetchSize
Server-side cursor
Spring Data EntityGraph
Eager fetch specific paths
4. Implementing Query Result Caching
Layer
Tool
App
Caffeine, @Cacheable
ORM
Hibernate 2nd-level + query cache
Distributed
Redis
DB
Materialized views
5. Implementing N+1 Query Prevention
Example: JOIN FETCH
// BAD: orders.forEach(o -> o.getLines()) // each triggers a query// GOOD:@Query("select o from Order o join fetch o.lines where o.status = :s")List<Order> findOpenWithLines(@Param("s") String status);
6. Implementing Projection Queries
Example: DTO projection
@Query("select new com.acme.OrderSummary(o.id, o.totalCents) from Order o where o.status = :s")List<OrderSummary> summarize(@Param("s") String status);