While Large Language Models (LLMs) offer promise for automating SQL query rewriting, their ”black-box” nature presents a major obstacle to practical adoption. The opacity of their reasoning leads to a lack of trust, unexplainable logic, and a high risk of generating syntactically or semantically flawed queries. Furthermore, because of search space pruning and strict cost models, classic database optimizers, despite their robustness, frequently struggle to handle the intricate structural transformations needed for contemporary analytical workloads. We provide Blueprint-SQL, a novel framework that reframes rewriting from opaque text production to transparent, organized planning, in order to address these issues. Our approach trains an LLM agent via reinforcement learning to produce a ”rewrite blueprint”—a high-level, human-readable sequence of composable actions like CONVERT_SUBQUERY_TO_CTE or REORDER_JOIN. This blueprint is then deterministically executed by applying each action to the query’s Abstract Syntax Tree (AST), a process that ensures the final output remains syntactically valid within the supported action space. This architecture decouples strategic reasoning from low-level code generation, yielding three key advantages: built-in explainability, guaranteed robustness, and learned adaptability. Through extensive evaluation against production-grade RDBMS optimizers (PostgreSQL), hybrid LLM-rule systems, and black-box generative models, we demonstrate that Blueprint-SQL achieves state-of-the-art performance on standard benchmarks. Critically, it maintains 100
更多
查看译文
关键词
SQL query optimization,Large Language Models (LLMs),Explainable AI (XAI),Reinforcement learning,Program synthesis