DB -Explore: Automated Database Exploration and Instruction Synthesis for Text-to- SQL (2025)

Abstract: Recent text-to-SQL systems powered by large language models (LLMs) have demonstrated remarkable performance in translating natural language queries into SQL.However, these systems often struggle with complex database structures and domain-specific queries, as they primarily focus on enhancing logical reasoning and SQL syntax while overlooking the critical need for comprehensive database understanding.To address this limitation, we propose DB-Explore, a novel framework that systematically aligns LLMs with database knowledge through automated exploration and instruction synthesis.DB-Explore constructs database graphs to capture complex relational schemas, leverages GPT-4 to systematically mine structural patterns and semantic knowledge, and synthesizes instructions to distill this knowledge for efficient fine-tuning of LLMs.Our framework enables comprehensive database understanding through diverse sampling strategies and automated instruction generation, bridging the gap between database structures and language models.Experiments conducted on the SPIDER and BIRD benchmarks validate the effectiveness of DB-Explore, achieving an execution accuracy of 67.0% on BIRD and 87.8% on SPIDER. Notably, our open‐source implementation based on Qwen2.5‐Coder‐7B achieves state‐of‐the‐art results at minimal computational cost, outperforming several GPT‐4‐driven Text‐to‐SQL systems.

## introduction text-to-sql systems have emerged as a crucial bridge between humans and structured data, enabling natural language interaction with databases without requiring sql expertise. while recent advances in large language models (llms), including chatgpt @xcite , gpt-4 (achiam † corresponding author. @xcite , and qwen2 @xcite , have significantly improved these systems' capabilities, demonstrating enhanced comprehension of user queries and the ability to generate complex sql queries, they still face fundamental challenges in understanding complex database structures and generating accurate sql queries, particularly for domain-specific applications @xcite . a key limitation of current approaches is their reliance on direct query translation without deep understanding of the underlying database structure and semantics. existing methods primarily focus improving prompting strategies @xcite @xcite or enhancing the reasoning ability of llms through supervised fine-tuning @xcite @xcite , but often overlook the critical role of database comprehension in query generation. while techniques like schema linking @xcite , question decomposition @xcite and self-correction @xcite have attempted to address this gap, they typically treat the database as a static structure rather than a rich source of semantic knowledge that can inform the query generation process. we argue that effective text-to-sql systems must first develop a comprehensive understanding of the database through systematic exploration before attempting query generation. this exploration should capture not only the structural relationships between tables but also the semantic patterns and domain-specific knowledge embedded within the schema. however, automated database exploration presents several challenges: (1) complex structural relationships: real-world databases often contain intricate networks of table relationships that are difficult to capture and represent effectively. (2) hidden semantic patterns: domain-specific knowledge embedded in schema elements (table names, column names, etc.) is often implicit and requires careful analysis to uncover. (3) query complexity progression: understanding how simple queries can be systematically extended to handle more complex scenarios requires a structured approach to knowledge acquisition. to address these challenges, we propose db-explore, a novel framework that transforms textto-sql training through systematic database exploration and instruction synthesis. at the core of our approach is a graph-based database representation technique called db graph, which captures both structural relationships and semantic patterns within the database. this representation serves as a foundation for three key components: (1) semantic knowledge extraction: we develop techniques to uncover domain-specific knowledge embedded in schema elements, enabling more contextually appropriate query generation. (2) structural pattern mining: by analyzing the db graph, our system identifies common join patterns, table relationships, and query templates that reflect the database's architectural design. (3) progressive instruction synthesis: using the extracted knowledge, we automatically generate training examples that progress from simple to complex queries, allowing models to gradually master increasingly sophisticated query patterns. our framework leverages these components to create comprehensive training datasets that capture both the structural and semantic complexity of the target database. this approach fundamentally differs from existing methods by treating database exploration as a crucial prerequisite for effective query generation, rather than relying solely on input-output pairs or schema information. the main contributions of this work are: ## related work early text-to-sql systems relied on handcrafted, rule-based parsers to map natural language into sql @xcite . the advent of large language models (llms) such as gpt-4 @xcite shifted focus to leveraging rich language priors for sql generation @xcite . building on this paradigm, schema-focused methods such as resd-sql, dts-sql, and rsl-sql enhance schema linking and selection to ground queries in database structures @xcite @xcite , while decomposition-based techniques like din-sql, ptd-sql, and qpl break complex queries into simpler sub-tasks @xcite @xcite . msc-sql, sql-palm, distillery, and xiyan improve robustness through multi-candidate generation and ranking @xcite @xcite , and skeleton-aware selection in dail-sql refines few-shot demonstrations @xcite . self-correction frameworks in mac and epi-sql further reduce errors via iterative feedback @xcite . in parallel, competitive open-source llms have spurred fine-tuning frameworks that exploit synthetic or task-specific data. codes incrementally pre-trains starcoder on sql corpora and integrates external plugins for enhanced performance @xcite , whereas dts-sql and sense employ schema-aware fine-tuning and direct preference optimization (dpo) @xcite to improve accuracy @xcite . route unifies multi-task tuning through targeted data synthesis @xcite . recently, reinforcement learning (rl) methods have emerged as a promising direction in the textto-sql field. sql-o1 @xcite employs monte carlo tree search (mcts) @xcite to enhance the reasoning process during sql generation. sql-r1 @xcite and reasoning-sql @xcite leverage the grpo @xcite framework and incorporate domain-specific reward functions, improving the accuracy and robustness of text-to-sql systems. despite these advances, most work remains focused on direct sql generation, often overlooking holistic database understanding. ## methodology in this section, we present a detailed explanation of db-explore, a fully automated framework that leverages llms to explore databases, generate data, and perform supervised fine-tuning. as illustrated in figure 1 , the framework comprises three core components: 1) database graph construction, 2) database multi-knowledge exploration, and 3) supervised fine-tuning. db-explore initiates with the construction of db graph, which captures the intricate topological relationships within the database. by applying various sampling strategies, we extract subgraphs containing diverse information. db-explore then converts this database knowledge into a instruction dataset through semantic knowledge extraction, structural pattern mining, and progressive instruction synthesis. to ensure the quality of the generated instructions, an instruction filtering mechanism is employed. finally, the extracted knowledge is injected into the base model via supervised fine-tuning. in the following subsections, we present a detailed description of each component of db-explore. ## experiments benchmarks (1) spider the spider dataset @xcite is a large-scale, cross-domain benchmark for text-to-sql tasks, designed to evaluate the generalization ability of models across diverse database structures. it consists of 8,659 training samples, and 1,034 development samples, spanning 200 databases and 138 domains. (2) bird the bird dataset @xcite ) is a more challenging text-to-sql benchmark compared to spi-der, featuring 95 real-world databases across 37 professional domains, with a total storage size of 33.4gb.