CodexBloom - Programming Q&A Platform

Spring Boot REST API: How to optimize response time for a large dataset with pagination?

πŸ‘€ Views: 20 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-18
spring-boot rest pagination postgresql performance java

I'm working on a Spring Boot REST API that returns a sizable dataset (over 10,000 records) in response to a GET request. The API endpoint is designed to handle pagination, but I'm observing important lag when fetching pages, particularly on the first call. The endpoint is defined like this: ```java @GetMapping("/api/users") public ResponseEntity<Page<User>> getUsers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { Page<User> users = userService.findAll(PageRequest.of(page, size)); return ResponseEntity.ok(users); } ``` I've implemented pagination using `Spring Data JPA`'s `Pageable` interface. However, the initial request (the first page) takes around 5 seconds to complete, while subsequent requests for the next pages are relatively fast. The database is a PostgreSQL instance, and I've ensured that indexes are correctly applied on the columns being queried. I’ve tried enabling query logging and discovered that the first request executes a complex query that isn't optimized properly. Here’s the query extracted from the logs: ```sql SELECT * FROM users ORDER BY id LIMIT 10 OFFSET 0; ``` I suspect the question might be related to the retrieval of a large dataset before it applies pagination. Additionally, I've tried using `@Query` annotations with native SQL to see if that improves performance, but the scenario continues. What strategies or best practices can I implement to improve the performance of the first page load? Are there specific configurations or optimizations in JPA or PostgreSQL that might help? My development environment is macOS. Could someone point me to the right documentation?