Home > SQL Server Tips > Database Administration > Writing SQL Server CLR routines
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE ADMINISTRATION

Writing SQL Server CLR routines


Adam Machanic, Contributor
02.09.2006
Rating: -5.00- (out of 5)


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


The much-lauded SQL CLR is a powerful tool in the SQL Server 2005 developer's arsenal. Being able to develop routines in .NET languages that run within SQL Server's process space gives developers the ability to create new kinds of custom methods and extend the server's type and aggregation system, giving them quite a bit of newfound control!

Using these new features is fairly straightforward, but like any other new programming paradigm there are bound to be pitfalls. The following is a loose collection of tips that represent some of my more important findings and realizations as I've worked with SQL CLR integration over the last several months.

Treat SQL CLR data access code like client data access code

The ADO.NET unified provider makes it incredibly easy to migrate code between tiers; virtually all of the ADO.NET classes you can use in client tiers function the same in the data tier. As a matter of fact, code migration from a client tier into SQL Server typically only requires changing the connection string to make use of the context connection.

As developers, we have quite a few standards and best practices built upon data access. A common adage to live by is "open late, close early." In other words, open connections at the last possible moment and close them as soon as you're done accessing data. Another example is stored procedures. Although there is certainly some debate in the areawhether or not stored procedures should be used instead of ad hoc SQL, most data access developers know the benefits of using stored procedures to encapsulate data access.

Given the ease with which data can be migrated between tiers when coding SQL CLR routines, you can never say for certain that a given piece of code will not have to be migrated between tiers. So keep the coding standards simple: Use the same standards inside SQL CLR routines that you'd use in client tiers for data access. This will make later migration and maintenance much easier.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
Database Administration
Top load balancing methods for SQL Server
Performance implications of transaction log autogrowth in SQL Server
The keys to database backup protection for SQL Server
Understanding transparent data encryption in SQL Server 2008
Working with sparse columns in SQL Server 2008
Determining the source of full transaction logs in SQL Server
Implementing SQL Server 2008 FILESTREAM functionality
Improving SQL Server full-text search performance
Using the OPENROWSET function in SQL Server
New replication features in SQL Server 2008 and what they mean to you

Microsoft SQL Server 2005 (Yukon)
SQL Server Reporting Services Fast Guide
SQL Server Service Broker Tutorial and Reference Guide
Tips for tuning SQL Server 2005 to improve reporting performance
SQL Server consolidation: Why it's an optimization technique
Parent-child dimensions in SQL Server 2005 with Analysis Services MDX
Enforcing data integrity in a SQL Server database
SSIS error message due to installation problem on SQL Server 2005
Should you upgrade to SQL Server 2005 or SQL Server 2008?
Basics for working with DATETIME and SMALLDATETIME in SQL Server 2005
How to configure Database Mail in SQL Server 2005 to send mail
Microsoft SQL Server 2005 (Yukon) Research

SQL/Transact SQL (T-SQL)
Working with sparse columns in SQL Server 2008
Determining the source of full transaction logs in SQL Server
New GROUP BY option provides better data control in SQL Server 2008
Using the OPENROWSET function in SQL Server
Loading data files with SQL Server's BULK INSERT statement
Importing and exporting bulk data with SQL Server's bcp utility
Testing transaction log autogrowth behavior in SQL Server
Securing SQL Server with access control, login monitoring and DDL triggers
Top 10 SQL Server development tips of 2008
The sqlcmd utility in SQL Server
SQL/Transact SQL (T-SQL) Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
application server  (SearchSQLServer.com)
Yukon  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


>Avoid DataSets in SQL CLR stored procedures and functions

Rules are meant to be broken. Unfortunately, the previous tip has its caveats. If you're migrating code that uses DataSets to store intermediate data, ask yourself why you're migrating. Hoping to see performance improvements by transferring less data across the network? Consider that you'll still be marshalling data in and out of the CLR via the ADO.NET objects; the data has to get into the DataSet somehow.

Does the migration still make sense? Consider a rewrite. DataSets (and collection classes) used as intermediate stores for data processing can take up a lot of memory, which may be needed for one of SQL Server's other pools. Thanks to SQL Server's pricey licensing, every megabyte of memory SQL Server may need is quite a bit more expensive than a megabyte of memory in the middle tier.

Instead of using the DataSet, try rewriting your code with a DataReader. The DataReader streams rows one at a time from SQL Server and therefore uses a lot less memory at once. That translates into less memory overhead for your routine and cheaper overall cost for a scalable solution.

Procedural logic is no better in SQL CLR than in T-SQL

If you've spent a lot of time reading SQL Server Web sites, you've probably learned that the most important thing you should do to write high-performance T-SQL: Avoid procedural logic (cursors) unless there is truly no other solution.

