arXiv:2610.09894cs.DBOct 2026

Manuscript Offprint

arXiv:2610.09894

QCATS: Query Context-Aware Transformer Slicing for Efficient Predictive Query Processing

Yueying Li, Zhongle Xie, Ke Chen, Lidan Shou

Yueying Li 1^{1}, Zhongle Xie 1,2,∗^{1,2,*}, Ke Chen 1,2^{1,2}, Lidan Shou 1,2^{1,2}

1^{1}The State Key Laboratory of Blockchain and Data Security, Zhejiang University;

2^{2}Hangzhou High-Tech Zone (Binjiang) Institute of Blockchain and Data Security

{zjuyueyingli, xiezl, chenk, should}@zju.edu.cn

Abstract—In-database predictive query processing increasingly applies Transformer-based models within relational pipelines. However, existing in-database inference typically exposes only tuple-level model inputs to the inference runtime, leaving relational predicates and metadata statistics invisible to neural execution planning. In this paper, we propose QCATS, a query context-aware transformer slicing framework that enables efficient sparse inference inside database systems. QCATS executes at query granularity: instead of routing individual tokens or tuples during inference, it uses query predicates and metadata statistics to pre-select context-aligned FFN slices before model execution. The framework comprises offline expert construction and lightweight query-level routing that dynamically selects experts during execution. QCATS further introduces system optimizations, including asynchronous CPU-GPU pipelines and routing-aware batching. Experiments on four predictive-query workloads with BERT-base and Qwen-0.6B show that QCATS achieves up to 4.42×4.42\times latency reduction while preserving prediction accuracy comparable to dense baselines.

Index Terms—In-database Learning, Model Slicing, Query Processing

I. INTRODUCTION

Modern analytical databases integrate Transformer-based predictive operators into SQL pipelines to process unstructured text alongside relational data. Known as in-database predictive querying [1], [2], this paradigm supports semantic analytics through cloud inference APIs or UDFs [3]–[5]. These queries typically produce short outputs, such as labels or scores, for many SQL-selected tuples, making accumulated tuple-level inference, rather than autoregressive decoding, a dominant cost of query execution.

Opportunities to reduce this cost arise from the structure of predictive queries: inference operates on subsets selected by SQL predicates. These predicates, together with subset statistics, define a semantic space that often differs from the global data distribution, which we refer to as query context. For Transformer models, different query contexts induce distinct activation patterns in feed-forward networks (FFNs) [6]. As illustrated in Fig. 1, sentiment analysis over food reviews activates a different subset of neurons from the same task on clothing or electronics reviews, suggesting that only a fraction of model capacity is required for many query workloads. We further empirically verify this phenomenon in Fig. 2 using eight queries, details are provided in the appendix. For each

Figure

Fig. 1: FFN Activation Varies Across Query Contexts.

Figure

Fig. 2: Average FFN neuron activations for text tuples selected by eight queries (Q1–Q8).

query, we feed its selected text tuples into the fine-tuned BERT model and compute the average activation of each FFN neuron, producing one row of the heatmap. Each column represents an FFN neuron, and darker colors indicate higher average activation. A small subset of neurons remains highly activated across the queries and is therefore placed on the left. Beyond this shared subset, highly activated regions appear at different neuron positions for different query contexts, indicating that distinct queries activate different subsets of neurons. This observation motivates selecting context-aligned FFN slices according to the query context.

However, existing database systems typically execute inference through black-box user-defined functions (UDFs) [3]–[5], transmitting only raw text data to the model while discarding relational constraints and metadata statistics. Even when the inference engine applies generic batching or kernel-level acceleration, these optimizations are unaware of the relational context that determines which tuples are semantically

  • Corresponding author.

similar. Consequently, models operate in a context-agnostic manner and execute dense inference for every input tuple. This mismatch leads to substantial waste in computation and memory resources in predictive query processing. That is, the computation repeatedly activates the full model despite the query-level locality of predictive workloads.

