-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.6.5, 3.0.0-rc8
-
Component/s: Querying
-
Query Optimization
-
Fully Compatible
With a valid index to cover the queries, the following in the shell:
db.coll.count({foo:{$in:[1,2]}});
Is consistently upwards of an order of magnitude slower than:
db.coll.count({foo:1}) + db.coll.count({foo:2})
In my test, where 700k documents match each predicate, the first variant takes ~1500ms to run, the second variant (without $in) takes ~120ms.
To reproduce requires no special documents, surrounding indexes don't matter, the index in use can be compound (include other fields), other documents present in the collection don't matter (overall cardinality is irrelevant, though the effect requires low cardinality for the items in test purely to measure it temporally).
The effect described above does not apply to a regular query, it applies only to the count command. Thus, "explain" does not reveal the reason why in 2.6.5, however, it is possible to observe a difference in the plans by running count a newfangled 3.0 explainable collection.
- duplicates
-
SERVER-27822 Count plans sometimes don't need a FETCH
- Closed
- is duplicated by
-
SERVER-26831 count() examines docs on covered query with $in
- Closed
-
SERVER-22574 Allow COUNT stage to sum COUNT_SCANS for queries with $in operator
- Closed
-
SERVER-44032 Mongodb Count is slow
- Closed
- is related to
-
SERVER-17148 Remove plans do not need a FETCH stage
- Blocked