c# - 我应该在 SQL Server 还是 C# 中使用 "User Defined Functions"?

标签 c# .net sql-server

我有一个相当复杂的数学函数,有人建议我将其实现为 SQL Server 中的用户定义函数,以便可以在 SQL 查询中有效地使用它。

问题是它必须非常高效,因为它每秒可能执行数千次,后来我听说 UDF 效率非常低。

有人建议我可以用 C# 来实现该函数,这样效率会更高。

我应该做什么?

最佳答案

复杂的数学函数在 C# 中的执行速度比 T-SQL 快得多。非常值得一试。

编辑在回复您的评论时,我发现 this blog post ,其中指出:

User-defined scalar functions are likely the most obvious candidates for SQLCLR usage. There are two primary reasons for this. The first is that CLR functions actually have lower invocation overhead than T-SQL functions. T-SQL functions require the runtime to create a new T-SQL frame, which is expensive. CLR functions are embedded in the plan as a function pointer for direct execution.

单个测试的结论:

In the case of this prime number validating function the performance advantage of SQLCLR over T-SQL is an order of magnitude in speedup!

所以,C# 听起来是不错的选择。

关于c# - 我应该在 SQL Server 还是 C# 中使用 "User Defined Functions"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2733634/

相关文章:

sql - 在 SQL 中获取当前年份的第一个月和第一天

c# - 使用 AvalonEdit 显示无效的 XML 语法

c# - 使用 HTTPWebRequest 获取远程页面的标题

c# - 隐藏截断复选框的工具提示

.net - 为 COM 互操作性注册我的 .net 程序集不会公开我的方法

c# - MySql LIMIT 1 子句的替代方案 - 不希望仅限于一个 RDBMS

sql-server - SQL Server 作业动态调度

c# - 如何使用正则表达式从字符串(如果有)中删除最后一个逗号和空格?

C#——如果没有数据通过管道传输到程序,Console.In.ReadToEnd() 在用户输入时挂起

c# - 从派生类调用的虚拟方法中的 typeof this