<!-- llms-explorer concept facts · https://llms-explorer.com/tree/cesource-cardinality-estimation-sampling-vs-heuristics/ · pack 2026-10-22 · ~1171 tokens -->

# ceSource cardinality estimation (sampling vs heuristics)

> Source-anchored research for ceSource cardinality estimation (sampling vs heuristics). Run status: COMPLETED.

Parent: [MongoDB Advanced Explain: Aggregation, Sharded Plans, Loose Index Scans and the 8.3 Cost-Based Ranker](https://llms-explorer.com/tree/mongodb-advanced-explain-aggregation-sharded-plans-loose-index-scans-and-the-8-3-cost-based-ranker/) · 4 facets · 10 facts · page: https://llms-explorer.com/tree/cesource-cardinality-estimation-sampling-vs-heuristics/

## How it works

- In MongoDB's v8.3 branch, CBR selects heuristic CE for trivially estimable queries when no relevant multikey index exists. The code comments say heuristics suffice for that case and explain that relevant multikey key counts require sampling. (high confidence) — [source](https://github.com/mongodb/mongo/blob/v8.3/src/mongo/db/query/plan_ranking/cbr_plan_ranking.cpp)
- The MongoDB 8.3.3 manual says heuristic CE is used only when sampling fails, but the v8.3 implementation selects `kHeuristicCE` directly for trivially estimable queries with no relevant multikey index. A `heuristics` label alone therefore does not prove that sampling was attempted and failed. (high confidence) — [source](https://www.mongodb.com/docs/v8.3/reference/explain-results/)

## Problems, failure modes and limitations

- `ceSource` describes estimator provenance, not a confidence or accuracy score. General optimizer research notes that cardinality-estimation errors can propagate into cost errors and poor plan choices, while Microsoft Research analyzes random-uniform sampling through error bounds and trade-offs among accuracy, storage, build time, and query latency. (medium confidence) — [source](https://www.microsoft.com/en-us/research/publication/cardinality-estimation-is-machine-learning-a-silver-bullet/)

## Comparisons and alternatives

- A 2024 study of MongoDB 7.0.1 evaluated its first-past-the-post planner, which raced partially executed candidate plans instead of estimating their costs. That study does not measure the accuracy of MongoDB 8.3.3's `ceSource` methods. (medium confidence) — [source](https://15799.courses.cs.cmu.edu/spring2025/papers/23-mongodb/tao-adc2024.pdf)
- Sampling is not an accuracy guarantee: a MongoDB Jira issue last updated 2026-10-13 reports a CBR example where a default 375-document sample with few matches under index bounds yielded 300,000 estimated seeks versus 45 actual seeks. Microsoft Research also analyzes upper error bounds for random-uniform sampling in cardinality estimation. (medium confidence) (verified as of 2026-10-13) — [source](https://jira.mongodb.org/browse/SERVER-136693)

## Facts and statements

- `explain.queryPlanner.winningPlan.estimatesMetadata.ceSource` identifies the cardinality estimation method for a plan stage. MongoDB documents the field as new in 8.3.3 and shows the same metadata shape on nested stages. (high confidence) — [source](https://www.mongodb.com/docs/v8.3/reference/explain-results/)
- MongoDB defines `sampling` as estimating over a sample of the base collection, `heuristics` as applying selectivity rules, `mixed` as combining estimate types, `metadata` as using database metadata such as collection cardinality, and `code` as calculating an estimate directly in C++ from a constant or expression. (high confidence) — [source](https://www.mongodb.com/docs/v8.3/reference/explain-results/)
- Starting in MongoDB 8.3.3, `$queryStats` reports counts for histogram, sampling, heuristic, mixed, metadata, and code CE methods under `metrics.costBasedRanker.cardinalityEstimationMethods`. MongoDB marks `$queryStats` unsupported and warns that its output format may change. (high confidence) (verified as of 2026-10-22) — [source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats/)
- A cardinality estimate predicts the number of documents or keys a stage produces and feeds plan cost estimation. MongoDB exposes `cardinalityEstimate` and `costEstimate`; Microsoft's query-optimizer documentation describes the same dependency for its cost-based optimizer. (high confidence) — [source](https://www.mongodb.com/docs/v8.3/reference/explain-results/)
- MongoDB 8.3.3 explain output exposes `queryPlanner.winningPlan.estimatesMetadata.ceSource` as per-stage cardinality-estimation metadata. The manual defines `sampling` as estimating over a sample of the base collection, `heuristics` as rule-based selectivity estimation, `mixed` as combining estimate types, `metadata` as using database metadata, and `code` as a direct C++ estimate. (high confidence) — [source](https://www.mongodb.com/docs/v8.3/reference/explain-results/)
