SQLGovernor: An LLM-powered SQL Toolkit for Real World Application

Jie Jiang, Siqi Shen, Haining Xie, Yang Li, Yu Shen, Danqing Huang, Bo Qian, Yinjun Wu, Wentao Zhang, Bin Cui, Peng Chen · DOI

Abstract

SQLGovernor is a unified, LLM-powered toolkit designed to automate the correction, optimization, and verification of complex SQL queries in real-world analytical environments. By implementing a novel "fragment-wise" processing strategy and a self-improving knowledge base, it boosts the performance of state-of-the-art models by up to 10% and significantly reduces the manual workload for database experts.

1. Introduction

In the landscape of Big Data, crafting accurate and efficient SQL for Online Analytical Processing (OLAP) is notoriously difficult. Queries are often massive, deeply nested, and computationally expensive. Current solutions suffer from three primary "pain points": fragmented toolsets that lack holistic optimization, a "complexity wall" where LLMs hallucinate on long SQL strings, and the high cost of maintaining expert-driven optimization rules. SQLGovernor addresses these by providing an integrated, self-learning framework.

2. System Architecture and Knowledge Management

SQLGovernor introduces a structured framework that integrates four specialized tools: a Syntax Error Corrector, a Query Rewriter, a Query Modifier, and an Equivalence Verifier.

The SQLGovernor architecture integrates specialized tools with a self-learning knowledge management module.

Central to the system is a Knowledge Management module that stores abstract rules and historical examples. It utilizes a "data flywheel" approach: when a query fails or performs poorly, an LLM agent analyzes DBMS logs to extract new rules. These are then clustered and validated by experts before being integrated into the permanent knowledge base. This mechanism allows the system to evolve without constant manual input.

3. Fragment-Wise Processing Strategy

A core innovation of SQLGovernor is its recursive decomposition of SQL into smaller, self-contained "fragments" (such as CTEs and subqueries). Instead of processing a 500-line query in one shot, the system analyzes each fragment independently, reducing the cognitive load on the LLM.

The two-stage processing pipeline: Broad Assessment for rule matching and Detailed Analysis for LLM reasoning.

As shown in the workflow, the system first performs a Broad Assessment to match fragments with existing rules. If a match is found, it retrieves the relevant description to fill a prompt template. If no match exists, it attempts to match with general efficiency rules. This is followed by a Detailed Analysis where the LLM performs reasoning on the specific fragment, returning results in a structured JSON format. This modularity ensures high accuracy even for queries exceeding 500 tokens.

4. Experimental Evaluation

The researchers evaluated SQLGovernor using three datasets: BIRD, BIRD-CRITIC, and a new industrial dataset, Payment-SQL, which features queries averaging 421 tokens—significantly longer than those in academic benchmarks.

Key Results

  • Performance: SQLGovernor improved Execution Accuracy (EX) by 6.8% and Valid Efficiency Score (VES) by 8.0% when used as a post-processor for CodeS-15B.
  • Efficiency: On the Payment-SQL dataset, the toolkit achieved a 45.92% average reduction in execution time.
  • Productivity: In a study with 60 practitioners, the integrated framework was 33% faster than manual tools, with non-experts seeing a 41% productivity gain.

5. Conclusion and Future Work

SQLGovernor demonstrates that modular, fragment-wise reasoning is key to handling enterprise-grade SQL. While the multi-stage pipeline increases inference latency (e.g., from ~9s to ~20s in some tests), the gains in accuracy and efficiency are substantial. Future work will focus on optimizing latency and expanding the system's scope to include complex Data Definition Language (DDL) operations and diverse database dialects.

Summary updated

BibTeX
@misc{jiang2025sqlgovernor,
  author = {Jie Jiang and Siqi Shen and Haining Xie and Yang Li and Yu Shen and Danqing Huang and Bo Qian and Yinjun Wu and Wentao Zhang and Bin Cui and Peng Chen},
  title = {{SQLGovernor: An LLM-powered SQL Toolkit for Real World Application}},
  year = {2025},
  eprint = {2509.08575},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2509.08575},
}
Figure