java / intermediate
Snippet
Memory-Efficient Database Query Paging Using Spring Data Window and ScrollPosition
Offset-based pagination forces databases to scan and discard rows, increasing memory and query times on deep pages. Spring Data's keyset scrolling uses ScrollPosition and Window to stream large data sets incrementally with stable memory usage.
snippet.java
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public interface AuditLogRepository extends Repository<AuditLog, Long> {Window<AuditLog> findByTenantId(String tenantId, ScrollPosition scrollPosition, Limit limit, Sort sort);}@Servicepublic class AuditLogArchiver {private final AuditLogRepository repository;public AuditLogArchiver(AuditLogRepository repository) {this.repository = repository;}public void exportInBatches(String tenantId) {ScrollPosition position = ScrollPosition.keyset();Window<AuditLog> currentWindow;do {currentWindow = repository.findByTenantId(tenantId, position, Limit.of(500), Sort.by("id").ascending());processBatch(currentWindow.getContent());position = currentWindow.positionAt(currentWindow.size() - 1);} while (!currentWindow.isEmpty() && currentWindow.hasNext());}}
spring
Breakdown
1
Window<AuditLog> findByTenantId(String tenantId, ScrollPosition scrollPosition, Limit limit, Sort sort);
Declares a repository method returning a Window slice anchored by the scroll position instead of an integer offset.
2
ScrollPosition position = ScrollPosition.keyset();
Initializes a keyset-based cursor that navigates results using primary key indices.
3
position = currentWindow.positionAt(currentWindow.size() - 1);
Extracts the cursor location from the last element of the current batch to prepare the query for the next iteration.