简体   繁体   中英

JPA repository findAll() limit

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?

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, eg JdbcTemplate and one of it's query methods taking a RowCallbackHandler

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM