Performance and usage of CLR functions in SQL

只谈情不闲聊 提交于 2019-12-22 12:54:20

问题


SQL Server allows you to create CLR functions, stored procedures, user types and other objects, for purpose that are really complex to be done inside SQL.

But, can someone compare those two things: TSQL Object and CLR Object, in sense of performance, benefits, and so on.

What are real situations for usage CLR objects?

Is there any best practices proposition for their usage?


回答1:


What are real situations for usage CLR objects?

SQL Server lacks an aggregate string concatenation function. This bizarre oversight leads to all manner of complicated work-arounds.

Creating and using a custom CLR aggregate function is a clean solution to this problem, and is in fact the reference example given in MSDN article on the subject of custom aggregate functions:

http://msdn.microsoft.com/en-us/library/ms131056.aspx

Performance

There's an MSDN article that gives at least a theoretical (no metrics) overview:

http://msdn.microsoft.com/en-us/library/ms131075.aspx

And here's a more practical post (with metrics) from AboutSqlServer.com:

http://aboutsqlserver.com/2013/07/22/clr-vs-t-sql-performance-considerations/




回答2:


There are two questions here that need to be addressed separately.

  1. In terms of functionality & benefits, I wrote an article (part of a series on the topic of SQLCLR) that looks at what uses of SQLCLR are "appropriate", mainly by looking at what it can do that cannot be done otherwise, or not done nearly as easily. That article is "Stairway to SQLCLR Level 1: What is SQLCLR?" (free registration required) and it is summarized in an answer to the question that was linked in a comment on the question, Advantage of SQL SERVER CLR.

  2. In terms of performance, I published a study a few years ago (July, 2011) that detailed various scenarios and tested the raw SQL, that SQL in a T-SQL function, and that algorithm in a CLR-based function. I tested both scalar functions and table-valued functions. That article is "CLR Performance Testing" (no registration required). Please keep in mind that the testing was done on SQL Server 2008 and there were performance improvements made in SQL Server 2012 regarding deterministic scalar CLR-based functions. Meaning, the performance results of the CLR functions would be better if re-running those tests on at least SQL Server 2012 if not a newer version. But the conclusion, even on SQL Server 2008 without having those improvements, is that it depends on many factors and sometimes CLR is faster and sometimes T-SQL is faster. But, most often a formula that can be expressed in simple to moderate T-SQL as part of the query, and not abstracted to a function of either type, is by far the fastest.



来源:https://stackoverflow.com/questions/15921956/performance-and-usage-of-clr-functions-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!