From a systems perspective, the inefficiency motivates conditional execution, where only computation relevant to the current query context is activated during inference. Analogous to predicate pushdown [7]–[9] and column pruning [10], [11], which avoid irrelevant data movement and attribute access, query-aware neural execution should avoid irrelevant parameter access and activation computation. Unfortunately, existing neural inference pipelines lack mechanisms to translate query semantics into deterministic and database-friendly conditional model execution.

Sparse activation techniques like Mixture-of-Experts (MoE) [12], [13] partially address this issue by enabling conditional computation within neural networks. However, they rely on token-level, layer-wise routing during inference, which introduces control-flow divergence and dynamic data redistribution overheads, degrading GPU utilization [14], [15].

In database workloads, this overhead is amplified because tuples selected by the same query are naturally processed in large batches; token-level routing breaks this homogeneity and prevents the executor from constructing a single static inference path for the batch. Furthermore, it introduces additional buffering overhead for fragmented tokens across experts, causing inflated activation memory and unpredictable resource demands.

Despite extensive research on sparse inference and expert routing, existing approaches [16]–[18] are primarily designed for standalone model serving and do not exploit the semantic structure exposed by database queries. To our knowledge, no prior system leverages query-level context to enable deterministic, database-integrated sparse execution of Transformer models. This gap prevents current solutions from achieving both high inference efficiency and tight integration with relational execution pipelines.

Beyond model-level limitations, predictive query processing also introduces system-level challenges. CPU-side preprocessing, including tokenization and data preparation, can become a bottleneck when GPU inference is accelerated on-the-fly. Meanwhile, long-tailed text length distributions [19] cause excessive padding overhead under conventional batching, while synchronous execution pipelines provide insufficient CPU–GPU overlap, leading to pipeline stalls and underutilized hardware resources.

In this work, we propose Query Context-Aware Transformer Slicing (QCATS), a framework that rethinks sparse inference for Transformer-based models from a database-first perspective. Instead of performing routing inside the model, QCATS shifts routing decisions to the query level. By leveraging the query context, QCATS pre-determines the subset of model parameters that will be activated before inference begins. This design enables static, deterministic routing, avoids runtime control-flow divergence, and aligns sparse execution with the granularity of database workloads. In this sense, QCATS treats model inference as a query physical operator whose execution path can be specialized using optimizer-visible relational context before tuple-level inference begins.

QCATS consists of two tightly integrated components. First, it transforms a fine-tuned dense model into a library of context-aligned expert slices by exploiting FFN activation sparsity, enabling sparse inference without retraining models from scratch. Second, it introduces a lightweight context-aware router that maps query contexts directly to expert combinations. By decoupling routing from execution, QCATS ensures contiguous memory access via homogeneous batching, thereby avoiding the extra buffering overhead and reducing peak activation memory. To further address system-level bottlenecks, we design an asynchronous CPU–GPU execution pipeline with hierarchical bucketing that masks preprocessing overhead and reduces padding-induced waste. We summarize our main contributions as follows:

• We formulate query context-aware transformer slicing for in-database predictive query processing and propose QCATS, which uses query predicates and metadata statistics to specialize neural inference execution paths.

• We introduce a parameter decomposition strategy that partitions FFN layers into shared and semantic components, enabling efficient sparse inference from a fine-tuned dense Transformer without constructing separate expert networks from scratch.

• We design a lightweight query-level routing mechanism that maps query contexts to sparse expert combinations, shifting conditional computation from token-level dynamic routing to query-level static routing.

• We integrate QCATS into a database-oriented execution pipeline with routing-aware batching and asynchronous CPU–GPU execution. Experiments on four workloads and two Transformer backbones show significant latency and memory reductions while preserving prediction accuracy.

The rest of the paper is organized as follows. §II defines predictive queries and formulates query context-aware transformer slicing. §III presents QCATS and §IV introduces database-oriented execution optimizations. §V reports the experimental evaluation. §VI reviews related work, and §VII concludes the paper.