KELM: Knowledge-Enhanced Learning Methodology for Cardinality Estimation

Yu Yan, Hongzhi Wang, Jian Geng, Jian Ma, Yuzhuo WangResearch 2022

Abstract

Recent years, the database community has attempted to develop learned cardinality estimator for improving the estimation accuracy. Although some researches show that the applying deep learning to cardinality estimation is a significant and promising direction, there still exists many problems in implementing these techniques to real applications (long preparation time, unstable performance and poor model transition). We comprehensively analyze the characteristics of statistical estimators which have the potential ability to enhance the learned estimators for solving these problems. After carefully design, we propose a knowledge-enhanced learning methodology for cardinality estimation, called KELM. To our knowledge, KELM is the first work to regard the statistical estimators as knowledge for enhancing the learned estimators. Specifically, we propose a knowledge enhanced preparation phase which could accelerate the model pretraining by reducing queries execution. For online phase, we design stable model inference algorithm and a two-stage transition method for efficiently adapting to dynamic workloads. Experimental results in four open source datasets demonstrate that KELM outperforms existing works in model preparation, model inference and model transition. Our work brings a new development direction for the application of learned estimators in real database environments.

Introduction

In database management, cardinality estimation—predicting how many rows a specific query will return—is the backbone of query optimization. Historically, databases relied on statistical methods like histograms or sampling. While these are fast and mathematically stable, they often struggle with complex, multi-table queries.

Recently, the database community has turned to learned estimators (deep learning models). While neural networks offer massive leaps in accuracy, they introduce three critical roadblocks for real-world production databases:

  1. Long Preparation Time: Training requires executing thousands of queries to gather labels, which can take hours or days.
  2. Unstable Performance: As "black boxes," neural networks can produce wildly inaccurate estimates in edge cases, leading to catastrophic execution plans.
  3. Poor Model Transition: When data distributions change, models become obsolete and require expensive retraining.

To address these issues, the authors propose KELM (Knowledge-Enhanced Learning Methodology), a hybrid framework that uses traditional statistical estimators as "knowledge bases" to supervise and enhance learned models.

Methodology

KELM treats statistical methods and deep learning as complementary. Instead of training from scratch on raw data, KELM uses statistical methods as an Experience Knowledge Base (EKB) to pre-train and bound the learned model.

The KELM framework architecture, illustrating the integration of statistical estimators as a knowledge base for learned models.

Preparation Phase (Fast Pre-training)

Instead of running thousands of slow queries on the actual database to get training labels, KELM queries the EKB (standard statistical estimators like Postgres' default, QuickSel, or Bayes). The neural network pre-trains on these statistical estimates, bypassing the need for actual query execution and generating training data almost instantly.

Online Phase (Inference & Transition)

  • Safe Inference: To solve instability, KELM runs both the learned model and the statistical model simultaneously. If the neural network's prediction diverges too far from the statistical baseline (exceeding a calculated theoretical bound), KELM rejects the neural network and falls back to the safe statistical guess.
  • Two-Stage Transition: When workloads change, KELM instantly updates the computationally cheap statistical model. As real queries execute, it stores actual results in a "Practical Pool" to gradually retrain the neural network in the background, ensuring zero downtime in accuracy.

Results and Key Insights

The authors evaluated KELM using four real-world datasets: Census (4.8MB), Forest (44.3MB), Power (110.8MB), and DMV (972.8MB).

  • Massive Speedup: Model preparation time dropped from over 8 hours (for a standard MSCN model) to under 5 seconds across all datasets (e.g., 4.19 seconds for the DMV dataset).
  • Maintained Accuracy: KELM-enhanced models successfully adapted to dynamic workloads, matching or exceeding the accuracy (measured in Q-error) of traditionally trained models after just a few background retraining cycles.
  • Robustness: The system proved robust to different retraining intervals, showing that statistical models are excellent "synthetic" teachers that allow the neural network to learn the general shape of data instantly.

Conclusion

KELM demonstrates that statistical models can provide the necessary theoretical error bounds and rapid adaptation required to make deep learning practical for database cardinality estimation. By using a two-speed adaptation strategy, KELM ensures that database systems remain accurate even during significant workload shifts. Future work will focus on the automatic construction of the Experience Knowledge Base to further reduce manual configuration requirements.

Summary updated

BibTeX
@inproceedings{yan2022kelm,
  author = {Yu Yan and Hongzhi Wang and Jian Geng and Jian Ma and Yuzhuo Wang},
  title = {{KELM: Knowledge-Enhanced Learning Methodology for Cardinality Estimation}},
  booktitle = {Research},
  year = {2022},
  doi = {10.21203/rs.3.rs-2290678/v1},
}
Figure