natural-language-processing text-to-sql sql-generation schema-linking query-reuse benchmark-hare on-demand-schema-linking verified-queries
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.
한국어 요약
한 줄 요약
MEM-SQL은 기존 SQL 쿼리 히스토리를 활용해 Text-to-SQL 생성 정확도와 효율성을 동시에 향상시키는 시스템이다.
핵심 기여도
- HARE 벤치마크를 제안하여 SQL 히스토리 재사용의 정확도와 비용을 평가.
- MEM-SQL은 3단계 메모리 계층(전체 템플릿, SELECT 블록, 스키마 개념)을 사용해 히스토리 재사용을 지원.
- On-demand Schema Linking(OSL)을 통해 스키마 참조를 최소화, 76.6%의 토큰 감소와 68.8%의 지연 감소 달성.
- 6개 언어 모델에서 평균 실행 정확도가 37.1%에서 50.0%로 향상됨.
핵심 아이디어
기존 Text-to-SQL 시스템은 독립적인 질문을 기준으로 평가되며, 동일 데이터베이스 내 이전 쿼리의 재사용을 고려하지 않았다. MEM-SQL은 이전에 검증된 SQL 쿼리(Verified Queries)를 메모리에 저장하고, 새로운 질문이 이를 부분적으로 또는 전체적으로 재사용할 수 있도록 한다. 이는 전체 쿼리 템플릿, 재사용 가능한 SELECT 블록, 도메인 개념과 스키마 요소 간 매핑을 3단계 메모리 계층으로 구분하여 관리한다. MEM-SQL은 각 계층의 요구사항이 해당 계층의 임계값 이상으로 매칭될 때만 해당 계층을 사용하며, 이는 불완전한 정보가 결과에 영향을 주는 것을 방지한다. OSL 알고리즘은 스키마를 참조하는 시점을 지연시켜, 불필요한 스키마 링킹을 줄인다.
기술적 접근법
- **MEM-SQL 메모리 계층**:
- 전체 쿼리 템플릿 (Whole-query Templates)
- 재사용 가능한 SQL 블록 (Reusable SELECT Blocks)
- 도메인 개념과 스키마 간 매핑 (Schema Concepts)
- **OSL (On-demand Schema Linking)**: 스키마 참조는 메모리 계층이 요구사항을 충족하지 못하거나, 생성기가 검색된 증거가 부족하다고 판단할 때만 수행.
- **HARE 벤치마크**: 4개 데이터셋(Spider, BIRD, Spider 2.0, BEAVER)에서 823개 검증된 쿼리로 1,600개 요청 생성. 6개 단일 범주와 6개 복합 범주로 구성.
주요 결과
- **MEM-SQL vs. Direct Generation**:
- 평균 실행 정확도: 37.1% → 50.0%
- 입력 토큰 감소: 76.6%
- 지연 감소: 68.8%
- **복합 요청 분석**:
- 여러 검증된 쿼리를 재사용하는 요청에서 정확도가 56.7%에서 29.7%로 급락.
- 특히, 재사용된 구성요소가 새로운 쿼리 구조에 맞춰 조합되어야 할 때 가장 낮음.
- **OSL 적용**: 3개 다른 메모리 설계에 적용 시 생성 비용 감소, 정확도 향상은 메모리 설계에 따라 달라짐.
의의 및 한계
MEM-SQL은 기존 Text-to-SQL 시스템이 독립적인 질문만 처리하는 한계를 극복하고, 동일 데이터베이스 내 쿼리 히스토리를 활용해 정확도와 효율성을 동시에 향상시킨다. 특히, OSL을 통해 스키마 참조를 최소화함으로써 대규모 데이터베이스에서도 성능을 개선할 수 있다. 그러나 HARE는 스키마 드리프트(정의 변경)를 고려하지 않으며, MEM-SQL은 정적 메모리 기반으로 동적 환경에서의 성능 변화를 평가하지 못한다는 한계가 있다. 또한, 실행 정확도는 형식 차이(예: 반올림)나 논리 오류를 감지하지 못한다.
실용적 활용
MEM-SQL은 기업 데이터베이스에서 반복적인 쿼리 요청이 많은 상황(예: BI 분석, 고객 지원)에 유용하게 적용될 수 있다. 특히, 기존 쿼리 히스토리를 활용해 비용을 절감하고, 사용자 질문에 대한 응답 정확도를 높이는 데 기여할 수 있다.