Abstract
Large Language Models have recently shown impressive capabilities in reasoning and code generation, making them promising tools for natural language interfaces to relational databases. However, existing approaches often fail to generalize in complex, real-world settings due to the highly database-specific nature of SQL reasoning, which requires deep familiarity with unique schemas, ambiguous semantics, and intricate join paths. To address this challenge, we introduce a novel two-stage LLM-based framework that decouples knowledge acquisition from query generation. In the Exploration Stage, the system autonomously constructs a database-specific knowledge base by navigating the schema with a Monte Carlo Tree Search-inspired strategy, generating triplets of schema fragments, executable queries, and natural language descriptions as usage examples. In the Deployment Stage, a dual-agent system leverages the collected knowledge as in-context examples to iteratively retrieve relevant information and generate accurate SQL queries in response to user questions. This design enables the agent to proactively familiarize itself with unseen databases and handle complex, multi-step reasoning. Extensive experiments on large-scale benchmarks demonstrate that our approach significantly improves accuracy over strong baselines, highlighting its effectiveness and generalizability.
1. Introduction
Translating natural language to SQL (Text-to-SQL) is a critical capability for democratizing data access. While Large Language Models (LLMs) excel at generic code generation, their performance often degrades significantly when deployed on real-world, enterprise-level databases. This "generalization gap" occurs because SQL reasoning is inherently database-specific; a valid query depends on unique schema structures, ambiguous column names, and undocumented join paths.
Human data engineers do not attempt to write complex queries zero-shot; they explore tables, test simple queries, and build familiarity with the data logic first. Existing LLM agents lack this crucial exploration step, suffering from a "cold-start" problem. To solve this, we propose SQLAgent, a framework that decouples knowledge acquisition from query generation into two distinct stages.
2. Methodology
2.1 Exploration Stage (Offline)
Before the system interacts with a user, it autonomously explores the database to build a custom knowledge base. This stage mimics the learning process of a human engineer through several key components:
- Schema Abstraction: The database is converted into a traversable graph. To handle the complexity of redundant or time-sharded tables (e.g., daily event logs), we introduce Shared Field Groups—canonical templates that drastically reduce schema complexity.
- LLM-Guided Tree Search: Using a strategy inspired by Monte Carlo Tree Search (MCTS), an LLM acts as a reasoning engine to navigate the schema graph. It selects columns, adds constraints, and executes test queries.
- Triplet Generation: When a query executes successfully and returns non-empty results, the system saves a triplet: the schema sub-structure, the executable SQL, and an LLM-generated natural language description. This forms the foundation of the database-specific knowledge base.
2.2 Deployment Stage (Online)
When a user submits a question, a dual-agent system takes over to synthesize the final query:
- InfoAgent: This agent extracts keywords, retrieves relevant schema components, and expands the context (e.g., inferring necessary foreign keys for complex joins).
- GenAgent: It searches the offline-generated knowledge base for structurally similar triplets to use as highly relevant, database-specific few-shot prompts. It then synthesizes the final SQL. If the query fails execution, the agents enter a collaborative refinement loop to fix the error.
3. Experimental Results
3.1 Performance on Enterprise Benchmarks
We evaluated SQLAgent on the Spider 2.0-Snow benchmark, which represents enterprise-level complexity with ~150 databases and an average of 800 columns each. SQLAgent achieved an overall Execution Accuracy (EX) of 25.78%, significantly outperforming strong baselines like ReFoRCE (20.84%) and Spider-Agent (12.98%). Notably, on the "Hard" subset of queries, SQLAgent achieved 12.21% accuracy, compared to the baseline's 0.00%.
3.2 Efficiency and Scaling
Despite the complex reasoning involved, the deployment stage remains efficient, requiring an average of only 5.2 LLM calls and 3.6 database calls per query. Furthermore, the two-stage architecture acts as a model-agnostic booster. For example, it more than doubled the execution accuracy of the 7B-parameter Qwen2.5 model, proving that structured exploration can compensate for lower raw reasoning capacity in smaller models.
4. Conclusion
SQLAgent effectively bridges the "schema familiarity" gap by allowing agents to explore and learn from a database before generation. While challenges remain regarding extreme logical nesting and fine-grained semantic disambiguation in massive schemas, the decoupling of knowledge acquisition from query generation provides a robust path forward for enterprise Text-to-SQL applications.
Summary updated