TL;DR
CardBench is an open-source benchmark featuring hundreds of thousands of queries across 20 diverse real-world databases, designed to push learned cardinality estimation (CE) beyond single-database models. By evaluating Graph Neural Networks (GNNs) and Graph Transformers across zero-shot, fine-tuned, and instance-specific setups, the authors demonstrate that while pure zero-shot estimation degrades rapidly on join queries, fine-tuning pre-trained models on modest data delivers near instance-level accuracy while drastically cutting training time.
What Problem Does This Paper Address?
Cardinality estimation (CE)—predicting how many records a database query or subquery will return—is the backbone of database query optimization. When an optimizer misjudges cardinalities, it picks suboptimal join orders and execution strategies, often causing query runtimes to degrade by multiple orders of magnitude.
Traditional relational database management systems (RDBMS) estimate cardinalities using crude statistical assumptions such as column independence and uniform data distribution. Recently, machine learning approaches have surpassed these traditional heuristics. However, existing learned CE models are almost exclusively instance-based: they are trained on a single database, rely on schema-specific features (e.g., column and table names), and require collecting tens of thousands of query execution labels per dataset.
Because prior benchmarks like the Join Order Benchmark (JOB) or STATS only evaluate one or two databases, researchers lacked the standardized, multi-database corpus needed to train and test pre-trained, transferable (zero-shot) CE models.
Methods
To enable zero-shot learned CE, the authors developed an automated benchmark creation pipeline:
- Transferable Featurization: SQL queries are converted into directed acyclic query graphs annotated with system-agnostic statistics (quantiles, null fractions, estimated predicate selectivities, and Pearson correlations). Specific identifiers (table/column names) are omitted so models learn general data-distribution patterns instead of memorizing table names.
- Automated Schema & Query Generation: The pipeline automatically infers primary-key/foreign-key (PK-FK) join relationships (assuming snowflake-like schemas) and uses a synthetic query generator to execute queries and extract true execution cardinalities (consuming over 14 CPU-years of compute).
- Model Architectures:
- Graph Neural Network (GNN): Uses node-type-specific MLPs and bottom-up message passing aligned with the query execution DAG, producing a graph-level embedding for cardinality regression.
- Graph Transformer: Implements shortest-path spatial encodings (adapted from Graphormer), directional topological causal masking, and a virtual readout node.
- Evaluation Setups: Evaluated across three paradigms:
- Instance-based (trained and tested on the same database).
- Zero-shot (leave-one-dataset-out: trained on 19 datasets, tested on the unseen 20th).
- Fine-tuned (pre-trained on 19 datasets, fine-tuned on a small slice of the 20th).
Datasets
The authors introduce CardBench, consisting of 20 real-world databases encompassing diverse domains, row counts (ranging from tens of millions to tens of billions of rows), and schema topologies (star, chain, and mixed).
- Datasets include sampled versions of
github_repos,wikipedia,stackoverflow,ethereum_blockchain,open_targets_genetics, andtpch_10G. - Queries are categorized into three sets: Single Table (1–4 filter predicates), Binary Join (joins between two tables with 1–3 filters per table), and Multi-Join (0–8 joins).
Key Insights and Ideas
- Zero-Shot CE Breaks Down on Joins: While zero-shot models generalize reasonably well to unseen single-table filters, introducing even a single binary join causes zero-shot error to spike by orders of magnitude. Cross-table correlations and multi-column join dynamics cannot be inferred from generic base-table summary statistics alone.
- Pre-Training + Fine-Tuning Is Highly Sample-Efficient: A pre-trained model fine-tuned on just 500 samples achieves tail accuracy comparable to an instance-specific model trained on twice as much data (1,000 samples). Pre-training imparts transferable structural priors that make adapting to a new database significantly cheaper.
- GNNs Edge Out Transformers for Query Plans: Despite the transformer's larger receptive field, bottom-up DAG message-passing in GNNs naturally mirrors relational dataflow, yielding lower prediction error and faster inference (35ms for GNN vs. 97ms for Transformer).
Key Results
Evaluations used the standard $q\text{-error}$ metric ($\max(y/\hat{y}, \hat{y}/y)$):
- Single-Table Queries:
- PostgreSQL baseline: Median ($P50$) $q\text{-error} = 1.03$, but tail ($P95$) degrades to $12,296.35$.
- GNN Instance: $P50 = 1.10$, $P95 = 24.08$.
- GNN Zero-Shot: $P50 = 1.39$, $P95 = 171.25$.
- GNN Fine-Tuned: $P50 = 1.22$, $P95 = 41.50$.
- Binary Join Queries:
- PostgreSQL baseline: $P50 = 5.29$, $P95 =$ $411,511.45$.
- GNN Zero-Shot: Degrades significantly ($P50 = 22.66$, $P95 =$ $16,271.84$; Transformer Zero-Shot $P95$ reaches $228,499.79$).
- GNN Fine-Tuned: Recovers performance dramatically ($P50 = 1.32$, $P95 = 109.77$), closely trailing GNN Instance ($P50 = 1.17$, $P95 = 37.55$).
- Training Efficiency: Fine-tuning the GNN required only 11 minutes on an 8-core VM (compared to ~1.3 hours for instance training), drastically reducing the barrier to adopting learned CE in practice.
Limitations and Future Work
- Pure Zero-Shot Remains Unsolved for Joins: Out-of-distribution join prediction without local fine-tuning remains too inaccurate for production database query optimizers.
- Incomplete Multi-Join Analysis: While the multi-join benchmark generation tools are released, multi-join cardinality labels were still being computed during submission, limiting experimental evaluation to single-table and binary-join workloads.
- Heuristic Schema Inference: Because many public datasets do not explicitly define PK-FK relationships, CardBench relies on heuristic thresholding (>90% uniqueness, size ratios) that may not capture arbitrary, non-snowflake relational dependencies.
Summary updated