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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Adapters/Storage/Mongo/MongoCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ export default class MongoCollection {
}

count(query, { skip, limit, sort, maxTimeMS, readPreference } = {}) {
// If query is empty, then use estimatedDocumentCount instead.
// This is due to countDocuments performing a scan,
// which greatly increases execution time when being run on large collections.
// See https://github.com/Automattic/mongoose/issues/6713 for more info regarding this problem.
if (typeof query !== 'object' || !Object.keys(query).length) {
return this._mongoCollection.estimatedDocumentCount({
maxTimeMS,
});
}

const countOperation = this._mongoCollection.countDocuments(query, {
skip,
limit,
Expand Down