Query rewriting, the process of transforming queries into semantically equivalent yet more efficient variants, is crucial for database optimization. Existing solutions predominantly rely on either rule-based heuristics or Large Language Models (LLMs). However, traditional rule-based methods lack adaptability, while LLM-based approaches incur prohibitive inference costs and privacy risks. In contrast, Small Language Models (SLMs) present a compelling middle ground, potentially offering both flexibility and efficiency. However, the development of such compact models is severely bottlenecked by the scarcity of high-quality, domain-specific training data. To bridge this gap, we introduce LASER, a data-centric framework designed to empower small models for robust SQL optimization. First, to address the scarcity of existing benchmarks and the limited optimization headroom of generic synthetic queries, we construct SQL-MCTS, a large-scale corpus of complex slow queries. We employ an MCTS-based hybrid expansion strategy that combines rule-guided anti-patterns with LLM mutations to evolve structurally expressive seeds into execution-verified slow variants. Second, to enable the model to autonomously discover latency-aware rewriting patterns, we propose SQL-GRPO, a specialized alignment strategy adapted from Group Relative Policy Optimization. By integrating Anchored Group Advantage to refine advantage estimation and Complexity-Adaptive Dynamic Rollout to efficiently allocate exploration budgets, this approach effectively empowers compact models to master execution-based optimization logic. Implemented on Qwen3 models, LASER significantly outperforms rule-based systems and LLMs in execution efficiency, while exhibiting robust zero-shot transferability with minimal overhead.
翻译:查询重写,即将查询转换为语义等价但执行效率更高的变体,是数据库优化的关键环节。现有解决方案主要依赖基于规则的启发式方法或大型语言模型(LLM)。然而,传统规则方法缺乏适应性,而基于LLM的方法则面临高昂推理成本与隐私风险。相比之下,小型语言模型(SLM)提供了一个兼具灵活性与效率的折中方案。然而,此类紧凑型模型的开发受限于高质量领域特定训练数据的稀缺性。为弥补这一不足,我们提出LASER——一个旨在赋能小模型实现稳健SQL优化的数据驱动框架。首先,针对现有基准数据稀缺及通用合成查询优化空间有限的问题,我们构建了SQL-MCTS——一个包含复杂慢查询的大规模语料库。我们采用基于蒙特卡洛树搜索(MCTS)的混合扩展策略,将规则引导的反模式与LLM变异相结合,将结构表达力丰富的种子查询逐步演化为经执行验证的慢查询变体。其次,为使模型能够自主发现时延感知的重写模式,我们提出SQL-GRPO——一种基于分组相对策略优化改进的特化对齐策略。该策略通过引入锚定组优势来优化优势估计,并采用复杂度自适应动态展开机制高效分配探索预算,有效赋能紧凑模型掌握基于执行的优化逻辑。基于Qwen3模型实现的LASER在执行效率上显著超越规则系统与LLM,同时以极低开销展现出强大的零样本迁移能力。