Documents and MongoDB · Engineering
Automatic indexes from query shapes
NonoDB can create indexes from frequent read shapes. Here is how the opt-in feature works, when it helps, and when it creates expensive clutter.
An index is only useful if it matches how the application actually queries the data. That is easy to say and hard to know in advance. Access patterns change, dashboards add filters, and a field that was cold last month becomes the hot path.
Automatic indexing is an opt-in layer that watches cache-missed read shapes and asks the writer to create an index when a shape becomes frequent.
It is not a replacement for schema design.
Off by default
The feature is disabled unless NONODB_AUTO_INDEX=1. The defaults are conservative:
| Setting | Default | Meaning |
|---|---|---|
NONODB_AUTO_INDEX_MIN_HITS |
50 | Cache misses in one window before a shape becomes a candidate |
NONODB_AUTO_INDEX_WINDOW |
300 | Frequency window in seconds |
NONODB_AUTO_INDEX_MIN_DOCS |
10,000 | Minimum collection size |
NONODB_AUTO_INDEX_MAX |
8 | Maximum automatic indexes |
NONODB_AUTO_INDEX_MAX_FIELDS |
3 | Maximum fields in an automatic compound index |
NONODB_AUTO_INDEX_TTL |
604,800 | Drop after seconds without demand |
NONODB_AUTO_INDEX_MIN_AGE |
3,600 | Minimum age before an automatic index can be dropped |
NONODB_AUTO_INDEX_INTERVAL |
5 | Maintenance tick interval |
The threshold matters. An index build blocks writers while its backfill transaction runs. Creating one for a query that ran 50 times during a traffic spike can cost more than the scan it was meant to replace.
What a “shape” is
The maintenance worker counts cache-missed read shapes by:
- collection;
- set of indexable fields;
- equality versus range classification.
Concrete values are deliberately excluded. status = "active" and status = "paused" are the same shape; status plus created_at is a different one.
A shape becomes a candidate after enough misses inside the rolling window. The worker then checks collection size, existing indexes, the automatic-index limit, and the write queue before asking the writer to create the index.
What it does not do
Automatic indexes are ordinary secondary indexes once created. They do not:
- rewrite a query;
- create a partial index from a predicate;
- add a text, fuzzy, or vector index;
- replace a missing compound-prefix design;
- guarantee that the planner will choose them.
The planner still applies the conservative rules: equality on every compound prefix field, optional range on the last, no null special case. An automatic index that does not match the planner’s conditions is just extra write cost.
When it helps
Automatic indexing is useful when:
- the workload is exploratory or changing;
- the collection is large enough that a scan is noticeable;
- the same field combination keeps appearing in slow reads;
- the team does not have a dedicated database tuner.
It is less useful when:
- the collection is small enough that scans are cheap;
- writes are the bottleneck;
- the hot queries need partial or text indexes;
- the application has a known, stable index plan.
In the last case, create the indexes explicitly. Predictability is worth more than automation.
Observability
Automatic index decisions should be visible. Check the daemon metrics and logs for index builds, drops, and the current automatic-index count. A sudden build during peak traffic is a signal that the threshold or window is too aggressive.
The automatic indexes are also visible in the normal index listing, so they can be inspected, explained, and dropped like any other index. Dropping one manually is a valid way to say “not worth the write cost”.
A practical policy
For a small deployment:
- Leave automatic indexing off until you have real traffic.
- Enable it with the defaults when read latency becomes a problem.
- Watch the first few builds; they are the expensive ones.
- Review automatic indexes after a week. Keep the useful ones, drop the noise.
- Replace important automatic indexes with explicit definitions so a restart or a policy change does not remove them.
The operations guide documents the settings and the build lifecycle. The short version: automatic indexes are a helpful safety net, not a substitute for knowing your query shapes.