更新时间:2022-11-13 12:34:51
标量函数和多语句表值用户定义函数会导致性能问题,因为它们隐式地将基于集合的操作转换为基于游标的操作.
Scalar functions and multi statement table valued user defined functions can cause performance issues, because they implicitly turn your set based operation into a cursor based operation.
然而,内联表值的用户定义函数不会遇到这个问题.他们很快.
However, inline table valued user defined functions do not suffer from this problem. They're fast.
区别在于您如何声明函数,以及它们内部的代码是什么样的.多语句函数执行它在罐头上所说的 - 它允许您有多个语句.像这样:
The difference is how you declare the fuction, and what the code looks like inside them. A multi statement function does what it says on the tin - it lets you have multiple statements. Like this:
create function slow() returns @t table(j int, k int) as
begin
declare @j int = 1; -- statement 1
declare @k int = 2; -- statement 2
insert @t values (@j, @k); -- statement 3
return; -- statement 4
end
内联表值函数不会返回填充在函数内部的命名表.它返回一个选择语句:
An inline table valued function does not return a named table which is populated inside the function. It returns a select statement:
create function quick() returns table as
return
(
select j = 1, k = 2
);
内联表值函数可以内联";进入外部 select 语句,与视图的方式大致相同.当然,区别在于 UDF 可以接受参数,而视图不能.
The inline table valued function can be "inlined" into the outer select statement, in much the same way as a view. The difference, of course, being that the UDF can take parameters, whereas a view cannot.
您还必须以不同的方式使用它们.使用交叉应用:
You also have to use them differently. Use cross apply:
select t.columnA, t.columnB, u.j, u.k
from MyTable t
cross apply quick(t.columnA, t.columnB) u
如果不清楚 - 是的,在您的情况下,您只需要一个标量"返回值,但这只是一个表值函数,它返回单列和单行.因此,与其编写标量函数,不如编写一个执行相同工作的内联表值函数,然后交叉应用
它.
In case it's not clear - yes, in your case you only want a "scalar" value back, but that's just a table valued function which returns a single column and a single row. So instead of writing a scalar function, write an inline table valued function that does the same job, and cross apply
it.