简体   繁体   English

我应该在SQL Server还是C#中使用“用户定义函数”?

[英]Should I use “User Defined Functions” in SQL server, or C#?

I have a fairly complicated mathematical function that I've been advised should be implemented as a User Defined Function in SQL Server so that it can be used efficiently from within a SQL query. 我有一个相当复杂的数学函数,建议我在SQL Server中将其实现为用户定义函数,以便可以在SQL查询中有效地使用它。

The problem is that it must be very efficient as it may be executed thousands of times per second, and I subsequently heard that UDFs are very inefficient. 问题在于它必须非常高效,因为它每秒可能执行数千次,并且随后我听说UDF效率很低。

Someone suggested that I could implement the function in C# instead, and that this would be much more efficient. 有人建议我可以改用C#实现该功能,这样会更加高效。

What should I do? 我该怎么办?

Complex mathematical functions will execute much more quickly in C# than T-SQL. 在C#中,复杂的数学函数比T-SQL更快地执行。 It's well worth trying. 值得尝试。

Edit In answer to your comment, I found this blog post , which states: 编辑为了回答您的评论,我发现了此博客文章 ,其中指出:

User-defined scalar functions are likely the most obvious candidates for SQLCLR usage. 用户定义的标量函数可能是使用SQLCLR的最明显候选者。 There are two primary reasons for this. 这有两个主要原因。 The first is that CLR functions actually have lower invocation overhead than T-SQL functions. 首先是CLR函数实际上比T-SQL函数具有更低的调用开销。 T-SQL functions require the runtime to create a new T-SQL frame, which is expensive. T-SQL函数需要运行时才能创建新的T-SQL框架,这很昂贵。 CLR functions are embedded in the plan as a function pointer for direct execution. CLR函数作为直接执行的函数指针嵌入到计划中。

And in conclusion of a single test: 并得出一个单一测试的结论:

In the case of this prime number validating function the performance advantage of SQLCLR over T-SQL is an order of magnitude in speedup! 在这种质数验证功能的情况下,与T-SQL相比,SQLCLR的性能优势是加速的一个数量级!

So, C# sounds like the way to go. 因此,C#听起来很可行。

我会两者都做,测量时间并坚持使用性能更好的方法。

Standard UDFs in SQL Server can be inefficient Because they are compiled each time they run, (as they are not included in the query plan for the entire SQL statement executed by the Query Processor). SQL Server中的标准 UDF效率低下,因为它们每次运行时都会被编译(因为它们不包含在查询处理器执行的整个SQL语句的查询计划中)。

This is because a standard UDF defines a processing algorithm that cannot be "folded" into the overall query plan that the outer sql statement is executing... 这是因为标准UDF定义了一种处理算法,该算法无法“折叠”到外部sql语句正在执行的整体查询计划中。

An inline table valued user defined function , otoh, because it is simply defined as a sql statement itself, can be folded into the overall query plan, and therefore is extremely fast and performant. 内联表值用户定义函数 otoh因为简单地定义为sql语句本身,因此可以折叠到整个查询计划中,因此非常快速且高效。

For a complex math function, generally this is probably not possible, or at minimum will be very difficult), but if your function can be written as an inline UDF, that would definitely be the way to go. 对于复杂的数学函数,通常这可能是不可能的,或者至少很难做到),但是如果您的函数可以作为内联UDF编写,那肯定是可行的方法。 Otherwise, you're probably better off doing it in code. 否则,最好在代码中进行。

As @otavio says you need to obtain measurements before deciding. 正如@otavio所说,您需要在决定之前获取测量值。 However should also consider the function it self in a larger set or optimisations . 但是,还应考虑在更大的范围内或对其进行优化的自身功能。 For example are you running this function many times on the same data, or could you perform this on as needed basis. 例如,您是在同一数据上多次运行此功能,还是可以根据需要执行此功能。 Can you store the results for example, and are you able to write a bigger function that operates over a set of data than calling it may times, etc. 例如,您可以存储结果吗?是否可以编写一个更大的函数来处理一组数据,而不是调用它等等。

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

相关问题 在c#中创建用户定义的表类型以在sql server存储过程中使用 - Create a user defined table type in c# to use in sql server stored procedure 将 C# 中的用户定义类型发布到 SQL Server 时出错 - Error publish user defined type in C# to SQL Server 如何导入Powerflex函数以在C#中使用? - How should I import Powerflex functions for use in C#? C#/ SQL Server 2005我应该对报表和数据网格使用OOP吗? - C# / SQL Server 2005 Should I use OOP for reports and datagrids? 我应该使用哪些类型来表示C#和SQL Server中的百分比? - What types should I use to represent percentages in C# and SQL Server? 注册C#汇编并在SQL Server中使用函数 - register C# assemblie and use functions in SQL Server 是否应该对带有参数(.NET C#)的SQL使用const字符串? - Should I use const string for SQL with parameters (.NET C#)? 代替C#Windows应用程序中的dataGridView,我应该如何使用控制台应用程序显示来自sql server的表? - Instead of dataGridView in C# windows application, what should I use for console application to display a table from sql server? 用户定义函数的SQL Server 2012安全问题 - Security issues on user defined functions for Sql server 2012 C#调用SQL Server中的用户定义标量函数,以表类型作为参数 - C# calling User Defined Scalar function in SQL Server that takes table type as its Parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM