Documents and MongoDB · Engineering

Query planning: when an index is used and when it isn’t

NonoDB’s planner is deliberately conservative. Here is how it chooses an access path, why null is special, and what explain tells you.

A database can have an index and still not use it. That is not necessarily a bug. NonoDB’s planner is intentionally conservative: it prefers a correct full scan over an index plan that might silently omit documents.

This post explains the rules, the fallback path, and how to read explain.

The basic access paths

A read chooses one of four paths:

  1. Exact _id — direct primary B+ tree lookup.
  2. Secondary index — range scan over index entries.
  3. _id range — direct primary-key range, useful for time-ordered custom ids.
  4. Full collection scan — iterate documents and apply the complete filter.

The planner picks the cheapest path it can prove correct. “Prove” is the important word: an index is an access path, not a substitute for query semantics.

When a secondary index is used

For a single-field index, the planner uses it when the filter has:

  • an equality condition;
  • a range condition;
  • a single-value $all;
  • a case-sensitive anchored regex (^literal).

For a compound index, every field in the index prefix needs an equality condition, with an optional range or anchored-regex prefix on the last field. A filter that skips the first field of a compound index will not use it.

The planner also refuses to use an index when the indexed value is null. A plain index stores a missing field as null, so using it for a null query could miss documents that are not in the index. The scan still applies the correct filter.

The residual matcher is always there

Even when an index supplies candidates, NonoDB evaluates the full filter against every candidate document before returning it.

That sounds wasteful, and for a perfectly covered query it is. It is also what keeps mixed filters correct: an index may narrow the candidates by tenant_id, while the residual matcher checks status, created_at, and a nested array condition.

Covering indexes can avoid fetching the document for projections, but they do not change the semantic rule.

Sorting and index order

If the requested sort exactly matches the full index order and its per-field directions, the database can stream the index without materialising a result set. A reverse scan works when every direction is inverted. Mixed-direction compound indexes encode direction into the key, so matching sorts can be index-backed.

If the sort does not match, the planner falls back to a scan plus a bounded top-K sort. Shallow full sorts (skip + limit <= 50,000) can use parallel workers; deeper skips stay single-threaded to bound memory.

Sparse and partial indexes

Sparse indexes omit documents where the key is absent. A single-field sparse index is useful when many documents may not have the field. A compound sparse index is subtler: it stores null for absent fields and omits a document only when every key is absent.

A partial index is only usable when the query explicitly repeats its predicate with equality, $eq, or $in. If the query has an unrelated range or does not mention the predicate, the planner falls back to a scan. A hint that selects a partial index without its predicate fails instead of returning incomplete results.

Hints

Find-shaped queries can include a hint. A hint is a promise that the named index is appropriate; an unusable hint fails rather than silently falling back. That behavior is deliberate: a hint that returns fewer rows than expected is worse than an error.

What explain shows

explain reports the selected access path, index and bounds, sort coverage, candidate count, and actual matched/returned counts. It executes the query, so use a small limit for broad filters.

Look for:

  • index_used: whether an index was selected;
  • the access path (index_scan, id_scan, collection_scan);
  • the index bounds;
  • whether the sort was covered;
  • candidates versus returned documents.

If candidates is much larger than returned, the index is doing coarse work and the residual matcher is filtering most of it. If both are near the collection size, the query is scanning.

Full scans are not a failure

A full scan is the correct plan for an ad-hoc filter with no usable index. The raw JSON prefilter can skip documents cheaply for simple string and boolean equalities, and parallel scans help shallow queries. A broad analytical query still costs what it costs.

The practical rules are:

  • index the fields used by frequent equality and range filters;
  • keep compound prefixes in query order;
  • use explain before adding an index;
  • do not expect a partial index to help unless the query repeats its predicate;
  • use find_cursor or aggregation cursors for large results instead of deep skip.

The architecture guide documents the planner and scan paths in more detail, and create_index covers index options.

All engineering notes →Read the docs