Reputation: 4050
I'm new to Jpa. I have a List list which has about 10000-50000 client objects in it.
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:
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?
Upvotes: 0
Views: 2308
Reputation: 30088
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. Then match up the results with clients in Java code after you get the results back. This avoids the overhead of doing potentially thousands of database queries. You might want to "order by" the client id and purchase date.
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. A table scan would probably kill performance.
Upvotes: 3