0
Programming the CLR in  SQL Server 2005 Mark Blomsma Develop-One Session Code:   DB.08
<ul><li>Introductions </li></ul><ul><li>The End of T-SQL </li></ul><ul><li>How does it work? </li></ul><ul><ul><li>Hosting...
Mark Blomsma <ul><li>Professional developer since 1992 </li></ul><ul><li>Microsoft Certified Professional </li></ul><ul><l...
The end of T-SQL? <ul><li>No! Managed code will not solve all your problems.  </li></ul><ul><ul><li>Select/Insert/Update/D...
So why put C# in the database? <ul><li>Complex string manipulation </li></ul><ul><li>Encryption </li></ul><ul><li>XML mani...
Show me the magic <ul><li>Stability is everything for a database </li></ul><ul><li>CLR 2.0 offers more control to host env...
Limited FCL support <ul><li>Not available </li></ul><ul><ul><li>Environment.Exit() </li></ul></ul><ul><ul><li>System.Conso...
SQL OS <ul><li>CLR uses SQL OS for: </li></ul><ul><ul><li>Memory </li></ul></ul><ul><ul><li>Threads/fibers </li></ul></ul>...
Hosting layer <ul><li>Hosting layer provides coordination of: </li></ul><ul><ul><li>Assembly Loading </li></ul></ul><ul><u...
How do I use it? <ul><li>Stored Procedures </li></ul><ul><li>Functions </li></ul><ul><li>Triggers </li></ul><ul><li>Custom...
Stored procedures <ul><li>Public class </li></ul><ul><li>Pubic method </li></ul><ul><li>Method must be static </li></ul><u...
Stored procedures
Functions <ul><li>Public class </li></ul><ul><li>Public method </li></ul><ul><li>Method must be static </li></ul><ul><li>U...
Functions
Triggers <ul><li>Public class </li></ul><ul><li>Public method </li></ul><ul><li>Method must be static </li></ul><ul><li>Us...
Triggers
Custom Aggregations <ul><li>Public struct </li></ul><ul><li>Use  [SqlUserDefinedAggregate]  attribute </li></ul><ul><li>Im...
Custom Aggregations
User Defined Type <ul><li>Public struct </li></ul><ul><li>Use  [SqlUserDefinedType]  attribute </li></ul><ul><li>8KB size ...
Deployment <ul><li>Use sp_configure ‘clr enabled’, ‘1’ to enable sqlclr </li></ul><ul><ul><li>Off by default </li></ul></u...
Deployment
Monitor assemblies <ul><li>Profiler trace events:  </li></ul><ul><ul><li>CLR:load assembly monitors assembly load requests...
Monitoring
Tips <ul><li>Database server CPU is valuable </li></ul><ul><li>SQLCLR is about making DB programming easier </li></ul><ul>...
Questions?
References <ul><li>SQL Server 2005 distilled  by Eric Brown, ISBN: 0321349792 </li></ul><ul><li>http://msdn.microsoft.com/...
Addendum New SQL Server 2005 features <ul><li>Presentation by Dinesh Chandrasekhar </li></ul><ul><li>www.microsoft.com/sql...
Evaluatieformulier Vul je evaluatieformulier in en maak kans op een van de prachtige prijzen!! Session Code:   DB.08
Upcoming SlideShare
Loading in...5
×

SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)

