简体   繁体   English

优化Spring Boot JPA查询

[英]optimizing Spring Boot JPA query

I'm new to Jpa. 我是Jpa的新手。 I have a List list which has about 10000-50000 client objects in it. 我有一个列表列表,其中包含大约10000-50000个客户端对象。

I'm iterating through this list and querying each client if he has made any purchases like this: 我正在遍历此列表,并查询每个客户是否进行过以下购买:

List<TransactRepViewModel> temporalList = transactRepViewRepository
        .findByClientIdAndClDateBetween(clieTabModel.getClientId(),
                reportInterval.getStartDate(),
                reportInterval.getEndDate());

TransactRepViewRepository.class method looks like this: TransactRepViewRepository.class方法如下所示:

List<TransactRepViewModel> findByClientIdAndClDateBetween(
        String clientId, Date startDate, Date endDate) throws DataAccessException;

I would really like to improve my search time, since iterating through such amount of clients takes quite some time. 我真的很想改善搜索时间,因为遍历如此多的客户端需要花费一些时间。 Are there any technique I could use? 有什么我可以使用的技术吗?

It's difficult to make a concrete recommendation without knowing a lot more about what you're trying to do, but generally: 在不了解您要做什么的情况下很难提出具体建议,但是通常:

  • Do one large query, so, for example, create a repository method that finds all purchases between the dates that you are interested in, and make sure that the result includes the client id. 进行一个大型查询,例如,创建一个存储库方法,以查找您感兴趣的日期之间的所有购买,并确保结果包括客户ID。 Then match up the results with clients in Java code after you get the results back. 返回结果后,将结果与Java代码中的客户端匹配。 This avoids the overhead of doing potentially thousands of database queries. 这避免了执行潜在的数千个数据库查询的开销。 You might want to "order by" the client id and purchase date. 您可能想按客户ID和购买日期“订购”。

  • Make sure that you have proper indexes on the table(s) involved, and verify that when jpa executes it's query, the database uses the indexes. 确保在涉及的表上具有正确的索引,并验证jpa执行查询时数据库是否使用了索引。 A table scan would probably kill performance. 表扫描可能会降低性能。

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

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