Before migrating procedural code that allows data access into SQL Server, carefully consider whether the benefits you're hoping to see will manifest. Just like RAM mentioned in the previous tip, CPU time in SQL Server is expensive compared to CPU time in client tiers. Scaling out a middle tier is generally cheaper than scaling up a database server.

Procedural code is often inefficient in a database context. Unless you have absolutely no other option, consider a rewrite instead of a straight migration. Can you rewrite the procedural logic using set-based T-SQL? Time spent doing such a rewrite will probably end up being cheaper than having to upgrade the SQL Server later because of excessive resource utilization.

Modularize: Use UDFs instead of stored procedures whenever possible

User-defined functions (UDFs) are a great way to encapsulate logic for flexible reuse. Unlike stored procedures, which can only be executed, UDFs can be used in other queries' SELECT lists, FROM clauses, WHERE clauses, etc. This makes them ideal for utility logic that needs to be reused again and again in a database system.

T-SQL UDFs were introduced in SQL Server 2000, but many database developers shied away from them due to performance concerns. T-SQL UDFs are called once for every row of the result set they are being executed against. Alone this might not be a problem, but T-SQL UDFs are also known to be quite slow; combining row-by-row execution with slow processing means greatly decreased performance. Thankfully, Tthese concerns are no longer warranted thanks to SQLCLR integration.

SQL CLR UDFs perform better than their T-SQL counterparts, showing especially vast improvements in the areas of mathematical operations and string handling. If you require common utility methods in these areas, there is no question that UDFs are the best choice in SQL Server 2005.

Also consider which type of routine to use when migrating code from client tiers into SQL Server. Once again, SQLCLR UDFs make a lot of sense. Instead of creating a stored procedure, which may or may not be usable in all future scenarios, consider a UDF. Its flexibility will guarantee you'll be able to utilize the logic in many different situations.

Favor T-SQL triggers that call SQL CLR UDFs over SQLCLR triggers

A SQL CLR trigger is a CLR method -- just like a UDF or a stored procedure – but it is defined as a trigger instead of a callable routine. This means that the logic it contains may not be used in other situations.

Instead of creating SQL CLR triggers, try creating T-SQL triggers that call SQL CLR UDFs for logic or external operations. This will make your code more modular and more reusable for a variety of requirements. Who knows when you'll need to use the trigger logic elsewhere?

Encapsulate data verification logic in SQL CLR UDTs

SQL CLR user-defined types (UDTs) have received mixed reactions from database developers. Some hate the new functionality and others can't wait to use it. I won't get into that debate here (stay tuned for an upcoming tip on this topic), but if you are going to use UDTs you should take advantage of all of the power they bring to the table.

New instances of SQL CLR UDTs are created under the covers by a method called Parse. This method's stated goal is to manipulate (parse) the input string into its component parts in order to populate the UDT's member fields and, therefore, create an instance of the type. But Parse can be much more than that.

Instead of simply breaking up the string, use Parse to encapsulate data verification logic. Once you've done that, your type will be self-enforcing. This eliminates the need to do things like bind CHECK constraints to columns that use the type -- if you know that the type enforces its own rules, there is no reason to repeat the work. This will also allow you to encode rules in exactly one place. Using the same type two times in the same database? You will not need two constraints if the type implements its own rule checking logic. The economy of embedding verification logic in UDTs should be immediately apparent: The more you use the type, the better the return on investment.

Summary

Those who've read this far are probably somewhat surprised to see that these tips mainly fall into the realm of monetary savings rather than raw performance. Money is a topic that all of my clients -- and most likely your employers -- are extremely interested in. By carefully considering when and how to use SQL CLR integration you can enhance your system's overall performance. But by misusing these features, you may incur financial losses.

The most important lesson learned so far is to think and test carefully before putting any solution into effect. There are always alternatives, so spend a few minutes before getting too invested in any one solution. A better, cheaper choice may exist.

About the author: Adam Machanic is a database-focused software engineer, writer and speaker based in Boston, Mass. He has implemented SQL Server for a variety of high-availability OLTP and large-scale data warehouse applications, and also specializes in .NET data access layer performance optimization. He is a Microsoft Most Valuable Professional (MVP) for SQL Server and a Microsoft Certified Professional. Machanic is co-author of Pro SQL Server 2005, published by Apress.


More information from SearchSQLServer.com

  • Tip: Top 10 new features in SQL Server 2005
  • FAQ: SQL Server 2005 features and functions
  • Learning Guide: SQL Server 2005 Learning Guide


  • Rate this Tip
    To rate tips, you must be a member of SearchSQLServer.com.
    Register now to start rating these tips. Log in if you are already a member.


    Submit a Tip




    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



    SQL Server Development - .NET, C#, T-SQL, Visual Basic
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts