Skip to main content
QUICK REVIEW

[论文解读] Flow-Loss: Learning Cardinality Estimates That Matter

Parimarjan Negi, Ryan Marcus|arXiv (Cornell University)|Jan 13, 2021
Machine Learning and Algorithms参考文献 47被引用 10
一句话总结

本文提出 Flow-Loss,一种新型可微分损失函数,通过将基数估计器的训练目标从整体准确率转向对查询优化器决策影响最大的估计值,从而提升实际查询性能。通过将查询优化建模为计划图上的流路由问题,并反向传播计划成本梯度,Flow-Loss 在估计准确率低于 Q-Error 的情况下仍能提升实际查询性能,尤其在未见查询模板上表现更优,实现最高 1.5 倍的运行速度提升。

ABSTRACT

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.

研究动机与目标

  • 解决现有学习型基数估计器的局限性:其优化目标为平均估计误差(如 Q-Error),但该目标与实际查询性能不一致。
  • 识别出对查询优化器决策影响最大的基数估计值,并优先改进这些估计以提升计划质量。
  • 开发一种可微分、平滑的优化器成本模型与动态规划搜索的近似方法,以支持基于梯度的训练。
  • 评估 Flow-Loss 是否在未见查询模板上实现比标准 Q-Error 训练更好的泛化能力。
  • 证明 Flow-Loss 训练的模型具有更好的泛化能力,能避免对无关或噪声估计的过拟合,即使特征工程减少亦能保持性能。

提出的方法

  • 将查询优化形式化为计划图上的最短路径问题,其中路径代表不同的查询计划。
  • 使用解析式、可微分函数近似动态规划优化器与成本模型,以计算计划成本相对于基数估计值的梯度。
  • 推导出 Flow-Loss 作为可微分代理损失,通过反向传播计划成本梯度,优化对计划影响最大的基数估计值。
  • 利用电流传导公式中的结构,通过矩阵求逆(B(Y)⁻¹)高效计算梯度,提升可扩展性。
  • 在真实世界工作负载上使用 Flow-Loss 训练神经网络模型(FCNN、MSCN),并与 Q-Error 和 PostgreSQL 默认设置进行比较。
  • 通过新基准(CEB)验证结果,该基准包含 21 种模板、超过 16,000 个查询,最多支持 15 次连接,提供真实基数。

实验结果

研究问题

  • RQ1通过 Flow-Loss 最小化计划成本训练的学习型基数估计器,是否能在实际查询性能上优于以估计准确率(Q-Error)为目标训练的模型?
  • RQ2Flow-Loss 是否能提升对训练期间未见的查询模板的泛化能力,尤其是在模型容量受限的情况下?
  • RQ3Flow-Loss 与 Q-Error 相比,在特征工程变化和数据分布偏移下是否更具鲁棒性?
  • RQ4Flow-Loss 训练的模型是否能在训练数据存在噪声或部分不准确时仍保持性能优势?
  • RQ5Flow-Loss 在多大程度上减少了对不影响计划选择的基数估计值的过拟合?

主要发现

  • Flow-Loss 训练的模型在平均 Q-Error 高于 Q-Error 训练模型的情况下,仍能降低查询计划成本(PPC)并提升运行时间。
  • 在未见查询模板上,Flow-Loss 模型的平均查询运行时间相比 Q-Error 模型最高提升 1.5 倍,即使使用相同的模型架构和训练数据。
  • Flow-Loss 模型泛化能力更强:当移除特征工程组件时,Flow-Loss 模型性能保持稳定,而 Q-Error 模型的 PPC 最多恶化 2 倍。
  • PostgreSQL 启发式特征(如基数和成本估计)对泛化至关重要;其移除会损害两类模型,但 Flow-Loss 模型更具鲁棒性。
  • Flow-Loss 模型对分布偏移具有更优鲁棒性:在稍有不同的工作负载上不会出现显著性能下降,而 Q-Error 模型可能比 PostgreSQL 慢最多 1.5 倍。
  • Flow-Loss 的训练开销比 Q-Error 高 3–5 倍,主要源于梯度计算中的矩阵求逆,但推理速度仍很快(数毫秒),适用于查询优化。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。