views:

156

answers:

2

I'm a C# developer who has done some basic database work in T-SQL. However, I need to write a very complicated stored procedure, well above my T-SQL knowledge.

Will writing a stored procedure in C# using the .net CLR as part of SQL Server 2008 cause my stored procedure to be less efficient than if it were written in T-SQL? Is the difference (if any) significant? Why?

+2  A: 

Please see Performance of CLR Integration:

This topic discusses some of the design choices that enhance the performance of Microsoft SQL Server integration with the Microsoft .NET Framework common language runtime (CLR).

Andrew Hare
+4  A: 

CLR require some communication overhead (to pass data between the CLR and SQL Server)

Rule of thumb is:

  • If your logic mostly includes transformations of massive sets of data, which can be performed using set operations, then use TSQL.

  • If your logic mostly includes complex computations of relatively small amounts of data, use CLR.

With set operations much more can be done than it seems. If you post your requirements here, probably we'll be able to help.

Quassnoi
How big of a communication overhead are we talking about? I do have massive sets of data, but I also am doing complex computations on it rather than set operations.
David Pfeffer
`@bytenik`: this heavily depends on what you are going to do with the data. A built-in `SQL` aggregate function, for instance, works about `3` times as fast as a `CLR` aggregate. If you are going, say, to join two recordsets, you'll need to develop and hardcode the join into the `CLR`, while `SQL Server` has several native algorithms and selects the most appropriate one, which may increase performance by orders of magnitude. On the other side, `SQL Server` lacks running aggregates, and a `CLR` can help greatly. I need to see your requirements to give a more specific answer.
Quassnoi