-
Type: Bug
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Query Execution
-
None
-
Query Optimization
-
ALL
-
QO 2022-09-19, QO 2023-08-07, QO 2023-08-21
As of DRIVERS-501 the driver CRUD specification defines a pipeline to be used to count documents without resorting to the count command (due to potential inaccurate counts without query predicates).
db.foo.drop(); db.foo.insertMany([{},{},{},{},{}]); // Driver CRUD spec definition for countDocuments //////////////////// filter = {} skip = null limit = null pipeline = [{'$match': filter}] if (skip) { pipeline.push({'$skip': skip}) } if (limit) { pipeline.push({'$limit': limit}) } pipeline.push({'$group': {'_id': 1, 'n': {'$sum': 1}}}) ////////////////////////////////////////////////////////////////////// db.foo.explain("executionStats").aggregate(pipeline)
Without specifying a filter the empty $match stage would result in a COLLSCAN plan winning, though a COUNT_SCAN should be appropriate.
/* "nReturned": 5, "executionTimeMillis": 0, "totalKeysExamined": 0, "totalDocsExamined": 5, "executionStages": { "stage": "COLLSCAN", */
This could be worked around by unshifting a {$sort: { _id: 1 }} stage to the pipeline, however after discussing with david.storch@mongodb.com this may be addressable server-side.
- causes
-
RUBY-3064 countDocuments() executes aggregation with $group: { _id: 1 }
- Closed
- depends on
-
SERVER-23406 index scan is slower than full collection scan in some scenarios
- Backlog
- is related to
-
SERVER-57518 16% performance loss switching from Count to CountDocuments
- Backlog
-
SERVER-29444 Use a covered and streaming plan for $group, $sum:1 queries
- Backlog