Recent database systems have introduced semantic operators that leverage large language models (LLMs) to filter, join, and project over structured data using natural language predicates. In practice, these operators are combined with traditional relational operators, e.g., equi-joins, producing hybrid query plans whose execution cost depends on both expensive LLM calls and conventional database processing. A key optimization question is where to place each semantic operator relative to the relational operators in the plan: placing them earlier reduces the data that subsequent operators process, but requires more LLM calls; placing them later reduces LLM calls through deduplication, but forces relational operators to process larger intermediate data. Existing systems either ignore this placement question or apply simple heuristics without considering the full cost trade-off. We present Horrila, a plan-level optimizer for hybrid semantic-relational queries. Horrila reduces hybrid query planning to semantic filter placement via two equivalence-preserving rewrites. We prove that deferring all semantic filters to the latest possible position minimizes LLM invocations under function caching, but show that this can cause relational processing costs to dominate on complex multi-table queries. To balance LLM cost against relational cost, Horrila uses a dynamic-programming-based cost model that finds the placement minimizing their weighted sum. On 44 semantic SQL queries across five schemas and two benchmarks, Horrila achieves up to 1.5$\times$ speedup and 4.29$\times$ cost reduction while maintaining high output quality: an average F1 of 0.85 against the unoptimized baseline and 0.84 against human-annotated ground truth on SemBench. Overall, Horrila achieves a significant cost reduction while preserving the highest accuracy among six publicly available systems.
翻译:近期数据库系统引入了利用大语言模型通过自然语言谓词对结构化数据进行过滤、连接与投影的语义算子。实践中,这些算子需与传统关系算子(如等值连接)协同使用,形成混合查询计划——其执行代价既取决于昂贵的LLM调用开销,也受传统数据库处理成本影响。一个关键优化问题是如何在查询计划中合理布局语义算子与关系算子的相对位置:前置语义算子可减少后续算子处理的数据量,但将增加LLM调用次数;后置语义算子可通过去重减少LLM调用,但迫使关系算子处理更大的中间数据集。现有系统要么忽视此布局问题,要么采用未考虑全成本平衡的简单启发式方法。本文提出Horrila——面向混合语义-关系查询的计划级优化器。通过两种等价保留重写规则,Horrila将混合查询规划问题简化为语义过滤器布局问题。我们证明在函数缓存机制下,将所有语义过滤器推迟至最终位置可最小化LLM调用次数,但实验表明该策略会导致复杂多表查询中关系处理成本占比过高。为平衡LLM成本与关系成本,Horrila采用基于动态规划的代价模型,寻找使加权和最小的算子布局方案。在涵盖五个模式与两个基准的44条语义SQL查询中,Horrila实现高达1.5倍加速与4.29倍成本降低,同时保持优异输出质量:在SemBench上平均F1值达到0.85(对比未优化基线)与0.84(对比人工标注基准)。整体而言,Horrila在六个公开系统中以最高精度实现了显著的成本降低。