Abstract
Recent developments in large language models (LLMs) trained on large-scale unstructured textual data have produced high-achieving models. However, it remains a challenge to train an LLM on vast structured (tabular) data for the task of understanding the information captured in the data and answering questions regarding the data. We propose a novel method - TQwerE, for approximating SQL aggregated queries’ results over large data sets. Our main focus was to reduce query latency and incurred costs. Moreover, since we focus on large data sets, majority of models that scan raw data are not applicable. Instead, our method fine tunes Jurassic-2 to learn the relations between aggregated SQL queries and their results without referring directly to the underlying raw data. We demonstrate TQwerE’s ability to approximate aggregated queries with state-of-the-art accuracy and speed. We evaluated TQwerE on twelve datasets, and our results demonstrated its superiority to both the state-of-the-art methods.
Introduction
Data scientists often need to perform exploratory data analysis on massive datasets. However, running aggregate queries (like AVG, SUM, or COUNT) on terabytes of data is both time-consuming and expensive. For instance, scanning a 5.3 TB dataset for a single query could take over 11 hours and cost over $80 in cloud compute fees.
Traditional solutions like Approximate Query Processing (AQP) attempt to solve this by using data sampling or specialized neural networks. However, sampling can introduce significant bias, and training custom models from scratch is computationally intensive. TQWERE (Transformer-based SQL Query Executor) addresses these challenges by fine-tuning a Large Language Model (LLM) to predict the results of aggregated SQL queries without ever scanning the underlying raw data at inference time.
Methodology
TQwerE treats query execution as a high-dimensional regression task, leveraging the pre-trained knowledge of the Jurassic-2 LLM. The pipeline follows four main steps:
- Synthetic Query Generation: The system analyzes the dataset's schema and uses bootstrapping to identify attribute boundaries (min, max, percentiles), generating a diverse set of synthetic SQL queries.
- Query Flattening: To simplify the learning task, complex
GROUP BYqueries are decomposed into multiple "flat" queries that return a single scalar value. This allows the model to focus on specific data points within a distribution. - Ground Truth Acquisition: These synthetic queries are run once against the actual database to obtain the "true" labels for training.
- Fine-Tuning: The LLM is fine-tuned on
<query, result>pairs. Because the model is already pre-trained, it only needs a relatively short fine-tuning period to learn the specific data distributions of the target dataset.
Crucially, at inference time, TQwerE does not look at the raw data; it simply "reads" the SQL query and predicts the answer based on the internalized distribution.
Experimental Evaluation
The researchers evaluated TQwerE on twelve different datasets to ensure domain diversity, including business/finance (IBM stock prices, real estate), operations (build success rates, product pass/fail), and customer behavior (churn). These datasets included up to 4 billion rows.
TQwerE was benchmarked against three major methods: a traditional LSTM-based AQP, Google’s TAPAS, and Smart Sampling (SS).
Key Results
- Accuracy: TQwerE outperformed all benchmarks in terms of Weighted MAPE (Mean Absolute Percentage Error) on the majority of the datasets.
- Latency: Query latency was equivalent to the fastest AQP methods and significantly better than TAPAS.
- Efficiency: Fine-tuning a foundation model proved significantly more efficient than training from scratch. TQwerE's longest fine-tuning session took 82 minutes, whereas the LSTM-based AQP required over 21 hours on the same hardware.
Discussion and Future Work
The most striking insight is that an LLM can internalize the statistical distribution of a database, mapping constraints in a WHERE clause to specific points in the data distribution. By removing the need to scan raw data during inference, TQwerE eliminates the primary bottleneck of traditional database engines.
A primary limitation is concept drift, where changes in the underlying data render the model's predictions inaccurate. Future research will focus on a hybrid fine-tuning scenario where the model is periodically updated using a mix of historical and new data to handle evolving datasets robustly.
Summary updated