SIGN UP MEMBER LOGIN:    
ARTICLE

SQLCLR vs. T-SQL - Performance Comparison

Posted by Manikavelu Velayutham Articles | SQL December 09, 2010
Before comparing the performance of SQLCLR and TSQL, just have a quick at the definition of SQLCLR.
Reader Level:


Before comparing the performance of SQLCLR and TSQL, just have a quick look at the definition of SQLCLR.

SQLCLR is the Microsoft name for the integration of CLR as a hosted runtime within SQL SERVER. It enables the execution of managed code inside the database.

Simple SQLCLR Architecture

sql.gif

T-SQL is nothing but the Transact SQL which is the native language supported by SQL SERVER.

CLR supported in SQL Server provides an alternative to the procedural approach in T-SQL.

Now the question comes, when to choose SQLCLR and when to choose T-SQL for the procedural approach. We can't blindly decide, because each approach has strengths and weaknesses. So the decision should be made judiciously.

Thumb Rule

  • If your logic mostly involves just the transformation of a large number of data, which can be easily performed by the declarative syntax of SQL, in that case T-SQL is preferred.
     
  • If your logic mostly involves the complex calculation on a row basis with a small amount of data, SQLCLR is preferred.

    From the above Thumb rule, another question may arise as below.

    I have a large number of data and the complex calculations are done on the row basis, in that case which I should prefer?

    It again depends on what you are going to do with the large number of data. Here we can't follow the thumb role. You need to test it with both the methods of large and small amount of data. A few inbuilt functions in SQL Server performs faster than in CLR functions and sometimes vice versa.

A comparison on String Split functionality

We need to split out strings in a string that has a delimiter and should be returned as a set of rows. This functionality is testing with above two approaches and the result is as follows.

Result - SQLCLR - Performs 3 times faster than T-SQL

So in this scenario SQLCLR performs better with a minimum amount of code.
  • User declarative TSQL like select, insert, update, delete whenever possible.
     
  • Procedural and row based approach is recommended only if it's not possible through normal TSQL command.
     
  • If the logic involves row by row processing with complex operation in forward only direction, CLR is the efficient way.
     
  • In some cases TSQL and SQLCLR should be combined to achieve a better performance.

So choosing the right one for the right scenario performs the best.

Login to add your contents and source code to this article
share this article :
post comment
 

I did not know the difference. Thanks for sharing Mani.

Posted by Mahesh Chand Dec 09, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor