Manuscript Offprint

arXiv:2611.18338

MEM-SQL: Memory-Empowered Text-to-SQL

Xiaoqi Wang, Wenqi Pei, Shizheng Hou, Yilai Liu, Han Chen, Boyan Li, Yuyu Luo, Hongyang Du

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Text-to-SQL systems translate natural language questions into SQL queries over increasingly complex databases. However, widely used benchmarks evaluate questions independently, overlooking settings where earlier queries over the same database can supply joins or calculations for a new question. We introduce HARE (History-AwaRe Text-to-SQL BEnchmark) to measure the accuracy and cost of adapting historical SQL. HARE comprises 1,600 requests derived from a fixed pool of 823 verified queries across four datasets. Annotations record which components each request preserves and which edits it requires, distinguishing whole-query adaptation from partial reuse. Our method, MEM-SQL (Memory-Empowered Text-to-SQL), stores whole-query templates, reusable SQL blocks, and mappings from domain concepts to schema elements. MEM-SQL uses a layer only if every requirement assigned to it has a match above that layer's threshold. With On-demand Schema Linking (OSL), MEM-SQL consults the schema only when no layer qualifies or the generator judges retrieved evidence insufficient. Across six language models on HARE, mean execution accuracy rises from 37.1% for Direct Generation without history to 50.0% for MEM-SQL, with 76.6% fewer input tokens and 68.8% lower latency on average. Further analysis shows that requests reusing several verified queries remain a bottleneck, with accuracy falling from 56.7% to 29.7%, most sharply when reused components must be placed in a new query structure or combined with a shared join. Applying OSL to three other memory designs consistently reduces generation cost, while its accuracy benefit depends on the memory design. Verified history can thus make generation both more accurate and cheaper, but its value depends on the evidence retained and when the system consults the schema.

1 Introduction

Text-to-SQL translates a natural language question into a SQL query that retrieves the answer (Hong et al., 2024; Liu et al., 2025). Its benchmarks have grown in scope and difficulty. Spider tests generalization across databases (Yu et al., 2018), BIRD asks questions that depend on database values (Li et al., 2023), and Spider 2.0 introduces enterprise tasks over schemas that can exceed 1,000 columns (Lei et al., 2024). Recent systems retrieve relevant schema elements, generate candidate queries, and test them against database (Talaei et al., 2024; Pourreza et al., 2024; Liu et al., 2026b; Wang et al., 2023; Li et al., 2026; Pei et al., 2025). AskData achieves 83.40% execution accuracy on BIRD test set by pairing offline meta-data extraction with iterative, draft SQL driven schema linking to resolve schema ambiguities (Shkapenyuk et al., 2025; BIRD Team, 2026).

However, benchmarks such as Spider and BIRD (Yu et al., 2018; Li et al., 2023) evaluate questions independently, overlooking a setting in which users repeatedly query the same database (Ma et al., 2018; Sun et al., 2025; Jiang et al., 2025). Verified queries from an earlier request may already answer a new question or supply part of the computation it requires. Access to these queries can spare the system of reconstructing the same logic from schema (Regmi & Pun, 2024), a costly step on large databases (Lian et al., 2024; Wang et al.,

*Equal Contribution ♢Project Lead

2025a). Yet a query can still execute even when some of its logic no longer matches the new request (Zhong et al., 2020; Xu et al., 2025a; Pei et al., 2026). To assess the value of query history, a benchmark must identify what carries over between requests and measure how access to that history affects accuracy and generation cost (Peng et al., 2025; Piao et al., 2025; Hou et al., 2026), across different types of edits.

To bridge this gap, we introduce HARE (History-AwaRe Text-to-SQL BEnchmark). A total of 1,600 requests are derived from 823 verified queries drawn from Spider (Yu et al., 2018), BIRD (Li et al., 2023), Spider 2.0 (Lei et al., 2024), and BEAVER (Chen et al., 2024). Six primitive and six composite categories specify which historical components a new request retains and which edits it permits. For example, a request may preserve a join path but change the calculation over the joined rows. These annotations make it possible to distinguish adapting a complete query from assembling partial evidence. Validation checks both the answer and the intended relationship to history, so an executable query alone does not qualify.

Effective adaptation also depends on what a system chooses to remember (Zhou et al., 2026a; Li et al., 2025c). Agent memory systems organize past interactions to support long-term question answering (Xu et al., 2025b; Liu et al., 2026a; Packer et al., 2023; Park et al., 2023; Chhikara et al., 2025; Maharana et al., 2024; Wu et al., 2025). In Text-to-SQL, prior work reuses successful queries, corrections, and execution traces (Yang et al., 2026b; Biswal et al., 2026). This raises the question of which parts a reliable Text-to-SQL system should retain from a verified query, and how it should decide if retrieved evidence suffices for a new question.

To address this question, we present MEM-SQL (Memory-EMpowered Text-to-SQL). Inspired by hierarchical agent memories (Wang et al., 2026a; Zheng et al., 2024), its three memory layers expose complete query templates, reusable SELECT blocks, and schema concepts. This lets new requests draw on the desired level of historical computations. Searchable descriptions connect new wording to stored SQL structures and column mappings (Koukouvinis et al., 2026; Yang et al., 2026a). Retrieval checks coverage separately for each requirement, and a layer contributes only if every assigned requirement has a match above required threshold. A strong match therefore cannot hide an uncovered requirement. With On-demand Schema Linking (OSL), the system consults schema only when no layer qualifies or generator judges selected evidence insufficient, rather than relying solely on model uncertainty (Deng et al., 2025; Zheng et al., 2026).

Experiments across six language models show that accuracy and efficiency can improve together. MEM-SQL raises mean execution accuracy from 37.1% for Direct Generation to 50.0%, while reducing input tokens and latency. The ablations reveal distinct roles for the memory layers: templates contribute most to accuracy, while query blocks matter especially for composition. Further analysis shows that the gains concentrate on requests that keep historical structure, while composing evidence from several verified queries remains difficult for every method. Applying OSL to three other memory designs (Gao et al., 2023; Xu et al., 2025b; Zhou et al., 2026b) lowers both costs in every comparison, but its accuracy benefit varies with the ability of designs to retrieve complete evidence. This separates a broadly transferable efficiency gain from the harder problem of deciding whether retrieved evidence is sufficient.

Our contributions are as follows.

(i) We introduce HARE to measure the accuracy and cost of reusing verified SQL queries. Source annotations distinguish whole-query adaptation from the reuse of individual components.

(ii) We develop MEM-SQL to retrieve evidence and check coverage at three granularities, supporting both complete-query adaptation and composition from historical components.

(iii) We conduct analysis on OSL transferability to three other memory designs and found that with good memory design, deferring schema consultation improves accuracy and reduces input tokens, latency.

References (76)