Abstract
Several data warehouse and database providers have recently introduced extensions to SQL called AI Queries, enabling users to specify functions and conditions in SQL that are evaluated by Large Language Models (LLMs). While powerful, these queries can become prohibitively costly when invoked thousands of times. This paper evaluates an AI query approximation approach that delivers >100x cost and latency reduction for semantic filters ($AI.IF$) and ranking ($AI.RANK$) by utilizing cheap, accurate proxy models over embedding vectors. We show that these proxy models preserve or even improve accuracy across various benchmarks, including a 10M-row Amazon reviews dataset.
The Challenge of AI Queries
Modern data platforms like Google BigQuery and Snowflake now support "AI Queries," allowing users to integrate LLMs directly into SQL for semantic filtering (AI.IF) or ranking (AI.RANK). For instance, a query might filter millions of customer support transcripts for specific sentiment or resolve complex natural language conditions.
However, LLM inference is computationally expensive and slow. Running an LLM on every row of a massive table is economically unfeasible for analytical workloads (OLAP) and too slow for interactive applications (HTAP). Existing optimizations like model cascading or row skipping often fail to reach the scale required for million-row datasets without significant accuracy loss.
Methodology: Lightweight Proxy Models
The proposed solution replaces expensive LLM calls with lightweight "proxy" models—specifically Logistic Regression (LR) trained on-the-fly. The system follows an automated workflow integrated into the database engine:
- Sampling & Labeling: The system selects a small representative subset of data. The LLM (e.g., Gemini) is called only to label this small sample, establishing a "ground truth."
- Embedding Generation: Unstructured text is converted into embedding vectors using models like Gecko.
- Proxy Training: A simple LR model is trained on these embeddings using the LLM-provided labels.
- Adaptive Selection: The system evaluates the proxy's accuracy. If it meets a quality threshold, it processes the remaining rows; otherwise, it falls back to the LLM.
The authors implemented this in two environments: Google BigQuery for online ad-hoc queries and AlloyDB for low-latency HTAP applications using offline pre-trained proxies.
Key Insights
- Accuracy through Regularization: Simple Logistic Regression often outperforms the LLM because it acts as a regularizer, smoothing out noise or inconsistencies in raw LLM outputs.
- Embedding Quality Over Hyperparameters: The choice of embedding model (e.g., Gecko vs. Gemma) significantly impacts performance more than fine-tuning the proxy model itself, allowing for a robust "one-size-fits-all" configuration.
- Handling Imbalance: Weighted training techniques ensure proxies remain accurate even when target classes are rare.
Performance Results
The evaluation across diverse benchmarks (Amazon Reviews, IMDB, TREC-COVID) demonstrates massive efficiency gains without sacrificing quality.
On a 10-million row table, the Online Proxy Model achieved a 329x latency improvement and 728x cost savings. The Offline version, suitable for transactional databases, reached a 991x latency improvement. Crucially, accuracy remained high; for example, on the Amazon Reviews 10k dataset, the proxy improved the F1-score from 0.739 (LLM) to 0.860.
Conclusion and Future Work
Lightweight proxy models offer a transformative path for scaling AI queries in databases. While they struggle with reasoning-heavy tasks like the HellaSwag benchmark, they provide order-of-magnitude improvements for most semantic filtering and ranking tasks. Future work includes integrating more diverse "cheap" models, such as cross-attention re-rankers, to handle complex ranking and semantic join operations.
Summary updated