Abstract: Text-to-SQL is a fundamental yet challenging task in the NLP area, aiming at translating natural language questions into SQL queries. While recent advances in large language models have greatly improved performance, most existing approaches depend on models with tens of billions of parameters or costly APIs, limiting their applicability in resource-constrained environments. For real world, especially on edge devices, it is crucial for Text-to-SQL to ensure cost-effectiveness. Therefore, enabling the light-weight models for Text-to-SQL is of great practical significance. However, smaller LLMs often struggle with complicated user instruction, redundant schema linking or syntax correctness. To address these challenges, we propose MCTS-SQL, a novel framework that uses Monte Carlo Tree Search to guide SQL generation through multi-step refinement. Since the light-weight models' weak performance of single-shot prediction, we generate better results through several trials with feedback. However, directly applying MCTS-based methods inevitably leads to significant time and computational overhead. Driven by this issue, we propose a token-level prefix-cache mechanism that stores prior information during iterations, effectively improved the execution speed. Experiments results on the SPIDER and BIRD benchmarks demonstrate the effectiveness of our approach. Using a small open-source Qwen2.5-Coder-1.5B, our method outperforms ChatGPT-3.5. When leveraging a more powerful model Gemini 2.5 to explore the performance upper bound, we achieved results competitive with the SOTA. Our findings demonstrate that even small models can be effectively deployed in practical Text-to-SQL systems with the right strategy.
Introduction
Translating natural language into SQL (Text-to-SQL) is a critical task for data accessibility. While frontier models like GPT-4 excel at it, they are often impractical for edge devices or privacy-sensitive environments due to cost and latency. Smaller, open-source models typically struggle with complex schemas and intricate joins.
The standard approach to fixing these errors is "single-shot" prediction. When that fails, researchers turn to iterative refinement. However, simple refinement is often unguided. Furthermore, running an LLM multiple times creates massive computational overhead. MCTS-SQL addresses these issues by combining guided search with an efficient caching mechanism.
Methods
The MCTS-SQL framework decomposes the generation process into three distinct phases:
- The Selector: To prevent "prompt overflow" in small models, the Selector filters massive database schemas to identify only the most relevant tables and columns.
- The Direct Generator: This component produces an initial SQL candidate using Chain-of-Thought (CoT) prompting. If the query passes verification, the process terminates.
- The MCTS-Refiner: If the initial query fails, the system enters a search loop using Monte Carlo Tree Search (MCTS). It explores a tree of potential SQL fixes, where each node represents a candidate query. The system generates a critique, refines the SQL, and evaluates the result to assign a reward score.
To mitigate latency, a token-level prefix-cache mechanism stores intermediate K/V states of the static prompt parts (schema and few-shot examples). This allows the model to skip redundant processing during search iterations.
Key Results
Evaluations on the Spider and BIRD benchmarks demonstrate that search can effectively substitute for model scale.
- Small Model Superiority: Using Qwen2.5-Coder-1.5B, MCTS-SQL achieved 40.69% Execution Accuracy (EX) on the BIRD dev set, surpassing ChatGPT-3.5 (36.64%).
- Pushing the SOTA: When paired with Gemini 2.5, the framework reached 72.91% EX on BIRD, achieving results competitive with the current State-of-the-Art.
- Efficiency: The prefix-cache reduced average latency per query by 53% (from 6.12s to 2.84s).
Key Insights
- Search as a Substitute for Scale: A 1.5B parameter model using MCTS can outperform models many times its size by being allowed to "think" through multiple trials.
- The Criticality of Refinement: Ablation studies showed that without the MCTS-Refiner, accuracy for the 1.5B model plummeted from 40.7% to 16.8%.
- Error Evolution: MCTS is excellent at fixing syntax and schema errors, but struggles with "deep" semantic issues where the model fundamentally misunderstands complex user intent.
Limitations and Future Work
MCTS-SQL still faces challenges with ambiguous user intent and very deep multi-table joins. Future research will focus on "distilling" the reasoning process, using search data to fine-tune small models to achieve high accuracy in a single shot.
Summary updated