The next task is: there are a million records in the database, you need to select all the records. They said that sampling just a million records at once is bad for memory.
Need implementation spring-data-jpa pagination without the parameters used. That is, in my Service , already filtered data will be transferred to the controller, but in order to filter it is necessary to iterate through all the records, you need to do a search for 10-20 thousand records.
This is what my Service looks like now. In pageable , the page size is 5, just for example.
@Transactional public List<Contact> getAllContactByFilter(String filter) { List<Contact> result = new ArrayList<>(); Page<Contact> contactList = contactRepository.findAll(createPageRequest()); logger.info("contList size: " + contactList.getSize()); Pattern p = Pattern.compile(filter); Matcher matcher = null; logger.info("get all contacts by filter"); for (Iterator<Contact> iterator = contactList.iterator(); iterator.hasNext();) { Contact currentContact = iterator.next(); matcher = p.matcher(currentContact.getName()); if (matcher.matches()) { iterator.remove(); } } return result; } private Pageable createPageRequest() { PageRequest request = new PageRequest(0, 5); return request; }