CardOOD: Robust Query-driven Cardinality Estimation under Out-of-Distribution

Rui Li, Kangfei Zhao, Jeffrey Xu Yu, Guoren Wang · DOI

TL;DR

This paper introduces CardOOD, a framework for building robust cardinality estimators for database query optimization that can handle "out-of-distribution" (OOD) query workloads. By adapting transfer and robust learning techniques, CardOOD significantly improves estimation accuracy and query execution speed, even when test queries differ from training data.

What Problem Does This Paper Address?

Database query optimizers rely on accurate cardinality estimates (the predicted number of rows a query will return) to choose the most efficient execution plan. Traditional methods often make simplifying assumptions that lead to inaccurate estimates, especially for complex queries. More recent "query-driven" machine learning models offer better accuracy and flexibility. However, these models struggle when the distribution of queries they encounter in practice (the "test workload") differs from the data they were trained on (the "training workload"). This "out-of-distribution" (OOD) problem can lead to significant performance degradation. CardOOD aims to solve this by developing robust training methods that make these learned estimators resilient to such workload shifts.

Methods

CardOOD is a framework that integrates several robust learning strategies to train query-driven cardinality estimators. These strategies are categorized into three main groups:

  • Representation Learning: These methods aim to learn query representations (embeddings) that are invariant to distribution shifts. Techniques like Deep CORAL (Correlation Alignment) and DANN (Domain-Adversarial Neural Networks) are adapted to align feature distributions across different query groups.
  • Data Manipulation: This category focuses on augmenting the training data to improve generalization. The paper extends "Mixup," a technique from computer vision, to create new training samples by interpolating existing queries and their cardinalities (Query Mixup). Another method, Query Masking, trains the model to predict cardinalities even when parts of the query features are masked.
  • New Learning Strategies: This group includes novel approaches. The paper proposes "OrderEmb," a self-supervised learning algorithm that leverages the inherent partial order of cardinalities among queries. If query A is a sub-condition of query B, then the cardinality of A should be less than or equal to the cardinality of B. OrderEmb trains the model to reflect this order in its query embeddings. Group Distributionally Robust Optimization (Group DRO) is also explored, which aims to optimize for the worst-case scenario across different query groups.

The CardOOD framework is modular, allowing for different query encoders, training pipelines, and machine learning models to be plugged in.

Datasets

The paper uses the following datasets for training and evaluation:

  • forest: A single-relation dataset for range queries.
  • IMDB: A multi-relation dataset with PK/FK and FK/FK joins.
  • DSB (Decision Support Benchmark): A benchmark designed to include intricate data distributions and challenging query templates.
  • JOB-light: A dataset containing hand-written join query templates.

Key Insights and Ideas

  • OOD is a critical challenge for learned cardinality estimators: Unlike image classification where features might shift but core objects remain, query features (predicates, joins) can change drastically, making generalization difficult.
  • Transfer learning techniques are applicable but need adaptation: Methods like Deep CORAL and DANN, successful in computer vision, can be adapted for cardinality estimation, but their effectiveness depends on how query distributions are grouped.
  • Self-supervised learning can exploit inherent data properties: The proposed OrderEmb method shows that by learning the partial order of query cardinalities, models can become more robust to OOD shifts without needing explicit group labels. This is a significant advantage when defining query groups is difficult.
  • Data manipulation can be a simple yet effective strategy: Query Mixup and Query Masking offer practical ways to improve robustness, with Query Masking being particularly efficient for training.
  • No single algorithm is universally best: The performance of different CardOOD algorithms varies depending on the dataset, query complexity, and the underlying ML model used.

Key Results

  • Improved Prediction Accuracy: CardOOD algorithms consistently outperform the baseline Empirical Risk Minimization (ERM) approach, especially at higher quantiles (95% and 99%), indicating better robustness against OOD shifts. For example, on the forest dataset, improvements of up to 36.6% were observed.
  • Practical Utility: When integrated into PostgreSQL, CardOOD significantly reduces query execution time. Speedups of up to 5.6% on IMDB, 36.6% on DSB, and 4.6% on JOB-light were achieved compared to the PostgreSQL built-in estimator.
  • Training Efficiency Trade-offs: While most robust learning algorithms are slower to train than ERM (e.g., Deep CORAL and OrderEmb are the slowest due to computational overhead), Query Masking is faster.
  • Data Efficiency: OrderEmb demonstrates good performance even with limited training data, suggesting its data efficiency.

Limitations and Future Work

  • Offline Training: CardOOD focuses on offline training algorithms that build one-off models. The authors note that online learning approaches, which adapt to dynamic workloads, are orthogonal and could further improve performance.
  • Covariant Shift: The framework primarily addresses covariant shift (changes in feature distributions) and not label shift (changes in true cardinalities due to database updates).
  • Hyperparameter Sensitivity: While some algorithms are robust to hyperparameter tuning, others like DANN and Group DRO can be more difficult to train.
  • Future Work: The authors suggest exploring more advanced transfer learning techniques, investigating different query encoding methods, and further optimizing the training efficiency of the proposed algorithms. The practical utility demonstrated in PostgreSQL also opens doors for deeper integration and optimization within real-world database systems.

Summary updated

BibTeX
@article{li2026cardood,
  author = {Rui Li and Kangfei Zhao and Jeffrey Xu Yu and Guoren Wang},
  title = {{CardOOD: Robust Query-driven Cardinality Estimation under Out-of-Distribution}},
  journal = {VLDB J.},
  year = {2026},
  doi = {10.1007/s00778-026-00979-3},
}
Figure