Abstract
With the advent of the Big Data era, it is usually computationally expensive to calculate the resource usages of a SQL query with traditional DBMS approaches. Can we estimate the cost of each query more efficiently without any computation in a SQL engine kernel? Can machine learning techniques help to estimate SQL query resource utilization? The answers are yes. We propose a SQL query cost predictor service, which employs machine learning techniques to train models from historical query request logs and rapidly forecasts the CPU and memory resource usages of online queries without any computation in a SQL engine. At Twitter, infrastructure engineers are maintaining a large-scale SQL federation system across on-premises and cloud data centers for serving ad-hoc queries. The proposed service can help to improve query scheduling by relieving the issue of imbalanced online analytical processing (OLAP) workloads in the SQL engine clusters. It can also assist in enabling preemptive scaling. Additionally, the proposed approach uses plain SQL statements for the model training and online prediction, indicating it is both hardware and software-agnostic. The method can be generalized to broader SQL systems and heterogeneous environments. The models can achieve 97.9% accuracy for CPU usage prediction and 97% accuracy for memory usage prediction.
Introduction
Managing a large-scale SQL federation system at Twitter involves processing over 10 petabytes of data daily across heterogeneous on-premises and cloud environments. Traditional database management systems (DBMS) rely on internal cost models to estimate resource usage. However, these models often introduce significant latency—Twitter's P95 planning latency is approximately 9 seconds—and are tightly coupled to specific database kernels. This lack of rapid, engine-agnostic forecasting leads to the "convoy effect," where resource-intensive queries overwhelm clusters, causing workload imbalances and service degradation.
Methodology and System Architecture
To address these challenges, we propose a machine learning-based SQL query cost predictor service that operates independently of the SQL engine. The system treats SQL statements as natural language text, utilizing Term Frequency-Inverse Document Frequency (TF-IDF) vectorization to extract features from raw query strings.
The core of the service is an XGBoost classifier. Rather than predicting exact resource values, which are inherently volatile, the model categorizes queries into discrete buckets (e.g., Low, Medium, and High resource consumption). The architecture includes a training cluster for processing historical request logs and a serving cluster that provides predictions via a REST API in approximately 200ms.
Data Analysis and Key Insights
The models were trained on a production dataset of 1.2 million SQL query logs collected over 90 days. Analysis revealed that the workload is dominated by SELECT statements (~80%) and follows a power-law distribution.
A critical insight is the decoupling of CPU and memory usage; the correlation between these two metrics is only 0.256. This necessitates separate models for each resource type. Furthermore, we found that model accuracy is sensitive to "concept drift," dropping from 98% to 92% within a month if not retrained, emphasizing the need for continuous learning.
Results and Performance
The XGBoost-based approach achieves high precision and recall across all categories. Specifically, the models reached 97.9% accuracy for CPU usage prediction and 97.0% for memory usage. By providing forecasts in milliseconds, the service enables the router to make informed scheduling decisions before queries are executed, facilitating proactive cluster scaling and improved resource utilization.
Conclusion
This work demonstrates that plain SQL text contains sufficient signal for accurate resource forecasting without the overhead of traditional query planning. Future improvements will focus on refining resource buckets and exploring more generalized embedding techniques to enhance cross-platform portability.
Summary updated