1

I have a collection with tens of millions of documents.

When performing a count() I see that MongoDB parses this as an aggregation query with an empty $match and a $group with {_id: 1, $sum: 1}.

It takes an unignorable amount of time for the query to perform.

My question is why doesn't MongoDB optimize this query?

It's a matter of reading a metadata variable of the collection and returning it (that is, the total number of documents).

Thanks!

greenButMellow
  • 316
  • 1
  • 9
  • 1
    What is the definition of "*optimized*"? By default, `_id` field is indexed and the group-then-count operation should be "optimized" in a canonical way. On the other hand, have you checked out [collStats.count](https://www.mongodb.com/docs/manual/reference/command/collStats/#mongodb-data-collStats.count) to get the collection count? – ray Nov 06 '22 at 11:21
  • @ray, so why is my `count()` query so slow? – greenButMellow Nov 06 '22 at 11:50
  • You have to define "slow" first. Do you have some performance metrics to measure it? What is your expected time consumed and what is the current time consumed? – ray Nov 06 '22 at 11:55
  • @ray, I have 55 million documents (single MongoDB instance). Is it reasonable for this query to take more than one minute? – greenButMellow Nov 06 '22 at 12:03
  • There is no strict answer to that. It depends on your organization's and your application's requirements. – ray Nov 06 '22 at 12:10
  • @ray, I understand but I'm asking from a technical perspective only. Is it an acceptable amount of time to wait? I know it depends on the hardware as well but even w/o such information I wonder if it's reasonable. As a comparison, with RDBMS it might take a fraction of a second, wouldn't it? – greenButMellow Nov 06 '22 at 12:24
  • 2
    Kind of going back to @ray's first comment - you're asking for the database to count all of the documents. If you want the metadata instead, then you can request it with the other function - https://stackoverflow.com/a/54607751/20042973 – user20042973 Nov 06 '22 at 13:12
  • 1
    Do you need the exact number? If not, then better use [estimatedDocumentCount](https://www.mongodb.com/docs/rapid/reference/method/db.collection.estimatedDocumentCount/), otherwise [countDocuments](https://www.mongodb.com/docs/rapid/reference/method/db.collection.countDocuments/) – Wernfried Domscheit Nov 06 '22 at 16:30

0 Answers0