1,028

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,028
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • The Common Language Runtime (CLR) is deeply integrated into the database engine. The CLR is really an engine of sorts that hosts applications—a special kind of application created using managed code. SQL Server is really a cohost of managed code. The SQL Engine works directly with the CLR to manage assemblies that have been placed inside SQL Server and that are called by Transact-SQL or MDX queries. The figure illustrates the coupling between the SQL Server Engine and the CLR. When you create an assembly in SQL Server, the bits are loaded into a table in the database. The assembly is registered but is not automatically pulled into memory. The bits simply exist in the database. The interaction between the CLR/hosting layer and the SQL operating system (OS) occurs until the assembly is called by a procedure. Upon evocation, the SQL Server Engine works with the CLR to manage memory, execution, and destruction of the running code for that assembly. Thus, the CLR goes through the SQL OS for the following: Memory. SQL Server manages its own memory. The CLR asks SQL for memory as needed. Threads/fibers get things done. Since SQL Server uses these natively, it makes sense for SQL to manage the CLR. All the threads are managed by SQL Server. Synchronization is moving data into and out of assemblies to TDS and in memory. The hosting layer has a set of APIs that manage the communication between the SQL Server OS and CLR. When objects are called, the CLR asks SQL Server for memory buffers, thread allocations, and security. The hosting layer manages the multiple security layers. The CLR uses something called an app domain to create an execution context that is really just a container for all the managed code found in a particular namespace. The app domain provides a container that controls interassembly interaction. Basically, no interaction occurs between different app domains. Thus, if a problem occurs, such as a memory issue, the app domain is the main container for SQL Server to unload the assembly. The CLR manages the escalation policy for assemblies. The CLR also maintains the state of the assembly. This topic could take up an entire chapter, but briefly, here’s what you need to know under All memory allocation from CLR through SQL Server.
  • Attributes on SqlFunctionAttribute DataAccess IsDeterministic IsPrecise Name
  • Safe: compute, access local data / database External access: files, registry, network Unsafe: full trust, unmanaged code, verification
  • Here&apos;s a brief list of interesting ways to monitor assemblies: Profiler trace events: CLR:load assembly monitors assembly load requests (successes and failures) SQL:BatchStarting, BatchCompleted SP:Starting, Completed, StmtStarting, StmtCompleted monitor execution of Transact-SQL and CLR routines Performance counters: SQL Server: Total CLR time .NET CLR Memory Processor DMVs and catalog views: sys.assembly* shows basic information about the assemblies stored sys.dm_os_memory_clerks sys.dm_clr* sys.dm_exec_query_stats sys.dm_exec_requests sys.dm_exec_cached_plans
  • Transcript of "SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)"

    1. 1. Programming the CLR in SQL Server 2005 Mark Blomsma Develop-One Session Code: DB.08
    2. 2. <ul><li>Introductions </li></ul><ul><li>The End of T-SQL </li></ul><ul><li>How does it work? </li></ul><ul><ul><li>Hosting Layer, SQL OS </li></ul></ul><ul><li>How do I use it? </li></ul><ul><ul><li>Stored procedures, Functions, Triggers, Custom aggregations, User Defined Type </li></ul></ul><ul><li>Deployment </li></ul><ul><li>Monitoring </li></ul><ul><li>Questions </li></ul>Programming the CLR in SQL Server 2005
    3. 3. Mark Blomsma <ul><li>Professional developer since 1992 </li></ul><ul><li>Microsoft Certified Professional </li></ul><ul><li>Microsoft Most Valuable Professional (MVP) for three years running </li></ul><ul><li>Current employment </li></ul><ul><ul><li>Software Architect : Develop-One </li></ul></ul><ul><ul><li>Consultant : Omnext.NET </li></ul></ul><ul><ul><li>Sales Manager North America : MRA Group </li></ul></ul>
    4. 4. The end of T-SQL? <ul><li>No! Managed code will not solve all your problems. </li></ul><ul><ul><li>Select/Insert/Update/Delete are here to stay ! </li></ul></ul><ul><ul><li>CLR is an alternative to the procedural portion of T-SQL </li></ul></ul>
    5. 5. So why put C# in the database? <ul><li>Complex string manipulation </li></ul><ul><li>Encryption </li></ul><ul><li>XML manipulation </li></ul><ul><li>No more need for external procedures </li></ul><ul><li>Tooling support for managed languages </li></ul><ul><li>Leverage .NET FCL & third party libraries </li></ul><ul><li>For everything that you cannot do with T-SQL </li></ul>
    6. 6. Show me the magic <ul><li>Stability is everything for a database </li></ul><ul><li>CLR 2.0 offers more control to host environment through “HostProtection” attribute </li></ul><ul><ul><li>No thread creation </li></ul></ul><ul><ul><li>No listening on sockets </li></ul></ul><ul><ul><li>No finalizers </li></ul></ul><ul><ul><li>Allow/deny allocation of memory </li></ul></ul><ul><ul><li>Deny killing the host process </li></ul></ul><ul><ul><li>CLR is sandboxed through CAS </li></ul></ul><ul><ul><ul><li>Checked during assembly creation process </li></ul></ul></ul><ul><li>CLR 2.0 AppDomain is hosted in SQL Server </li></ul><ul><ul><li>Loading and unloading of assemblies is controlled via SQL Server </li></ul></ul>
    7. 7. Limited FCL support <ul><li>Not available </li></ul><ul><ul><li>Environment.Exit() </li></ul></ul><ul><ul><li>System.Console </li></ul></ul><ul><ul><li>System.Windows.Forms </li></ul></ul><ul><ul><li>System.Drawing </li></ul></ul><ul><ul><li>System.Web </li></ul></ul><ul><ul><li>… </li></ul></ul>
    8. 8. SQL OS <ul><li>CLR uses SQL OS for: </li></ul><ul><ul><li>Memory </li></ul></ul><ul><ul><li>Threads/fibers </li></ul></ul><ul><ul><li>Synchronization </li></ul></ul>SQL Engine Windows SQL OS CLR Hosting Layer
    9. 9. Hosting layer <ul><li>Hosting layer provides coordination of: </li></ul><ul><ul><li>Assembly Loading </li></ul></ul><ul><ul><li>Memory management </li></ul></ul><ul><ul><li>Security Model </li></ul></ul><ul><ul><li>Reliability </li></ul></ul><ul><ul><li>Threads & Fibers </li></ul></ul><ul><ul><li>Deadlock detection </li></ul></ul><ul><ul><li>Execution context </li></ul></ul>SQL Engine Windows SQL OS CLR Hosting Layer
    10. 10. How do I use it? <ul><li>Stored Procedures </li></ul><ul><li>Functions </li></ul><ul><li>Triggers </li></ul><ul><li>Custom Aggregations </li></ul><ul><li>SqlContext </li></ul><ul><ul><li>SqlExecutionContext </li></ul></ul><ul><ul><li>SqlTriggerContext </li></ul></ul>
    11. 11. Stored procedures <ul><li>Public class </li></ul><ul><li>Pubic method </li></ul><ul><li>Method must be static </li></ul><ul><li>Use [SqlProcedure] attribute </li></ul><ul><li>Use in, out inout and return parameters </li></ul>
    12. 12. Stored procedures
    13. 13. Functions <ul><li>Public class </li></ul><ul><li>Public method </li></ul><ul><li>Method must be static </li></ul><ul><li>Use [SqlFunction] attribute </li></ul><ul><li>Must return a value </li></ul>
    14. 14. Functions
    15. 15. Triggers <ul><li>Public class </li></ul><ul><li>Public method </li></ul><ul><li>Method must be static </li></ul><ul><li>Use [SqlTrigger] attribute </li></ul><ul><ul><li>[SqlTrigger(Event=“FOR INSERT”)] </li></ul></ul><ul><li>Use SqlTriggerContext for getting at the data related to the trigger. </li></ul>
    16. 16. Triggers
    17. 17. Custom Aggregations <ul><li>Public struct </li></ul><ul><li>Use [SqlUserDefinedAggregate] attribute </li></ul><ul><li>Implement: </li></ul><ul><ul><li>Init </li></ul></ul><ul><ul><li>Accumulate </li></ul></ul><ul><ul><li>Merge </li></ul></ul><ul><ul><li>Terminate </li></ul></ul>
    18. 18. Custom Aggregations
    19. 19. User Defined Type <ul><li>Public struct </li></ul><ul><li>Use [SqlUserDefinedType] attribute </li></ul><ul><li>8KB size limit. </li></ul><ul><li>Do not use for business objects. </li></ul>
    20. 20. Deployment <ul><li>Use sp_configure ‘clr enabled’, ‘1’ to enable sqlclr </li></ul><ul><ul><li>Off by default </li></ul></ul><ul><li>Use create assembly </li></ul><ul><li>SqlClr uses 3 CAS permission buckets </li></ul><ul><ul><li>Safe </li></ul></ul><ul><ul><li>External access </li></ul></ul><ul><ul><li>Unsafe </li></ul></ul>
    21. 21. Deployment
    22. 22. Monitor assemblies <ul><li>Profiler trace events: </li></ul><ul><ul><li>CLR:load assembly monitors assembly load requests (successes and failures) </li></ul></ul><ul><ul><li>SQL:BatchStarting, BatchCompleted </li></ul></ul><ul><ul><li>SP:Starting, Completed, StmtStarting, StmtCompleted monitor execution of Transact-SQL and CLR routines </li></ul></ul><ul><li>Performance counters: </li></ul><ul><ul><li>SQL Server: Total CLR time </li></ul></ul><ul><ul><li>.NET CLR Memory </li></ul></ul><ul><ul><li>Processor </li></ul></ul><ul><li>DMVs and catalog views: </li></ul><ul><ul><li>sys.assembly* shows basic information about the assemblies stored </li></ul></ul><ul><ul><li>sys.dm_os_memory_clerks </li></ul></ul><ul><ul><li>sys.dm_clr* </li></ul></ul><ul><ul><li>sys.dm_exec_query_stats </li></ul></ul><ul><ul><li>sys.dm_exec_requests </li></ul></ul><ul><ul><li>sys.dm_exec_cached_plans </li></ul></ul>
    23. 23. Monitoring
    24. 24. Tips <ul><li>Database server CPU is valuable </li></ul><ul><li>SQLCLR is about making DB programming easier </li></ul><ul><li>Managed code has a performance advantage over T-SQL with respect to most procedural computation, but for data-access T-SQL generally fares better. </li></ul>
    25. 25. Questions?
    26. 26. References <ul><li>SQL Server 2005 distilled by Eric Brown, ISBN: 0321349792 </li></ul><ul><li>http://msdn.microsoft.com/sql/learning/prog/clr/default.aspx?pull=/library/en-us/dnsql90/html/sqlclrguidance.asp </li></ul><ul><li>http://blog.develop-one.com </li></ul><ul><li>This presentation on: </li></ul><ul><ul><li>www.sdn.nl </li></ul></ul><ul><ul><li>www.develop-one.com </li></ul></ul>
    27. 27. Addendum New SQL Server 2005 features <ul><li>Presentation by Dinesh Chandrasekhar </li></ul><ul><li>www.microsoft.com/sql/prodinfo/overview/whats-new-in-sqlserver2005.mspx </li></ul><ul><li>http://msdn2.microsoft.com/en-us/library/ms170363(SQL.90).aspx </li></ul>
    28. 28. Evaluatieformulier Vul je evaluatieformulier in en maak kans op een van de prachtige prijzen!! Session Code: DB.08
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×