Home > Blockchain >  JPA repository findAll() limit
JPA repository findAll() limit

Time:09-07

Hi I just wanted to know is there any limit for retrieving row from MYSQL database using JPARepository.findAll() , I have around 3700000 entries in my database. If I use findAll() do I get all the entries or truncated count?

CodePudding user response:

You will receive all entities until something breaks.

Things that might break:

  • List implementations may have a size constraint. See How much data can a List can hold at the maximum?
  • The memory of your computer is limited.
  • The size of your JVM heap is limited.
  • The duration your user is willing to wait for a result is limited.

As a rule of thumb: Never plan to have millions of JPA entities in memory. Either process them in batches using pagination as Jimmy proposed. Or use some other technology that scales better, e.g. JdbcTemplate and one of it's query methods taking a RowCallbackHandler

CodePudding user response:

It is better to not use findAll if there are a lot of entities. You can use pagination something like, ( taking no of records in single page as SIZE):

Pageable pageable = PageRequest.of(0, SIZE);  // for example SIZE = 500
Page<Qmail> page = repository.findAll(pageable);

while (!page.isEmpty()) {
    pageable = pageable.next();

    //   TODO WITH ENTITIES    
    page.forEach( entity -> SOP(entity.getId()) );

    page = repository.findAll(pageable);
}
  • Related