Skip to content

Fix for count being very slow on large Parse Classes' collections #5264

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

Merged
merged 4 commits into from Dec 28, 2018
Merged

Fix for count being very slow on large Parse Classes' collections #5264

merged 4 commits into from Dec 28, 2018

Conversation

ghost
Copy link

@ghost ghost commented Dec 28, 2018

For newer version of parse-server (at least 3.1.2) fetching count for a Parse Class takes a very long time if the Class has lots (1M+) of rows.

This is due to the Parse-Server's internal MongoCollection class using mongoDb's db.collection.countDocuments() method, which causes all of the collection's documents to be scanned; thus making the operation extremely slow.

This should be fixed so that countDocuments is only used for counting if a query is also provided. Otherwise the much faster db.collection.estimatedDocumentCount() should be used instead.

In the PR I've updated MongoCollection.count(...) so that if no query is specified, then the counting is done by the estimatedDocumentCount() method.

This problem has previously been discussed in issue #5218, and similar performance issues have been discussed here for mongoose.

@codecov
Copy link

codecov bot commented Dec 28, 2018

Codecov Report

Merging #5264 into master will decrease coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5264      +/-   ##
==========================================
- Coverage   93.88%   93.85%   -0.03%     
==========================================
  Files         123      123              
  Lines        8971     8973       +2     
==========================================
  Hits         8422     8422              
- Misses        549      551       +2
Impacted Files Coverage Δ
src/Adapters/Storage/Mongo/MongoCollection.js 97.43% <100%> (+0.13%) ⬆️
src/RestWrite.js 92.88% <0%> (-0.37%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 66f5943...db9f8e0. Read the comment docs.

@flovilmart
Copy link
Contributor

This is a reasonable solution :) thanks for the PR!

@flovilmart flovilmart merged commit de92ce5 into parse-community:master Dec 28, 2018
UnderratedDev pushed a commit to UnderratedDev/parse-server that referenced this pull request Mar 21, 2020
…rse-community#5264)

* * Added fix for MongoCollection's count function, so that it uses the much more effecient estimatedDocumentCount if no queries were specified

* * Added missing options when running estimatedDocumentCount for Mongo Collections

* * Fixed issue with checking for zero query for Mongo Collections count
NicksonYap added a commit to NicksonYap/parse-server that referenced this pull request May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant