Question 40 / 50
Good to KnowMediumConceptPerformance
Is a COLLSCAN always a problem?
Think about it first.
Short answer
No. A collection scan can be the right plan when the collection is tiny, or when the query must read most documents anyway. It is a problem when a selective query on a large collection has no usable index.
Why?
The planner compares costs. IXSCAN plus a huge FETCH can lose to COLLSCAN on a small collection. Judge explain() against the data size and selectivity, not against a rule that 'COLLSCAN means you failed'. Be suspicious of COLLSCAN on the 50 million order collection for a single userId.
Interview tip
This question exists to catch people who memorised 'COLLSCAN bad, IXSCAN good' without nuance.
Common mistake
Adding an index to a 200-document collection 'to eliminate COLLSCAN' and calling that a win.
How did you do?
Cheat Sheet