【发布时间】:2023-04-02 11:13:01
【问题描述】:
我有一个当前使用标量函数的 proc,在 select 语句中两次,如下所述。当我们处理数百万条记录时,用内联函数替换它是否更好的性能。如果是这样应该是什么
CREATE FUNCTION getcategory
(
@shopping_store CHAR(4),
@cart_type CHAR(2),
@category_type INT,
@index_cat_type INT
)
RETURNS INT
BEGIN
IF @shopping_store IN ('1111','1222','3222') AND @cart_type in ('120')
RETURN -@category_type
ELSE IF @shopping_store IN ('4333','54322') AND @cart_type IN ('120')
RETURN @index_cat_type
ELSE
BEGIN
IF @shopping_store IN ('32214','5432','5654')
RETURN @category_type
ELSE
RETURN -@index_cat_type
END
RETURN @category_type
END
【问题讨论】:
-
内联函数很可能更好。你试过什么吗?将其更改为内联函数非常简单
-
关于性能方面,将其更改为内联函数几乎肯定会有所帮助,但也会改变您调用它的方式。阅读this article 了解更多详情。顺便说一句,您将参数
@cart_type声明为CHAR(2),然后检查@cart_type in ('120'),这将永远为真,因为@cart_type只有两个字符。我怀疑这不是预期的行为。
标签: sql-server query-optimization user-defined-functions