Abstract
User-Defined Functions (UDFs) are essential for modular database development but often cripple performance by forcing row-by-row execution. This paper evaluates two primary optimization strategies: inlining and batching. By translating procedural UDFs into pure relational SQL, the authors demonstrate that while inlining is theoretically elegant, batching often yields better performance on legacy optimizers due to the generation of simpler, more easily decorrelated subqueries. We propose a hybrid strategy that selects the optimal transformation based on the target DBMS's optimizer capabilities.
The UDF Performance Gap
Developers favor PL/SQL UDFs for their ability to encapsulate complex procedural logic, such as conditional branching and variable assignments, directly within the database. However, these functions are a notorious performance bottleneck. Because standard query optimizers treat UDFs as opaque "black boxes," they cannot integrate them into set-oriented execution plans. This leads to "row-by-agonizing-row" (RBAR) execution, where the database engine must context-switch between the SQL executor and the procedural runtime for every single row in a result set.
To address this, two main techniques have emerged:
- Inlining: Translating the entire UDF into a single, massive correlated subquery that is injected into the calling query.
- Batching: Executing the UDF’s logic on all input rows simultaneously by maintaining a global state table.
Methods: Syntax-Directed Translation
The authors developed a framework to convert PL/SQL UDFs into pure SQL:1999 using LATERAL joins. This approach allows for a direct comparison of optimization techniques without requiring internal modifications to the database engine.
- Inlining Translation: As shown in the formal rules above, procedural constructs like
IF/ELSEare mapped to SQLCASE/WHENexpressions. Sequential statements are chained usingLATERALjoins to pass state forward. While this allows for full pipelining, it often results in deeply nested, complex queries. - Batching Translation: This method creates a temporary
statetable to hold variables for all input rows. Each procedural statement is converted into a set-orientedUPDATEon this table. While this incurs the overhead of materializing intermediate state, it produces significantly simpler subqueries for the optimizer to handle.
The Decorrelation Bottleneck
The fundamental insight of this research is that the success of any UDF optimization depends on decorrelation. For a query to run efficiently, the DBMS must transform nested subqueries into joins.
Inlining generates complex relational trees that often exceed the pattern-matching capabilities of legacy heuristic optimizers (like those in SQL Server or Oracle). In contrast, batching produces simpler queries—typically one SELECT per UPDATE—which are much easier for these systems to decorrelate. Conversely, in systems with advanced algebraic decorrelation (like DuckDB), the complexity of inlining is not a barrier, and its lack of materialization overhead makes it the clear winner.
Experimental Results
The framework was evaluated using ProcBench, a workload of 13 loop-free UDFs containing embedded SQL. The results highlight how the "best" technique is entirely dependent on the specific database engine's optimizer.
- SQL Server: Batching achieved a 9.28x geometric mean speedup, significantly outperforming inlining’s 2.04x. A hybrid approach further improved this to 10.46x.
- Oracle: Batching provided a 1.82x speedup, while inlining offered no improvement (1.00x) because the optimizer failed to decorrelate the complex inlined queries.
- DuckDB: Utilizing state-of-the-art Neumann-style decorrelation, DuckDB achieved a massive 191x geometric mean speedup with inlining, rendering batching unnecessary.
- PostgreSQL: Both techniques struggled due to the optimizer's limited decorrelation logic, yielding marginal gains (Batching: 1.85x, Inlining: 1.33x).
Conclusion and Future Work
This study demonstrates that inlining is not a universal solution for UDF optimization. For many enterprise DBMSs, the simpler queries produced by batching are more effective. The authors suggest that future work should focus on generating LATERAL-free translations for broader compatibility and extending these techniques to modern UDF runtimes like Python or WebAssembly. By dynamically choosing between inlining and batching, database systems can finally bridge the performance gap between procedural modularity and declarative efficiency.
Summary updated