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 PLOP, a plan-level optimizer for hybrid semantic-relational queries. PLOP 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, PLOP 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, PLOP 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, PLOP achieves a significant cost reduction while preserving the highest accuracy among six publicly available systems.
翻译:摘要:近期数据库系统引入了语义算子,这些算子利用大语言模型(LLMs)通过自然语言谓词对结构化数据进行过滤、连接和投影操作。在实际应用中,这些算子与传统关系算子(如等值连接)结合,形成混合查询计划,其执行代价同时依赖于昂贵的LLM调用和常规数据库处理。一个关键的优化问题在于如何将每个语义算子放置在计划中相对于关系算子的位置:提前放置可减少后续算子处理的数据量,但需增加LLM调用次数;延后放置可通过去重减少LLM调用,但会迫使关系算子处理更大的中间数据。现有系统要么忽略这一放置问题,要么采用不考虑完整代价权衡的简单启发式方法。我们提出PLOP,一种面向混合语义-关系查询的计划级优化器。PLOP通过两种保持等价性的重写规则,将混合查询规划问题简化为语义过滤器的放置问题。我们证明,在函数缓存机制下,将所有语义过滤器推迟至可能的最新位置可最小化LLM调用次数,但同时也表明这种做法可能使复杂多表查询中的关系处理代价占据主导地位。为平衡LLM代价与关系代价,PLOP采用基于动态规划的代价模型,以找到最小化二者加权和的最优放置方案。在涵盖五个模式与两个基准测试的44个语义SQL查询中,PLOP在保持高输出质量的同时,实现了最高1.5倍的加速比与4.29倍的代价降低:在SemBench上,其平均F1分数相对于未优化基线为0.85,相对于人工标注真值为0.84。总体而言,PLOP在六种公开系统中取得了显著代价降低,同时保持了最优准确率。