Powering In-Database Dynamic Model Slicing for Structured Data Analytics

Lingze Zeng, Naili Xing, Shaofeng Cai, Gang Chen, Beng Chin Ooi, Jian Pei, Yuncheng Wu

TL;DR

Training separate deep learning models for every possible data slice in a database is combinatorially impossible, while a single global model fails to capture niche subset behaviors. LEADS (SQL-awarE dynAmic moDel Slicing) treats SQL WHERE clauses as conditioning metadata: it uses a SQL-aware gating network over a Mixture of Experts (MoE) to dynamically activate only the sub-models relevant to a query. Integrated directly into PostgreSQL, it boosts worst-case prediction accuracy (Worst-AUC) by up to 55.76% while cutting inference latency by up to 2.06× compared to decoupled database-ML pipelines.

What Problem Does This Paper Address?

In enterprise analytics, analysts rarely ask models to predict across an entire dataset; they evaluate specific subsets (e.g., patient readmission rates under a specific drug regimen, or default risk for specific regional demographics) retrieved via SQL WHERE clauses.

This introduces two distinct bottlenecks:

  1. The Model Capacity Dilemma: Training one monolithic model washes out patterns unique to minority sub-populations. Conversely, training a custom model per subdataset is computationally impossible due to combinatorial explosion.
  2. The Systems Mismatch: Traditional machine learning workflows decouple the database from inference engines. Exporting query results to external frameworks (like PyTorch) introduces heavy I/O latency, network serialization overhead (accounting for ~40% of runtime), and data security risks.

Methods

LEADS bridges relational database querying and deep learning inference via two core contributions:

[SQL Query: WHERE Age=24 AND City='NYC']
             │
             ▼
   [Discretization & Encoding] ──► Query Vector q
                                         │
                                         ▼
                               [Gating Network + α-entmax]
                                         │ (Sparse Weights)
                                         ▼
 [Data Tuples x] ──► [General MoE Model: Experts 1...K] ──► [Dynamically Sliced Model]
  • SQL Query Encoder: LEADS converts primitive conjunctive SQL queries into dense feature vectors. Numerical predicates are discretized using supervised binning (OptBinning) to maximize Information Value (IV), while categorical conditions and unspecified attributes receive learned or default embeddings.
  • SQL-Aware Dynamic Model Slicing: Instead of a single model, LEADS builds a general model consisting of $K$ base-model replicas (experts). A 2-layer MLP gating network processes the encoded SQL query vector and outputs sparse routing weights via $\alpha$-entmax. Experts assigned zero weight are pruned at inference, yielding an on-the-fly, specialized "sliced model."
  • Dual-Regularization Objective: Training MoE models often leads to representation collapse (where a few experts handle everything). LEADS balances this with:
  • Balance loss ($\mathcal{L}_{baln}$): Promotes even expert utilization across mini-batches.
  • Sparsity loss ($\mathcal{L}_{sprs}$): Counteracts over-activation to keep active expert count ($n_o$) small.
  • In-Database Inference Extension: Implemented as a PostgreSQL UDF (SELECT infer(...)) with three systems optimizations:
  • Multi-language allocation: Rust (via PGRX and PostgreSQL's Server Programming Interface) handles fast data retrieval; Python handles tensor inference.
  • Shared Memory: Zero-copy memory buffer between Rust and Python environments eliminates intra-UDF serialization.
  • State Caching: Session-level caching for the general model plus an LRU cache for frequently derived sliced models.

Datasets

Evaluations used five real-world tabular datasets across finance, sociology, and advertising:

  • Payment (30k rows, default prediction)
  • Credit (244k rows, Home Credit default risk)
  • Census (269k rows, annual income >$50k classification)
  • Diabetes (102k rows, 10-year hospital clinical readmission)
  • Avazu (40.4M rows, mobile ad click-through rate prediction)

Because standard OLAP benchmarks (e.g., TPC-H/TPC-DS) lack prediction targets and standard ML tabular datasets lack SQL queries, the authors introduced an algorithm to generate synthetic SQL query workloads by sampling tuples and creating conjunctive filter predicates of variable length ($1 \le m \le 3$).

Key Insights and Ideas

  • SQL Filters as Semantic Priors: Filter conditions are usually discarded once data tuples are selected. LEADS recognizes that the query predicate itself is a strong prior describing the latent distribution of the target subset.
  • Adaptive Sparsity via Learnable $\alpha$-entmax: Standard softmax forces dense weights (activating all $K$ experts and inflating FLOPs). A learnable $\alpha$-entmax lets the network decide how many experts are truly needed per query, preserving computational efficiency.
  • Rescuing the "Long Tail" of Queries: The biggest gains appear on rare slices (e.g., queries matching $<0.5\%$ of training data). Monolithic models fail here due to data sparsity, but conditioned expert routing borrows relevant parameters without overfitting.

Key Results

  • Predictive Accuracy: Evaluated across four base architectures (DNN, CIN, AFN, and ARMNet), LEADS improved average Workload-AUC by up to 3.95%.
  • Robustness on Edge Cases: On the Worst-AUC metric (the single lowest-performing query in a workload), LEADS achieved dramatic improvements: +55.76% on Credit, +41.77% on Payment, and +16.00% on Diabetes.
  • System Latency: The PostgreSQL in-database extension delivered a 1.53× to 2.06× end-to-end speedup over decoupled external inference. Enabling all three UDF optimizations (SPI, shared memory, and caching) accelerated execution by 3× relative to an unoptimized UDF.

Limitations and Future Work

  • Predicate Formats: The query encoder strictly targets primitive conjunctive queries (AND predicates on equality/bins). Arbitrary boolean logic (OR, nested clauses, or range scans without binning) is not directly handled.
  • Schema Mutations: While resilient to tuple insertions, schema deletions (dropped columns) cause sharp AUC degradation, necessitating offline model retraining.
  • Complex JOIN Overheads: For Select-Project-Join (SPJ) queries, internal relational join execution times dominate latency, diminishing the relative speedup gained from zero-copy memory optimizations.
  • Future Integration: Integrating LEADS into autonomous database engines (NeurDB) alongside native model-selection frameworks (e.g., TRAILS).

Summary updated

BibTeX
@article{zeng2024powering,
  author = {Lingze Zeng and Naili Xing and Shaofeng Cai and Gang Chen and Beng Chin Ooi and Jian Pei and Yuncheng Wu},
  title = {{Powering In-Database Dynamic Model Slicing for Structured Data Analytics}},
  journal = {Proc. VLDB Endow.},
  year = {2024},
  url = {https://openreview.net/forum?id=9kDGldXNdU},
}
Figure