这个问题源于SQLServer: Why avoid Table-Valued User Defined Functions?。我开始在一些评论中提问,而对我的评论的回复则偏离主题。
因此,您不必阅读整个讨论:我从未听说它说用户定义的函数(UDF)很慢,或者要避免。在上面引用的问题中发布了一些链接,以说明它们很慢。我仍然没有得到它,并要求一个例子。发布了一个示例,性能差异很大。
我不可能是唯一一个没有意识到可能存在如此大的性能差异的人。我觉得这个事实应该分成一个新的问题和答案,以提高被发现的机会。这是“问题”。请不要关闭,因为我想让回答者有时间发布答案。
当然,其他人也应该发布答案或例子。我特别感谢任何有助于我理解为什么性能差异如此之大的内容。
另请注意,我不是在谈论在WHERE子句中使用UDF。我知道这可以阻止优化器完成它的工作。当原始UDF是SELECT列列表的一部分时,我对性能差异特别感兴趣。
答案 0 :(得分:5)
对于基准测试,让我们创建一个包含1M行的表:
CREATE TABLE dbo.Numbers(n INT NOT NULL PRIMARY KEY)
GO
DECLARE @i INT;
SET @i = 1;
INSERT INTO dbo.Numbers(n) SELECT 1;
WHILE @i<1024000 BEGIN
INSERT INTO dbo.Numbers(n)
SELECT n + @i FROM dbo.Numbers;
SET @i = @i * 2;
END;
GO
运行简单的内联添加:
SELECT COUNT(*) FROM(
SELECT n,n+1 AS ValuePlusOne
FROM dbo.Numbers
) AS t WHERE ValuePlusOne>0
CPU time = 15 ms, elapsed time = 122 ms.
(1 row(s) affected)
Table 'Numbers'. Scan count 1, logical reads 3521, physical reads 3, read-ahead reads 3498, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 406 ms, elapsed time = 951 ms.
创建一个标量UDF,只需向整数添加一个,并运行1M次:
CREATE FUNCTION dbo.[AddOne]
(
@value int
)
RETURNS int
AS
BEGIN
DECLARE @Result int
SELECT @Result = @value + 1
RETURN @Result
END
GO
SELECT COUNT(*) FROM(
SELECT n,dbo.AddOne(n) AS ValuePlusOne
FROM dbo.Numbers
) AS t WHERE ValuePlusOne>0
CPU time = 15 ms, elapsed time = 122 ms.
(1 row(s) affected)
Table 'Numbers'. Scan count 1, logical reads 3521, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 108313 ms, elapsed time = 295072 ms.
创建内联UDF,其速度与添加速度一样快,并运行1M次:
CREATE FUNCTION dbo.[AddOneInline]
(
@value int
)
RETURNS TABLE
AS
RETURN(SELECT @value + 1 AS ValuePlusOne)
GO
SELECT COUNT(*) FROM(
SELECT ValuePlusOne
FROM dbo.Numbers
CROSS APPLY dbo.[AddOneInline](n)
) AS t WHERE ValuePlusOne>0
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 35 ms.
(1 row(s) affected)
Table 'Numbers'. Scan count 1, logical reads 3521, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 391 ms, elapsed time = 403 ms.
标量UDF与内联UDF的性能差异是显而易见的。
答案 1 :(得分:2)
好吧,既然你开了一个棘手的主题:-)我认为我们需要更现实的例子并且不要玩游戏。看起来太做作的例子总让我怀疑。所以,我稍微重新排列了查询,直接标量UDF优于查询。不要相信它 - 尝试一下 - 这是在2k8 Server Std下的开发盒上的SQL 2k8上。
到目前为止我们所学到的是在WHERE子句中使用计算列和等价物是不好的。该查询在WHERE子句中使用标量函数,同时假装它在select中。
SELECT COUNT(*) FROM(
SELECT n as X,n+1 AS ValuePlusOne
FROM dbo.Numbers
) AS t WHERE X>0
表'数字'。扫描计数1,逻辑读取3521,物理读取0,预读读取0,lob逻辑读取0,lob物理读取0,lob预读读取0。
SQL Server执行时间: CPU时间= 234 ms ,已用时间= 228 ms。
SELECT COUNT(*) FROM(
SELECT n as X ,dbo.AddOne(n) AS ValuePlusOne
FROM dbo.Numbers
) AS t WHERE X>0
表'数字'。扫描计数1,逻辑读取3521,物理读取0,预读读取0,lob逻辑读取0,lob物理读取0,lob预读读取0。
SQL Server执行时间: CPU时间= 202 ms ,经过时间= 215 ms。
那么,既然我们已经解决了一些真实信息和现实用例的问题呢?
我将提供2个辩论:-)但请记住没有人为的陷阱。 TVF和标量UDF只是调用它以方便的方式获取值,然后在查询中用作值或连接 - 没有人计算任何东西。有人可以构建一个表或说明病理数据必须如何才能看到LCID1和LCID2之间的perf差异吗?
CREATE FUNCTION [PublishingCulture] ( @XLanguage int,
@XLocale int
) RETURNS TABLE
AS
RETURN
(
select TOP 1 * from [Culture] C
where ((C.XLang = @XLanguage and C.XLoc = @XLocale)
or (C.XLang = @XLanguage and C.XLoc = 0)
or (C.XLang = 0 and C.XLoc = @XLocale)
or (C.XLang = 0 and C.XLoc = 0))
)
CREATE FUNCTION [MyLCID1] ( @XLanguage int,
@XLocale int )
RETURNS TABLE
AS
RETURN ( SELECT LCID from dbo.PublishingCulture(@XLanguage, @XLocale) )
CREATE FUNCTION [MyLCID2] ( @XLanguage int,
@XLocale int )
RETURNS int
AS
BEGIN
RETURN ( SELECT LCID from dbo.PublishingCulture(@XLanguage, @XLocale) )
END
select * from
(select Row_number() OVER(order by StartDate) as RN, Message
from [Ticker] as T
join dbo.MyLCID1(@XLanguage, @XLocale) as L on T.LCID = L.LCID
where
Getutcdate() BETWEEN StartDate AND EndDate
) AS T
where RN BETWEEN @StartIndex AND (@StartIndex + @MaxItems -1)
select * from
(select Row_number() OVER(order by StartDate) as RN, Message
from [Ticker] as T
where
LCID = dbo.PubLCID1(@XLanguage, @XLocale) AND
Getutcdate() BETWEEN StartDate AND EndDate
) AS T
where RN BETWEEN @StartIndex AND (@StartIndex + @MaxItems -1)
[文化]在XLang上有PK,Xloc,[Ticker]在LCID上有PK,Id(Id是人工)和StartDare,EndDate,LCID上的IX - 接近真实的东西,可以在几行中获得