[Paper Review] Flow-Loss: Learning Cardinality Estimates That Matter
This paper introduces Flow-Loss, a novel differentiable loss function that trains cardinality estimators to prioritize estimates that most impact query optimizer decisions, not just overall accuracy. By modeling query optimization as a flow routing problem on a plan graph and backpropagating plan cost gradients, Flow-Loss improves actual query performance—even when estimation accuracy is lower than Q-Error—especially on unseen query templates, achieving up to 1.5× faster runtimes.
Previous approaches to learned cardinality estimation have focused on improving average estimation error, but not all estimates matter equally. Since learned models inevitably make mistakes, the goal should be to improve the estimates that make the biggest difference to an optimizer. We introduce a new loss function, Flow-Loss, that explicitly optimizes for better query plans by approximating the optimizer's cost model and dynamic programming search algorithm with analytical functions. At the heart of Flow-Loss is a reduction of query optimization to a flow routing problem on a certain plan graph in which paths correspond to different query plans. To evaluate our approach, we introduce the Cardinality Estimation Benchmark, which contains the ground truth cardinalities for sub-plans of over 16K queries from 21 templates with up to 15 joins. We show that across different architectures and databases, a model trained with Flow-Loss improves the cost of plans (using the PostgreSQL cost model) and query runtimes despite having worse estimation accuracy than a model trained with Q-Error. When the test set queries closely match the training queries, both models improve performance significantly over PostgreSQL and are close to the optimal performance (using true cardinalities). However, the Q-Error trained model degrades significantly when evaluated on queries that are slightly different (e.g., similar but not identical query templates), while the Flow-Loss trained model generalizes better to such situations. For example, the Flow-Loss model achieves up to 1.5x better runtimes on unseen templates compared to the Q-Error model, despite leveraging the same model architecture and training data.
Motivation & Objective
- To address the limitation of existing learned cardinality estimators that optimize for average estimation error (e.g., Q-Error), which does not align with actual query performance.
- To identify which cardinality estimates most influence query optimizer decisions and prioritize improving those to enhance plan quality.
- To develop a differentiable, smooth approximation of the optimizer’s cost model and dynamic programming search to enable gradient-based training.
- To evaluate whether Flow-Loss leads to better generalization on unseen query templates compared to standard Q-Error training.
- To demonstrate that Flow-Loss-trained models generalize better and avoid overfitting to irrelevant or noisy estimates, even with reduced featurization.
Proposed method
- Formalizes query optimization as a shortest path problem on a plan graph, where paths represent alternative query plans.
- Approximates the dynamic programming optimizer and cost model using analytical, differentiable functions to compute gradients of plan cost w.r.t. cardinality estimates.
- Derives Flow-Loss as a differentiable surrogate loss that backpropagates plan cost gradients to refine cardinality estimates that matter most.
- Employs matrix inversion (B(Y)⁻¹) to compute gradients efficiently, leveraging structure from electric flow formulations for scalability.
- Trains neural network models (FCNN, MSCN) on real-world workloads using Flow-Loss and compares against Q-Error and PostgreSQL defaults.
- Validates results using a new benchmark (CEB) with ground-truth cardinalities for 16K+ queries across 21 templates with up to 15 joins.
Experimental results
Research questions
- RQ1Can a learned cardinality estimator trained to minimize plan cost (via Flow-Loss) outperform one trained for estimation accuracy (Q-Error) in real query performance?
- RQ2Does Flow-Loss improve generalization to query templates not seen during training, especially when model capacity is constrained?
- RQ3How does Flow-Loss compare to Q-Error in robustness to featurization changes and data distribution shifts?
- RQ4Can Flow-Loss-trained models maintain performance improvements even when training data is noisy or partially inaccurate?
- RQ5To what extent does Flow-Loss reduce overfitting to cardinality estimates that do not affect plan choice?
Key findings
- Flow-Loss-trained models improve query plan cost (PPC) and runtime despite having worse average Q-Error than Q-Error-trained models.
- On unseen query templates, Flow-Loss models achieve up to 1.5× better average query runtimes than Q-Error models, even with identical model architectures and training data.
- Flow-Loss models generalize better: when featurization components are removed, Flow-Loss models maintain performance, while Q-Error models degrade by up to 2× in PPC.
- PostgreSQL heuristic features (e.g., cardinality and cost estimates) are critical for generalization; their removal harms both models, but Flow-Loss remains more robust.
- Flow-Loss models show superior robustness to distributional shift: they avoid significant performance regressions on slightly different workloads, unlike Q-Error models which can be up to 1.5× slower than PostgreSQL.
- The training overhead for Flow-Loss is 3–5× higher than Q-Error due to matrix inversion in gradient computation, but inference remains fast (few ms), making it practical for query optimization.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.