[英]Java - Spring - Hibernate extracting 1000 of objects from Database takes long time
I have a Postgres database with 17 tables create by Hibernate with the @Entity annotation and iam running CrudRepository with the findAll method for extracting objects from the database 我有一个Postgres数据库,其中包含由Hibernate使用@Entity批注创建的17个表,并且IAM使用findAll方法运行CrudRepository从数据库中提取对象
Everything works fine until I try to retrieve 1000 + objects from the database which takes 27 seconds. 一切正常,直到我尝试从数据库中检索1000多个对象为止,这需要27秒。 Retrieving 600 objects takes around 10 seconds.
检索600个对象大约需要10秒钟。
The time is increasing exponentially and I am wondering if there is a solution to this, I know that if I try to join tables it would speed up the process but joining tables on 17 tables is a lot of work. 时间正成倍增加,我想知道是否有解决方案,我知道如果我尝试联接表会加快过程,但是在17个表上联接表是很多工作。 Are there other extracting methods that are faster on retrieving objects besides the one that I am using?
除了我正在使用的方法以外,还有其他提取方法可以更快地检索对象吗?
This is how I retrieve objects: 这是我检索对象的方式:
public List<Document> getAllDatabaseDocuments() {
List<Document> listOfDocs = new ArrayList<>();
listOfDocs.addAll((List<Document>) sdr.findAll());
return listOfDocs;
}
All JPA providers have QueryHints
to do certain query optimization. 所有JPA提供程序都有
QueryHints
来进行某些查询优化。 It provides you with the ability to fetch all associations in a single query or using joins. 它使您能够在单个查询或使用联接中获取所有关联。 For example, In EclipseLink, you can use
QueryHints.BATCH
to fetch all one to many
or many to many
associations in a single query. 例如,在EclipseLink中,可以使用
QueryHints.BATCH
在单个查询中获取所有one to many
或many to many
关联。
Instead of using findAll()
, create your own query in your entity and provide the appropriate query hint specific to hibernate. 代替使用
findAll()
,在您的实体中创建您自己的查询并提供特定于休眠的适当查询提示。 join-fetch
in your case will take a lot of time because you have a lot of related entities. 在这种情况下,
join-fetch
将花费大量时间,因为您有很多相关实体。 Try to use batch-fetch
as it will reduce the number of queries and effectively speeds up the fetching. 尝试使用
batch-fetch
因为它会减少查询数量并有效地加快提取速度。
You can learn more about hibernate specific fetching strategies here 您可以在此处了解有关休眠特定提取策略的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.