DB2 for i:非 UDF 宏函数? (User Defined Functions速度慢,但复杂的公式很复杂)

DB2 for i: non-UDF macro functions? (User Defined Functions are slow, but complicated formulas are complicated)

我需要执行复杂但相当一致的公式。

当在大表上完成时,UDF 非常慢,所以我宁愿避免它们(在 SELECT 子句中使用 UDF 的执行时间比等效的内联语句慢 30-100 倍, 在更大的桌子上)

我可以改为编写内联公式,但生成的代码庞大、不可读且更难维护。

有没有办法设计像 C 或 SAS 宏这样的东西,它只在查询中直接进行文本替换?

我想要如下内容

给定:

isKeyValid(key, param1, param2)

生成的代码:(这只是我随意写的废话 - 它应该支持任何数量的原始 SQL)

    case 
    when param1 in ('a','b','c') and substr(param2,1,2) = '53' and key=param1||param2 then 1
    when param1 in ('a','d','e') and substr(param2,1,2) = '51' and key=param1||param2 then 1
    else 0 
    end

我从未在 i...

上看到 UDF 有任何问题

您使用的是什么版本?假设当前版本(v7.2 或更高版本)查看 UDF 内联的增强功能 added in 2016,以确保您的代码可以内联。

Inlining support for User Defined Table Functions (UDTFs)
Improved Inlining support for SQL Scalar User Defined Functions (UDFs)

Inline functions: When an SQL scalar function is inlined, instead of invoking the function as part of a query, the expression in the RETURN statement of the function may be copied (inlined) into the query itself. Such a function is called an inline function. A scalar function is an inline function if:

  • The SQL function is global deterministic.
  • The SQL-routine-body contains only a RETURN statement.
  • No input parameter is an array type.
  • The data type of the result is not XML or an array type.
  • All objects referenced in the function exist when the function is created.
  • The SQL-routine-body does not contain a common table expression that references an input parameter.
  • The SQL-routine-body does not contain a nested table expression without a preceding LATERAL keyword that references an input parameter.

An inline function is only copied (inlined) into a query if:

  • The query is eligible for the SQL Query Engine (SQE).
  • The function references an object and the authority attributes of the function and the query are compatible based on one of the following conditions:
    • The function is defined to run under the user's authority (*USER).
    • The query is running under the owner's authority (*OWNER) and the owner of the query is the same as the owner of the function.
    • The query is running under the user's authority (*USER), and the user or the user's group profile is the same as the owner of the function.

Note: If the function is defined as FENCED, the query must not use adopted authority. If the query runs under the owner's authority (*OWNER) and the function runs under the user’s authority (*USER), the owner of the query must be the same as the user or the user's group profile.

特别注意函数只包含一个 RETURN 的突出要求。如果您有使用 BEGINEND 的复合语句;你的函数不会被内联。