DBCopilot: Natural Language Querying over Massive Databases via Schema Routing

Tianshu Wang, Xiaoyang Chen, Hongyu Lin, Xianpei Han, Le Sun, Hao Wang, Zhenyu Zeng

Abstract

Natural language querying (NLQ) over massive database collections, such as enterprise data lakes, remains a significant challenge due to issues of schema scalability, structural complexity, and semantic mismatch. Traditional Large Language Model (LLM) approaches often fail when confronted with thousands of tables that exceed context window limits. We present DBCopilot, a framework that decouples the NLQ task into two distinct stages: schema routing and SQL generation. By utilizing a lightweight "copilot" model trained via generative retrieval on synthetic data, DBCopilot effectively navigates vast database hierarchies to provide LLMs with a relevant schema subset. Our evaluation across multiple benchmarks demonstrates that DBCopilot significantly outperforms traditional retrieval-augmented generation (RAG) methods in both routing precision and end-to-end SQL execution accuracy.

1. Introduction

While Large Language Models (LLMs) have demonstrated remarkable proficiency in translating natural language (NL) to SQL, most existing research operates under a "small-world" assumption where the model only interacts with a handful of tables. In real-world enterprise environments, data consumers often face "massive databases"—data lakes or warehouses containing hundreds of databases and thousands of tables. This environment presents three primary hurdles:

  • Schema Scalability (C1): Thousands of table definitions cannot fit into an LLM's prompt due to token limits and prohibitive costs.
  • Schema Complexity (C2): Real-world schemas possess intricate relationships (Foreign Keys, joinable columns). Simple keyword search (BM25) or vector search (DPR) often fails to identify the necessary "join paths" for complex queries.
  • Semantic Mismatch (C3): Database column names (e.g., cust_id_01) frequently do not align with how humans phrase questions ("Who are our top customers?").

2. The DBCopilot Framework

DBCopilot addresses these challenges by treating schema discovery as a "routing" problem rather than a simple retrieval task. The framework consists of a lightweight Copilot model and a downstream LLM for SQL generation.

Overview of the DBCopilot framework, showing schema routing from a massive database hierarchy to SQL generation.

2.1 Schema Routing via Generative Retrieval

Instead of traditional "index-then-retrieve" approaches like vector search, DBCopilot utilizes a Differentiable Search Index (DSI). It employs a lightweight Seq2Seq model (T5-base) trained to directly generate the identifiers of the target database and tables from an NL question. To ensure the model only generates valid paths within the database hierarchy, the system employs graph-based constrained decoding, preventing the hallucination of non-existent tables.

2.2 Relation-Aware Schema Graph

To manage structural complexity, the authors construct a Schema Graph representing databases, tables, and their relationships, including Foreign Keys and "Joinable" relations. A Depth-First Search (DFS) Serialization algorithm transforms these graph paths into token sequences that the Copilot model can learn to navigate.

3. Training via Data Synthesis

A major bottleneck in scaling to massive databases is the lack of labeled training data. DBCopilot overcomes this through a "reverse training" paradigm.

The training pipeline for the Copilot model, utilizing schema graph construction and synthetic data generation.

As shown in the training pipeline, the system uses a Schema Questioning Model that takes a sampled schema path and generates a "pseudo-question" for it. This synthetic data allows the Schema Router to learn the mapping between natural language intent and specific database structures without requiring manual labeling of thousands of tables.

4. Experimental Evaluation

The authors evaluated DBCopilot on several benchmarks adapted for massive database settings, including Spider, Bird, and Fiben (a complex financial dataset).

4.1 Key Results

  • Routing Performance: DBCopilot outperformed state-of-the-art retrieval baselines (such as BM25 and DTR) by up to 19.88% in Recall@1 on the Spider and Bird datasets.
  • End-to-End Accuracy: In schema-agnostic NL2SQL tasks, DBCopilot improved Execution Accuracy (EX) by 4.43% to 11.22% compared to other RAG methods.
  • Robustness: On the "Spider-real" dataset, which uses vague questions without explicit column mentions, DBCopilot maintained a significant lead, proving its ability to handle semantic mismatches better than keyword-based systems.

5. Discussion and Limitations

The success of DBCopilot highlights that decoupling "where the data is" (Routing) from "how to write the query" (Generation) is essential for scalability. By filtering the schema down to a minimal relevant set, the system avoids the "lost in the middle" phenomenon often seen in LLMs with long contexts.

However, limitations remain. Currently, the system routes questions to a single target database and does not support cross-database joins. Furthermore, performance is heavily dependent on the quality of synthetic data; hallucinations in the questioning model can lead to incorrect routing. Future work will focus on incremental updates for evolving data lakes and supporting multi-database queries.

Summary updated

BibTeX
@inproceedings{wang2025dbcopilot,
  author = {Tianshu Wang and Xiaoyang Chen and Hongyu Lin and Xianpei Han and Le Sun and Hao Wang and Zhenyu Zeng},
  title = {{DBCopilot: Natural Language Querying over Massive Databases via Schema Routing}},
  booktitle = {EDBT},
  year = {2025},
  url = {https://openreview.net/forum?id=BwHro3J4DK},
}
Figure