简体   繁体   English

JPA 存储库 findAll() 限制

[英]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.嗨,我只是想知道使用 JPARepository.findAll() 从 MYSQL 数据库中检索行是否有任何限制,我的数据库中有大约 3700000 个条目。 If I use findAll() do I get all the entries or truncated count?如果我使用 findAll() 我会得到所有条目还是截断计数?

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?请参阅List 最多可以容纳多少数据?
  • The memory of your computer is limited.您电脑的 memory 是有限的。
  • The size of your JVM heap is limited. JVM 堆的大小是有限的。
  • 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.根据经验:永远不要计划在 memory 中拥有数百万个 JPA 实体。 Either process them in batches using pagination as Jimmy proposed.或者按照Jimmy的建议使用分页分批处理它们。 Or use some other technology that scales better, eg JdbcTemplate and one of it's query methods taking a RowCallbackHandler或者使用其他一些可以更好地扩展的技术,例如JdbcTemplate和其中一个采用RowCallbackHandlerquery方法

It is better to not use findAll if there are a lot of entities.如果有很多实体,最好不要使用 findAll。 You can use pagination something like, ( taking no of records in single page as SIZE ):您可以使用类似的分页(将单页中的记录数作为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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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