Query planning and EXPLAIN
A FluxDB statement does not run as written. It is classified to an engine, rewritten into a cheaper shape, costed against statistics, split across the shards and cells that hold the data, and only then executed. This page is what happens in that gap, and how to inspect it with EXPLAIN.
The planner is not one component. It is a small set of parts, each with a narrow job, and the honest way to read this page is to notice which parts work from real measured statistics and which fall back to estimates when no statistics have been collected yet.
From statement to plan
A read or write passes through four stages before any rows move.
| Stage | What happens | Where it runs |
|---|---|---|
| Classify | The statement is routed to the engine whose workload class it matches: transactional, analytical, graph, vector, search or streaming | On the cell that received it |
| Rewrite | A rule-based optimizer rewrites the logical plan into an equivalent cheaper one | In the engine command path |
| Cost and route | A cost model estimates the plan, the join planner picks a strategy, and the statement is split into per-cell fragments | On the coordinating cell |
| Execute and gather | Fragments run in parallel on their shards, and results merge | Across the targeted shards and cells |
Classification is deliberately simple and pattern-based. A statement that carries graph traversal keywords routes to the graph engine, vector similarity keywords to the vector engine, window functions or grouped aggregates to the analytical engine, and plain row reads and writes to the transactional engine.
Engine classification and the first pass of table extraction work by matching patterns in the SQL text. This is fast and correct for ordinary statements. It does not unpick quoted identifiers, table names buried in string literals or comments, or common table expressions, so a statement built from those is classified and routed on what the text plainly shows. The executor still runs the full statement correctly. The limitation is in plan quality for exotic statement shapes, never in plan correctness.
The cost model and statistics
The cost model answers one question: how long will this plan take. It answers it two ways, and it tells you which one it used.
- From history. FluxDB keeps a short record of recent executions keyed by a hash of the statement, and averages the last several runs. A plan that has run before is estimated from what it actually cost, with a confidence that rises as more runs accumulate.
- From heuristics. A statement with no history is estimated from its engine and its shape. An analytical aggregate is costed higher than an analytical scan, which is costed higher than a transactional point read. Confidence on a heuristic estimate is low by construction, and it is marked that way.
The model also records how close its last estimate was to the measured time, so estimation accuracy is itself observable rather than assumed.
Real cardinality when it exists
Row counts are the input the plan is most sensitive to. FluxDB resolves them through a single authority with a defined fallback chain:
- Real analyzed cardinality. When a transactional table has been analyzed, the planner reads that engine's collected row count directly. This is a live, in-process read of the cell's own engine, so it needs no network and holds under sovereign and air-gapped deployment.
- A configured estimate, when one has been supplied for the table.
- A per-table default of one thousand rows, when nothing better exists.
The effect is that a cell with no statistics collected plans exactly as it would with no statistics source at all, and a cell that has analyzed its tables plans on true cardinality. The estimate path is never removed. Better statistics change plan quality, never plan correctness.
On the analytical and lakehouse side, a separate per-file statistics path drives data skipping: for a scan with predicates, the planner reads each data file's recorded minimum and maximum values and produces a pruning plan, the list of files that must still be read because they cannot be skipped. That estimate reports the total files, the files that survive pruning, and the proportional row and byte cost of reading only the survivors.
Rewriting the plan
Before costing, a rule-based optimizer rewrites the logical plan into an equivalent one that is cheaper to run. The rewrites are the standard, safe set, applied in order.
| Rewrite | What it does |
|---|---|
| Predicate pushdown | Moves a filter below a join so each side is filtered before it is joined, not after |
| Projection pushdown | Moves column selection below a join so only the needed columns cross the join |
| Constant folding | Removes an always-true filter, and replaces an always-false filter with an empty result rather than scanning for rows that cannot exist |
| Join strategy selection | Chooses the join algorithm from input shape: a merge join when both inputs are already sorted, a nested-loop join when one input is small, a hash join otherwise |
| Limit pushdown | Pushes a limit into each branch of a union so no branch produces more rows than the result can use |
Each of these is a faithful rewrite: it produces the same rows as the statement as written, for less work. That property is what lets the optimizer apply them without consulting anything beyond the plan and its statistics.
Join ordering and strategy
A join across shards is where distribution either helps or quietly spends the gain on moving rows, so the join planner costs the strategy rather than assuming one.
| Strategy | When it is chosen | Relative cost |
|---|---|---|
| Local | Both sides live on the same shard | Lowest |
| Colocated | Both sides are partitioned on the join key, so matching rows already sit together | Low |
| Broadcast | One side is small enough to replicate to every shard holding the other | Moderate |
| Shuffle | Neither of the above, so both sides are repartitioned on the join key | Highest |
The planner weighs a broadcast against a shuffle using each side's estimated size, derived from its row count and average row width. Driving the decision off the measured sizes, rather than a flat cost for any join token, is what lets EXPLAIN report a distributed join honestly as colocated, broadcast or shuffle rather than just "join".
Pushdown into engines and across shards
Pushdown happens at two levels, and they compound.
Into the engine. The rewrites above push filters and projections down the plan so the executing engine reads the least data. The analytical engine then applies data skipping on top, so a filtered scan touches only the files whose recorded value ranges overlap the predicate.
Across shards. The router pushes predicates and projections down into each shard before a scatter-gather, so a shard returns the smallest useful result rather than raw rows for the coordinator to filter. A cross-cell statement is split into one query fragment per target cell plus a collect fragment that gathers them, and the fragments carry the pushed-down predicates with them.
Security travels the same path. Row filters and column masks are compiled into the plan of the engine that executes, so in-engine enforcement means a caller reaching a shard gets exactly the rows and columns their grants allow, with no separate filtering step that could be skipped.
The adaptive optimizer and index advisor
Two components close the loop between what a workload does and how it is planned.
The adaptive optimizer caches a plan per statement and improves it from feedback. It classifies the workload, estimates cost, and caches the result. On each execution it records the measured cost against the estimate. When a statement classified as transactional repeatedly runs long, or one classified as analytical repeatedly runs short, the optimizer reclassifies it and invalidates the stale cached plan, so the learned decision reaches the router on the next run rather than waiting out the cache. A cached plan also ages out if it is not used, so the cache tracks the live workload rather than its history.
Its objective is narrow on purpose. It looks for a cheaper plan that returns the identical result, not a faster plan that returns a different or lower-quality one. That narrowness is what makes its decisions safe to apply automatically.
The index advisor recommends indexes from observed query patterns. It reads the cell's live column-usage tracker, the same accumulator the read path feeds, so there is one source of truth and no parallel counting. A column that appears often in filter, join or order clauses and is not already indexed is flagged as a candidate, ranked by how often it is used. The recommendations feed an auto index manager that can create the index and record it in a catalog registry, and that registry is readable back through the node admin surface, so an automatically created index is visible rather than hidden.
EXPLAIN and plan inspection
EXPLAIN is available through the query API and the query workbench. It returns the plan the planner would run, without executing the statement, so you can see the engine, the shards, the estimated cost and the warnings before you commit to a query.
An EXPLAIN result carries:
| Field | What it tells you |
|---|---|
| Engine | Which engine the statement is classified to |
| Steps | The ordered operations: scan or index lookup per table, filter, join (labeled with the chosen strategy), aggregate, sort |
| Shards targeted | How many shards the statement reaches |
| Estimated rows and cost | The planner's estimates, from real cardinality where a table has been analyzed and from defaults otherwise |
| Index used | The index the planner expects to use, if any |
| Warnings | Plain advice: a full table scan on a large table, a result with no limit, a statement it could not classify |
| Data skipping stats | For a scan with file statistics, how many files the predicate prunes |
The costs, row counts and index choices in an EXPLAIN result are the planner's estimates. They are grounded in real analyzed cardinality when a transactional table has been analyzed, and in per-table defaults otherwise, so a plan for a freshly loaded table that has not been analyzed will read in round numbers. Analyze the tables a plan depends on to make EXPLAIN report against their true sizes. The engine still executes the full statement correctly regardless of what the estimate said.
A companion scan-cost estimate reports data-skipping pruning for a table and a set of predicates directly: how many files survive pruning and the proportional cost of reading them. It is read-only and plans without executing, and it applies the same read checks as the data itself, so a caller who may not read a table cannot probe its contents through a pruning estimate.
Cross-shard and cross-cell planning
A single-shard statement is planned and run in one place. A statement that spans shards or cells is split into fragments: one query fragment per target cell, carrying the pushed-down predicates and projections, and a collect fragment that gathers the per-cell results. The target cells are resolved from the tables the statement names against the routing map, so a statement that touches tables on one cell stays local and only a genuine fan-out pays for coordination.
Cross-cell result fragments are signature-verified before they merge, so a gathered result is trusted because it was verified, not because it arrived. The sizing consequence is the one the scale-out plane describes: a single-shard query pays no coordination, and a fan-out pays only for the rows that survive pushdown.
Related
- Scale-out and elasticity, the distribution plane the planner routes across.
- Storage and performance, the two storage planes a plan reads from.
- Execution cores and access methods, the engines a statement is classified to.
- Change data capture, how committed writes become a change feed.