TL;DR
CardBench introduces a diverse, open-source benchmark featuring thousands of queries across 20 real-world databases to systematically evaluate learned cardinality estimation (CE) models. The paper demonstrates that while zero-shot generalization drops significantly when handling complex table joins, fine-tuning pre-trained models with minimal data offers a highly efficient alternative to training costly instance-specific models from scratch.
What Problem Does This Paper Address?
In relational databases, the query optimizer is responsible for finding the most efficient way to execute a SQL query. A critical input to this decision is Cardinality Estimation (CE)—predicting the number of intermediate rows a query or sub-query will return. Traditional database systems rely on simplistic heuristics (like assuming data is uniformly distributed and columns are independent), which frequently fail and lead to slow, suboptimal query plans.
Recently, machine learning models have been proposed to replace these traditional heuristics with highly accurate, data-driven predictions. However, most existing learned CE models are "instance-based," meaning they must be trained from scratch for every single database. Collecting the training data requires executing tens of thousands of queries to find their true row counts, costing massive amounts of time and compute.
To build generalizable, "zero-shot" models that can work out-of-the-box on unseen databases, researchers need a diverse training corpus. Unfortunately, existing CE benchmarks only contain one or two datasets, making it impossible to systematically develop or evaluate generalizable machine learning approaches for databases.
Methods
The authors build a standardized pipeline that extracts database-agnostic statistics, generates realistic SQL queries, and converts them into annotated, heterogeneous query graphs. To ensure models can generalize to entirely unseen databases, they exclude dataset-specific features (like table or column names) and instead use transferable features such as table sizes, null fractions, data types, and estimated selectivities.
They evaluate two deep learning architectures:
- Graph Neural Networks (GNNs): Captures query structure through bottom-up message passing from leaf nodes to the root.
- Graph Transformers: Adapts standard transformers to graphs using spatial encodings (shortest path distances) and directional causal masks to respect the directed acyclic graph (DAG) structure of query plans.
These models are evaluated across three paradigms:
- Instance-based: Trained and tested on the same database.
- Zero-shot: Pre-trained on 19 databases and tested on a completely unseen 20th database.
- Fine-tuned: Pre-trained on 19 databases and adapted to the 20th using a very small subset of local training data.
Datasets
The authors introduce CardBench, a benchmark containing thousands of queries over 20 distinct real-world databases spanning various domains (e.g., wikipedia, stackoverflow, movielens, airline). The benchmark provides two primary query configurations:
- Single Table Queries: 1 to 4 filter predicates on a single table.
- Binary Join Queries: Joins between two tables with 1 to 3 filter predicates per table.
Generating the true cardinality labels for these workloads required a massive 7 CPU-years of execution time on Google BigQuery, which the authors have open-sourced to lower the entry barrier for future ML-for-DB research.
Key Insights and Ideas
- The Join Wall for Zero-Shot: Zero-shot models perform reasonably well on simple single-table queries, but their accuracy plummets as soon as binary joins are introduced. Cross-table data distributions and multi-column correlations are incredibly difficult to generalize without seeing the specific dataset.
- Fine-Tuning is a Game Changer: Instead of training massive instance-specific models from scratch, pre-training a model on 19 datasets and fine-tuning it on the 20th with just 500 samples yields excellent accuracy. This dramatically reduces training overhead and makes learned CE practical for real-world deployment.
- GNNs Edge Out Transformers: Despite the large receptive field of attention mechanisms in Graph Transformers, GNNs consistently performed better. The iterative message-passing nature of GNNs naturally aligns with the hierarchical structure of relational query execution plans.
Key Results
The performance of the models was evaluated using the q-error, which measures the factor by which an estimate deviates from the true cardinality (a q-error of 1 is perfect).
- Single Table: Instance-based GNNs achieved an impressive average P50 q-error of 1.1 and a P95 error of 24.08.
- Binary Joins: Traditional database heuristics (like those in PostgreSQL) struggle immensely, yielding a median q-error of 55.54 and a P95 error of $4.4 \times 10^6$.
- Fine-Tuning Efficiency: While zero-shot models struggled on joins (experiencing up to a 5300x increase in P95 q-error), fine-tuning a pre-trained GNN with just 500 samples brought the P50 and P95 q-errors down to 1.32 and 120, respectively—outperforming instance-based models trained on the same amount of data.
Limitations and Future Work
The current iteration of CardBench is limited to single-table and binary join queries. Real-world database workloads often involve complex multi-way joins, aggregations, and subqueries. The authors note that more research is required to scale pre-trained models to these complex query shapes, and they hope the community will use their open-sourced query generation infrastructure to extend CardBench further.
Summary updated