MEM-SQL: Memory-Empowered Text-to-SQL: what it means for business leaders
MEM-SQL answers new database questions by reusing verified past queries instead of re-reading the schema each time, lifting accuracy from 37.1% to 50.0% while cutting input tokens by about three quarters.
1. What the method is
MEM-SQL is a text-to-SQL system that treats a database's verified query history as its main source of evidence. It stores that history at three levels: whole-query templates with the literal values masked out, reusable SELECT blocks that keep their joins and clause structure, and schema concepts that map business terms such as lap time to specific columns and join relationships. A new question is answered from this memory whenever the memory covers everything the question needs. The schema is consulted only when coverage fails, a policy the authors call On-demand Schema Linking. The paper also introduces HARE, a benchmark of 1,600 requests derived from 823 verified queries, built to measure how accurately and how cheaply a system adapts historical SQL. Each request is labelled with what it keeps from history and what it changes.
2. Why the method was developed
Standard text-to-SQL benchmarks such as Spider and BIRD score every question in isolation. Real analytics work looks different: users query the same database repeatedly, and a new request often changes only a date range, a threshold, a metric or a grouping on a query someone has already verified. Systems that ignore this rebuild the same joins and calculations from the raw schema every time, which is slow and expensive on enterprise schemas that can exceed 1,000 columns. Existing memory approaches retrieve past queries as few-shot examples or fragments, but they still link the schema on every request, and they have no check on whether what they retrieved actually covers the new question. A reused query can execute cleanly while its logic no longer matches the request, so the open questions were what to retain from history and when it is sufficient.
3. Who should care
Data and analytics leaders running natural-language query tools over our warehouse should care most, because the method converts the query logs and vetted reports we already have into an asset that makes generation cheaper and more accurate. Platform and ML engineering teams building or buying such tools get a concrete design: a layered memory with a coverage check, and a schema-on-demand policy that the paper shows can be bolted onto other memory designs. Finance and whoever owns model spend should note the token reduction, which holds across all six models tested, including small open-weight ones we could host ourselves. Business users of self-service analytics would see faster answers on routine variations of known reports. Data governance teams have a stake too, since the approach depends on a curated pool of verified queries and on someone keeping it current.
4. How the method works
Offline, each verified query is broken into memory items, each pairing a searchable description and its embedding with an exact SQL skeleton or schema mapping. Near-duplicates are merged only when descriptions are similar and the stored SQL is equivalent. At query time a decomposer splits the question into retrieval atoms, one per requirement and assigned to a layer, and each atom retrieves its own nearest items. A layer contributes evidence only if every atom assigned to it has a match above that layer's threshold, so one strong match cannot hide a missing requirement. The generator writes SQL from the accepted layers. Schema linking runs only if no layer passes the gate or the generator declares the evidence insufficient. The result is checked with a database dry run and repaired if it fails. On HARE, 95.4% of requests were completed from memory alone.
5. How it was evaluated
The evaluation uses HARE's 1,600 requests, drawn from Spider, BIRD, Spider 2.0 and BEAVER, across six single reuse categories (rephrase, parametric change, component parametric change, structural change, shared join, shared concept) and six two-way combinations. Requests were generated by an LLM under category rules and validated by execution and by a second model, so the workload is synthetic rather than logged from real users. Six language models served as backbones: two closed-weight and four open-weight, down to 8B parameters. Baselines were direct generation without history, DAIL-SQL demonstration retrieval, the general-purpose agent memory A-MEM, and the BIRD-History retriever, all of which link the schema on every request. Metrics were execution accuracy against gold results, mean input tokens and end-to-end latency per request. Memory was built once from the verified pool and held fixed. Ablations removed each layer and the atom-based retrieval.
6. How it performed
Averaged over six models, execution accuracy rose from 37.1% for direct generation to 50.0%, with 76.6% fewer input tokens and 68.8% lower latency, and MEM-SQL led every memory baseline on every backbone. Gains concentrate on rephrases and parametric changes (88.2% and 77.7% accuracy) and on the join-heavy BEAVER enterprise set, 8.6 points over the strongest baseline. It slightly trails that baseline on Spider and Spider 2.0. Structural changes reach only 14.8%, and requests combining several past queries fall from 56.7% to 29.7%. Working without the schema also yields more invalid SQL, 10.8% of memory-route errors against 3.2% for A-MEM. The authors note that memory was static, schema drift was not modelled, and execution accuracy cannot detect right rows for the wrong reason. Our call: pilot it on a database where we hold a large stock of verified queries and requests are mostly variations, with a human check on results, since half the answers are still wrong. (Source: arXiv 2611.18338, 2026)
← Back to dossier index