Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo count operation called twice in QuerydslMongoPredicateExecutor.findAll(Predicate, Pageable) [DATAMONGO-1860] #2761

Closed
spring-projects-issues opened this issue Jan 26, 2018 · 1 comment

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Jan 26, 2018

Erik T opened DATAMONGO-1860 and commented

https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QuerydslMongoPredicateExecutor.java#L172

return PageableExecutionUtils.getPage(applyPagination(query, pageable).fetchResults().getResults(), pageable,
() -> createQueryFor(predicate).fetchCount());

Count is a very expensive operation in MongoDB https://jira.mongodb.org/browse/SERVER-1752

In snippet above, fetchResults() internally calls count to populate total count of QueryResults.
Again, fetchCount also calls count.

final QueryResults<T> queryResults = applyPagination(query, pageable).fetchResults();
return PageableExecutionUtils.getPage(queryResults.getResults(), pageable,
() -> queryResults.getTotal());

would end up calling count once.

Moreover, giving the option of returning Slice<T> as well as Page<T>, and do away with count altogether would also be a nice addition.


Affects: 2.0.3 (Kay SR3)

Reference URL: https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QuerydslMongoPredicateExecutor.java#L172

Referenced from: pull request #529

Backported to: 2.0.4 (Kay SR4), 1.10.11 (Ingalls SR11)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jan 28, 2018

Mark Paluch commented

Hey Erik T, thanks for raising this issue. Reusing the count from QueryResults makes sense. Going forward with this topic we could use fetch() instead and retrieve the count only if necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants