SQL Server 2008 R2用户定义函数(表值)性能(SQL Server 2008R2 User Defined Function (Table valued) Performance)
我正在使用SQL Server 2008R2。
我有以下设置:
-- Query #1 SELECT * FROM Product P INNER JOIN ProductComments C ON C.ProductId = P.ProductId -- Query #2 SELECT * FROM GetAllProducts() P CROSS APPLY GetCommentsOfProduct(P.ProductId) C
GetAllProducts()
在哪里CREATE FUNCTION GetAllProducts ( ) RETURNS TABLE AS RETURN ( SELECT * FROM Product )
和
GetCommentsOfProduct(P.ProductId)
是CREATE FUNCTION [dbo].[GetCommentsOfProduct] ( @ProductId int ) RETURNS TABLE AS RETURN ( SELECT * FROM ProductComments WHERE ProductId = @ProductId )
查询#1和查询#2产生完全相同的实际执行计划并返回相同的结果。 他们的查询成本相对于批次都是50%。 这是否意味着SQL查询优化器将这些查询转换为相同的查询? 为什么调用UDF没有任何开销? 这是否意味着我可以将所有表格构造为UDF,并且即使在执行JOIN操作时也不会有性能问题? 你知道任何缺点吗?
I am using SQL Server 2008R2.
I've got the following setup:
-- Query #1 SELECT * FROM Product P INNER JOIN ProductComments C ON C.ProductId = P.ProductId -- Query #2 SELECT * FROM GetAllProducts() P CROSS APPLY GetCommentsOfProduct(P.ProductId) C
where
GetAllProducts()
isCREATE FUNCTION GetAllProducts ( ) RETURNS TABLE AS RETURN ( SELECT * FROM Product )
and
GetCommentsOfProduct(P.ProductId)
isCREATE FUNCTION [dbo].[GetCommentsOfProduct] ( @ProductId int ) RETURNS TABLE AS RETURN ( SELECT * FROM ProductComments WHERE ProductId = @ProductId )
Both query #1 and query #2 result in the exact same actual execution plan and return the same result. Their query costs are both 50% relative to batch. Does this mean, that the SQL query optimizer translates these queries into the same query? And why is there no overhead for calling the UDFs? And does it mean I can structure all of my tables into UDFs and will have no performance issues even when doing JOIN operations? Do you know of any downsides?
原文:https://stackoverflow.com/questions/8789461
最满意答案
strcmp
是要走的路。==
运算符是元素方面的。 如果x
不是单个字符,则测试返回logical
数组而不是一个:>> x = 'abc'; >> x == 'b' ans = 0 1 0 >> x = 'bbb'; >> x == 'b' ans = 1 1 1
两者都不相等,第二个满足if语句。
还要注意,虽然
==
(eq
)是元素方面的,但是对象相等的isequal
测试。 需要注意的是,isequal
在测试中不考虑数据类型 。 那是:>> isequal('abc',[97 98 99]) ans = 1 >> strcmp('abc',[97 98 99]) ans = 0 >> eq('abc',[97 98 99]) ans = 1 1 1
如果您关心数据类型,请使用
strcmp
,否则请使用isequal
。
strcmp
is the way to go. The==
operator is element-wise. Ifx
is not a single character, then the test returns alogical
array instead of one:>> x = 'abc'; >> x == 'b' ans = 0 1 0 >> x = 'bbb'; >> x == 'b' ans = 1 1 1
Neither are equal, the second one satisfies the if statement.
Also note that while
==
(eq
) is element-wise,isequal
tests for object equality. The caveat is thatisequal
does not consider data type in the test. That is:>> isequal('abc',[97 98 99]) ans = 1 >> strcmp('abc',[97 98 99]) ans = 0 >> eq('abc',[97 98 99]) ans = 1 1 1
If you care about data type, use
strcmp
, if not, useisequal
.Consider also using
strcmpi
to ignore case orstrncmp
to compare the firstN
elements.
相关问答
更多-
您可以使用in -operator来检查字符串中是否存在字符: if char in "aeiou": #Do something You can use the in-operator to check if a char exists in a string: if char in "aeiou": #Do something
-
什么`\ x1b(B`做什么?(What does `\x1b(B` do?)[2021-10-15]
控制序列\x1b(B选择默认字符集ASCII。请参阅XTerm控制序列 : ESC ( C Designate G0 Character Set (ISO 2022, VT100). Final character C for designating 94-character sets. In this list, 0 , A and B apply to VT100 and up, the remainder to VT220 and ... -
这一切都取决于... x = (a) => b(a)与x = b 如果 b是一个只需要1个参数的函数, 那么你错了 - 或者b是一个curried函数 如果 b是一个期望多于一个参数的函数, 那么你是对的 由于Ramda有一个神奇的API,可以让你像一个单独的函数那样进行交互,就好像它是咖喱或者不饱和一样。 // Ramda magic R.propEq('a', 'b', {a: 'b'}) // true R.propEq('a')('b', {a: 'b'}) // true R.propEq('a' ...
-
当`a`和`b`都是'X`类时,`a = b()`是什么意思?(What does `a = b()` mean when both `a` and `b` are of class `X`?)[2023-08-11]
我知道X foo(); 将调用X的构造函数 不,它不会。 它将声明(但不定义)一个名为foo的函数,该函数不带参数,并返回一个X. 而且我很确定X x; 将不会。 是的,如果有的话会的。 通过对您理解的上述更正,您的问题的答案现在应该是明显的,但是: 但是, x = foo();行是什么x = foo(); 意思? 它意味着调用函数foo,并将结果存储在x中。 I know that X foo(); will call X's constructor No, it won't. It will decla ... -
g = filter中的逗号是什么意思(a,b中的lambda x:x)(what does the comma mean in g = filter(lambda x: x in a, b))[2021-11-26]
b是执行过滤器的集合。 filter有两个参数。 第一个是函数(可以是lambda),第二个是应用函数的集合。 该函数将应用于集合中的每个项目。 所以在这种情况下我们有: a = [1,2,3,5,7,9] b = [2,3,5,6,7,8] 和 g = filter(lambda x: x in a, b) ,分隔论点。 第一个参数是lambda函数,所以只是lambda x: x in a ,所以它只返回一个返回True的项集合来过滤集合。 所以对于我们的第一个项目,我们在b有第一个项目,即2. 2 ... -
会员究竟是什么(b,X)?(What exactly is member(b,X)?)[2022-09-14]
查询member(b,X)生成包含b列表。 由于第二个参数未实例化,因此您有(理论上)无数个解决方案。 第一种解决方案将使b处于第一位置,第二种解决方案将使b处于第二位置,依此类推。 此外,如果仔细查看任何解决方案,您会发现它代表了该位置上有b任何列表。 例如,第一个解决方案是[b| _] [b| _] 。 由于未实例化列表尾部(请参阅member/2谓词基本情况),此解决方案将与头部位置中具有b任何列表统一。 如果你想使member/2确定性的,即如果你只想使用谓词来检查一个术语是否是列表的成员,你需要在 ... -
strcmp是要走的路。 ==运算符是元素方面的。 如果x不是单个字符,则测试返回logical数组而不是一个: >> x = 'abc'; >> x == 'b' ans = 0 1 0 >> x = 'bbb'; >> x == 'b' ans = 1 1 1 两者都不相等,第二个满足if语句。 还要注意,虽然== ( eq )是元素方面的,但是对象相等的isequal测试。 需要注意的是, isequal在测试中不考虑数据类型 。 那是: >> i ...
-
除非A等级不足,否则Ax = B与A square的解是唯一的。 因此,没有办法摆脱答案中的负面因素。 如果您假设A和B可能包含导致负面组件的错误但想要找到包含所有非负组件的“附近”解决方案,那么您可以将其转换为: minimize |Ax - b| subject to x >= 0 这是一个二次方案。 有解决此类问题的库,但LAPACK不是其中之一。 编辑 你的矩阵是满级的。 Wolfram Alpha非常适合玩这样的小问题。 你的6x6有一个5x10 ^ 11的行列式,所以它的条件非常好。 我不是 ...
-
我想我找到了答案,这是功能构成: https://en.wikipedia.org/wiki/Function_composition 所以我会给我的功能命名。 I think I have found my answer, it is function composition: https://en.wikipedia.org/wiki/Function_composition So I will give a name compose to my function.
-
需要x上升到b的上标(Need a superscript for x raise to b)[2023-01-29]
如果字体支持,您可以尝试\u1D47 ( \u1D47 )。 请注意,在Unicode中,这并不是真正的“上标b”,而是一个语音符号。 找到适当解决方案的富文本标签库。 a = ᵃ = \u1d43 (modifier letter small a) b = ᵇ = \u1d47 (modifier letter small b) c = ᶜ = \u1d9c (modifier letter small c) d = ᵈ = \u1d48 (modifier letter small d) e = ᵉ = ...