简体   繁体   English

SQL Server:为什么CLR函数比CLR存储过程快?

[英]SQL Server: Why CLR Function is faster than CLR Stored Procedure?

I came across the debate of CLR function may be faster than T-SQL scalar function for some people or the other way round for another group of people. 我碰到了一些人认为CLR函数可能比T-SQL标量函数更快的说法,或者对于另一组人来说则相反。

And I also read about comparing CLR function with CLR stored procedure and I learn that CLR function is faster than CLR stored procedure. 而且我还阅读了有关将CLR函数与CLR存储过程进行比较的信息,并且了解到CLR函数比CLR存储过程要快。 Can someone explain to me, in technical, why a CLR function is faster? 有人可以在技术上向我解释为什么CLR功能更快吗?

I'm new to CLR and currently developing a CLR stored procedure in C# to be used in SQL Server. 我是CLR的新手,目前正在C#中开发CLR存储过程以在SQL Server中使用。 The CLR execution basically consume 0 seconds even after many tried. 即使多次尝试,CLR执行也基本上消耗0秒。 I haven't tried implementing into an actual T-SQL stored procedure yet but I need to do that to produce the expected result. 我还没有尝试实现到实际的T-SQL存储过程中,但是我需要这样做才能产生预期的结果。 So I want to understand the execution part and determine which method to use 所以我想了解执行部分并确定使用哪种方法

I don't think that a SQLCLR Scalar User-Defined Function (UDF) is necessarily faster than a SQLCLR Stored Procedure. 我认为SQLCLR标量用户定义函数(UDF) 不一定比SQLCLR存储过程快。 I think it would depend on what the operation is and how it is being used. 我认为这取决于什么是手术以及如何使用。 One advantage that some SQLCLR UDFs have the ability to make use of is, if there is no data access (and sometimes, though not always, if IsDeterministic is set to true ), then they can participate in parallel plans. 某些SQLCLR UDF可以利用的优势之一是,如果没有数据访问权限(有时(虽然并非总是true ),如果IsDeterministic设置为true ),则它们可以参与并行计划。 This is not something that T-SQL UDFs can do. T-SQL UDF无法做到这一点。 So, for operations that can be done in a UDF (not all can be, some require being done in a Stored Procedure), that do not do any data access, and that are being called repetitively in a single query, this can provide a performance improvement over Stored Procedures, which cannot be called in set-based fashion. 因此,对于可以在UDF中完成的操作(并非全部可以完成,某些操作需要在存储过程中完成),不进行任何数据访问并且在单个查询中被重复调用的操作,这可以提供一个存储过程的性能改进,不能以基于集合的方式调用。

For more information on working with SQLCLR in general, please see the series of articles I am writing on the topic on SQL Server Central: Stairway to SQLCLR 有关一般情况下使用SQLCLR的更多信息,请参见我在SQL Server Central: SQLCLR的阶梯上就该主题撰写的系列文章。

PS If you are going to state that you "came across a debate" and then "read about" some topic, you really should provide the links to those discussions and articles / posts, assuming that they are not in private forums or on a corporate intranet. PS:如果您要声明自己“经过辩论”然后“了解”某个主题,那么您真的应该提供这些讨论和文章/帖子的链接,前提是它们不在私人论坛或公司中。内联网。 It really helps to have the context of what is being said, especially to help ensure that you weren't simply misreading / misunderstanding what you have found (and yes, that has happened). 了解所讲内容确实有帮助,尤其是有助于确保您不会只是误读/误解了所发现的内容(是的,确实发生了这种情况)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM