首页 \ 问答 \ SQL Server 2008 R2用户定义函数(表值)性能(SQL Server 2008R2 User Defined Function (Table valued) Performance)

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() is

CREATE FUNCTION GetAllProducts
(      
)
RETURNS TABLE 
AS
RETURN 
(
    SELECT * FROM Product
)

and GetCommentsOfProduct(P.ProductId) is

CREATE 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
更新时间:2024-02-01 10:02

最满意答案

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

还可以考虑使用strcmpi来忽略case或strncmp来比较前N元素。


strcmp is the way to go. The == operator is element-wise. If x is not a single character, then the test returns a logical 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 that isequal 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, use isequal.

Consider also using strcmpi to ignore case or strncmp to compare the first N 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选择默认字符集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' ...
  • 我知道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 ...
  • 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 ...
  • 查询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.
  • 如果字体支持,您可以尝试\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 = ᵉ = ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。