Sql Summit Clr, Service Broker And Xml


Published on

Presentation from the SQL Summit in Grand Rapids for the launch of SQL Server 2005

Published in: Technology
1 Comment
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Sql Summit Clr, Service Broker And Xml

    1. 2. SQL Server 2005 for Developers David Truxall NuSoft Solutions Drew Robbins Microsoft Corporation
    2. 3. Agenda <ul><li>XML Enhancements </li></ul><ul><li>CLR in SQL Server </li></ul><ul><li>Service Broker </li></ul>
    3. 4. XML in the Database Overview <ul><li>XML has become widely adopted </li></ul><ul><li>XML is ubiquitous, extensible, and a platform independent transport format </li></ul><ul><li>Supports structured and semi-structured data </li></ul><ul><li>Limited support in SQL Server 2000 </li></ul><ul><li>First class support in SQL Server 2005 </li></ul>
    4. 5. XML In the Database Business Opportunity <ul><li>The majority of all data transmitted electronically between organizations is now in XML. </li></ul><ul><li>By storing XML natively, SQL Server 2005 helps eliminate the friction of data transfer between organizations. </li></ul><ul><li>With the improved XML support in VS 2005, building e-business applications is radically simplified. </li></ul>
    5. 6. XML in the Database XML Data Type <ul><li>SQL Server 2005 adds native XML storage with XML data type </li></ul><ul><li>Stores both well-formed and validated XML </li></ul><ul><li>Implementation </li></ul><ul><ul><li>Markup documents </li></ul></ul><ul><ul><li>Tree-structured data where re-assembly costs should be minimal </li></ul></ul><ul><ul><li>Semi-structured data </li></ul></ul>
    6. 7. XML in the Database XML Data Type <ul><li>An alternative to storing XML on disk </li></ul><ul><li>Supports column, variable, or parameter </li></ul><ul><li>Can represent Version 1.0 documents or fragments </li></ul><ul><li>Methods on XML data type </li></ul><ul><ul><li>query(), value(), exist(), modify(), and nodes() </li></ul></ul>
    7. 8. XML in the Database XML Schema Support <ul><li>Validation and typing provided by XSD </li></ul><ul><ul><li>W3C recommendation </li></ul></ul><ul><ul><li>Mechanism for type definitions </li></ul></ul><ul><ul><li>Validation constraints </li></ul></ul><ul><li>Typed Data </li></ul><ul><ul><li>Guarantees the shape of the data </li></ul></ul><ul><ul><li>Allows storage and query optimizations </li></ul></ul>
    8. 9. XML in the Database XML Schema Collections <ul><li>Creating an XML Schema Collection </li></ul><ul><ul><li>CREATE XML SCHEMA COLLECTION ResumeSchema AS </li></ul></ul><ul><ul><li>'<xs:schema </li></ul></ul><ul><ul><li>targetNameSpace='http://schemas.ms.com/emp&quot; …/>' </li></ul></ul><ul><li>Constraining XML documents </li></ul><ul><ul><li>CREATE TABLE Employee (..., Resume XML(ResumeSchema)) </li></ul></ul><ul><li>Retrieving XML Schema Collections </li></ul><ul><ul><li>SELECT Name </li></ul></ul><ul><ul><li>FROM AdventureWorks.sys.xml_schema_collections </li></ul></ul>
    9. 10. XML Data Type
    10. 11. XML in the Database XML Indexes <ul><li>Create XML index on XML column </li></ul><ul><ul><li>CREATE PRIMARY XML INDEX IDX1 ON JobCandidate (Resume) </li></ul></ul><ul><li>Creates a secondary, B-tree index on </li></ul><ul><ul><li>Path, Property, or Value </li></ul></ul><ul><li>Speeds up queries </li></ul><ul><ul><li>Results can be served directly from index </li></ul></ul><ul><ul><li>Uses SQL Server's cost based optimizer </li></ul></ul><ul><li>Full-Text Indexing/Searching supports XML </li></ul>
    11. 12. XML in the Database XML Indexes <ul><li>PATH Index </li></ul><ul><ul><li>When querying by path and value </li></ul></ul><ul><ul><li>/ItemList/Item[@ProductID=&quot;1&quot;] </li></ul></ul><ul><li>PROPERTY Index </li></ul><ul><ul><li>When retrieving node values by path </li></ul></ul><ul><ul><li>(/ItemList/Item/@ProductID)[1] </li></ul></ul><ul><li>VALUE Index </li></ul><ul><ul><li>When querying imprecisely, like for existence </li></ul></ul><ul><ul><li>//Item[@ProductID=&quot;1&quot;] </li></ul></ul>
    12. 13. XML in the Database XML Indexes <ul><li>Requirements </li></ul><ul><ul><li>A clustered PK must exist in the table </li></ul></ul><ul><ul><li>If XML index exists, the PK may not be altered </li></ul></ul><ul><ul><li>Only one primary XML index per column </li></ul></ul><ul><ul><li>Many secondary XML indexes per column </li></ul></ul><ul><ul><ul><li>PATH, PROPERTY, and VALUE </li></ul></ul></ul><ul><ul><li>All index names must be unique per table </li></ul></ul>
    13. 14. XML in the Database XQuery <ul><li>Query XML documents and data </li></ul><ul><ul><li>Based on W3C working draft </li></ul></ul><ul><ul><li>FLWOR - For, Let, Where, Order by, Return </li></ul></ul><ul><li>XQuery methods </li></ul><ul><ul><li>.query() – returns XML data type instance </li></ul></ul><ul><ul><li>.value() – returns a single (scalar) value </li></ul></ul><ul><ul><li>.exist() – returns 1 if expression returns item(s) </li></ul></ul><ul><ul><li>.nodes() – returns a row per selected node </li></ul></ul>
    14. 15. XML in the Database XML Data Manipulation Language <ul><li>Inefficient to UPDATE the entire column </li></ul><ul><li>XML sub-tree modification using .modify() </li></ul><ul><ul><li>Three actions: Insert , Delete , or Replace </li></ul></ul><ul><ul><li>UPDATE xmlTable SET xmlColumn.modify('insert </li></ul></ul><ul><ul><li><section num=&quot;2&quot;> </li></ul></ul><ul><ul><li><heading>Background</heading> </li></ul></ul><ul><ul><li></section> </li></ul></ul><ul><ul><li>after (/doc/section[@num=1])[1]') </li></ul></ul>
    15. 16. XQuery
    16. 17. XML in the Database FOR XML Improvements <ul><li>Support for XSD schemas </li></ul><ul><li>Support for NULL values </li></ul><ul><li>Support for XML data type </li></ul><ul><li>Nested FOR XML </li></ul><ul><li>Can be assigned to an XML data type </li></ul><ul><li>FOR XML PATH </li></ul>
    17. 18. CLR in the Database Overview <ul><li>Enhanced programming model </li></ul><ul><li>Enhanced safety and security </li></ul><ul><li>User defined types and aggregates </li></ul><ul><li>Common development environment </li></ul><ul><li>Performance and scalability </li></ul>
    18. 19. CLR in the Database Business Opportunity <ul><li>Leverage .NET productivity and the BCL </li></ul><ul><li>Better way to encapsulate your IP </li></ul><ul><li>A new world of ways to extend SQL Server </li></ul><ul><ul><li>User-defined types to fit your domain </li></ul></ul><ul><ul><li>User-defined aggregates to fit your domain </li></ul></ul><ul><li>Go over the wall you hit with T-SQL </li></ul><ul><li>Replace Extended Procedures (XPs) </li></ul>
    19. 20. CLR in the Database Architecture <ul><li>Common Language Runtime (CLR) Assemblies </li></ul><ul><ul><li>Can write stored procedures, triggers, UD functions, UD types, and aggregate functions </li></ul></ul><ul><ul><li>Any CLR language </li></ul></ul><ul><ul><li>Great replacement for Extended Stored Procedures (XPs) </li></ul></ul><ul><li>Supported in SQL Express </li></ul>
    20. 21. CLR in the Database Microsoft® SQL Server™ Runtime hosted inside SQL Microsoft® Visual Studio® .NET Project VB, C# SQL Queries: select sum( tax(sal,state) ) from Emp where county = ‘King’ Assembly: “TaxLib.dll” build SQL Data Definition: create assembly … create function … create procedure … create trigger … create type … deploy
    21. 22. CLR in the Database Security <ul><li>Managed Code running in CLR </li></ul><ul><ul><li>Type safety, application domains, CAS </li></ul></ul><ul><ul><li>More secure than XPs </li></ul></ul><ul><li>Reliability and safety design goals </li></ul><ul><ul><li>User code can't compromise SQL engine </li></ul></ul><ul><ul><li>User code can't overwrite SQL engine memory buffers or internal data structures </li></ul></ul><ul><ul><li>User code must be authenticated and authorized before being allowed access </li></ul></ul>
    22. 23. CLR in the Database Security <ul><li>Permission Sets </li></ul><ul><ul><li>SAFE </li></ul></ul><ul><ul><li>EXTERNAL_ACCESS </li></ul></ul><ul><ul><li>UNSAFE </li></ul></ul>
    23. 24. CLR in the Database T-SQL or Managed Code? <ul><li>T-SQL is superior when </li></ul><ul><ul><li>Code mostly performs data access </li></ul></ul><ul><ul><li>Code has little or no procedural logic </li></ul></ul><ul><li>Managed Code is superior when </li></ul><ul><ul><li>Code has CPU-intensive methods </li></ul></ul><ul><ul><li>Code has complex logic </li></ul></ul><ul><ul><li>Code can make use of the .NET BCL </li></ul></ul>
    24. 25. CLR in the Database Managed Database Objects X Aggregate Functions X User Defined Types X X User Defined Functions X X Triggers X X Stored Procedures .NET (SQL 2005) T-SQL (SQL 2000)
    25. 26. CLR in the Database Managed Stored Procedures <ul><li>Stored Procedures </li></ul><ul><ul><li>Public, static methods on a class </li></ul></ul><ul><ul><li>Returns a void or int </li></ul></ul><ul><ul><li>SqlPipe.Execute() and SqlPipe.Send() </li></ul></ul><ul><ul><ul><li>public static void ExecuteToClient() { SqlConnection conn = new SqlConnection(&quot;context connection=true&quot;); </li></ul></ul></ul><ul><ul><ul><li>SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = &quot;select @@version&quot;; SqlContext.GetPipe().Execute(cmd); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
    26. 27. CLR in the Database Managed Triggers <ul><li>Triggers </li></ul><ul><ul><li>Similar to stored procedures </li></ul></ul><ul><ul><li>Determine which columns have been modified </li></ul></ul><ul><ul><li>Reference INSERTED and DELETED tables </li></ul></ul><ul><ul><li>SqlConnection conn = new SqlConnection(&quot;context connection=true&quot;); </li></ul></ul><ul><ul><ul><li>sqlCommand sqlComm = conn.CreateCommand() </li></ul></ul></ul><ul><ul><ul><li>sqlComm.CommandText = &quot;SELECT column_1 from INSERTED&quot;; </li></ul></ul></ul>
    27. 28. CLR in the Database Managed User Defined Functions <ul><li>Two types of User Defined Functions </li></ul><ul><ul><li>Scalar-valued function (SVF) </li></ul></ul><ul><ul><ul><li>Returns a single value, such as string or integer </li></ul></ul></ul><ul><ul><ul><li>Class methods determine return type </li></ul></ul></ul><ul><ul><li>Table-valued function (TVF) </li></ul></ul><ul><ul><ul><li>Data returned through an ISqlReader object </li></ul></ul></ul><ul><ul><ul><li>public static ISqlReader ReadLog(String logname) { </li></ul></ul></ul><ul><ul><ul><li>return (ISqlReader)new MySqlReader(logname); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
    28. 29. CLR in the Database Managed User Defined Types <ul><li>When the built-in SQL Server types won't do </li></ul><ul><ul><li>Create custom, complex structured types </li></ul></ul><ul><ul><li>Benefits </li></ul></ul><ul><ul><ul><li>Strong encapsulation (both at client and server) </li></ul></ul></ul><ul><ul><ul><li>Deep integration support with SQL Server </li></ul></ul></ul><ul><ul><li>Requirements </li></ul></ul><ul><ul><ul><li>SqlUserDefinedTypeAttribute, Serializable, INullable, Parse(), ToString() </li></ul></ul></ul><ul><ul><ul><li>Read() and Write() to support custom serialization </li></ul></ul></ul><ul><ul><ul><li>Others </li></ul></ul></ul>
    29. 30. CLR in the Database Managed Aggregate Functions <ul><li>Perform a calculation on a set of values and return a single value, such as SUM or MAX </li></ul><ul><ul><li>SqlUserDefinedAttribute </li></ul></ul><ul><ul><li>Must implement these methods so the Query Processor can use your aggregate function </li></ul></ul><ul><ul><ul><li>Init(), Accumulate(), Merge(), Terminate() </li></ul></ul></ul>
    30. 31. CLR in the Database Assemblies <ul><li>.NET Assemblies </li></ul><ul><ul><li>Self-describing, metadata, dependencies </li></ul></ul><ul><ul><li>Unit of deployment, CLR permission grants, versioning, etc. </li></ul></ul><ul><ul><li>Stored in database </li></ul></ul><ul><li>CREATE, ALTER, DROP DDL </li></ul><ul><ul><li>CREATE ASSEMBLY FinanceFunctions FROM 'c:codeFinanceFunctions.dll' WITH PERMISSION_SET = SAFE </li></ul></ul>
    31. 32. CLR in the Database
    32. 33. Service Broker Overview <ul><li>Service Broker adds asynchronous, distributed, and decoupled environment </li></ul><ul><ul><li>Fully integrated into database engine </li></ul></ul><ul><ul><li>New set of SQL Server objects (DDL & DML) </li></ul></ul><ul><ul><li>Integrated management and deployment </li></ul></ul><ul><li>Custom solutions and benefits </li></ul><ul><ul><li>Generalized asynchronous semantics </li></ul></ul><ul><ul><li>Inter and intra-instance queuing </li></ul></ul><ul><ul><ul><li>Distributed transactions not required </li></ul></ul></ul><ul><ul><li>Multi-threading in stored procedures </li></ul></ul>
    33. 34. Service Broker Business Opportunity <ul><li>Don't call us, we'll call you </li></ul><ul><li>Asynch can result in faster applications </li></ul><ul><li>Asynch can smooth out connectivity bugs </li></ul><ul><li>Notifications can help w/ middle-tier or client caching of data </li></ul><ul><li>Leverage Message Queue architecture </li></ul><ul><li>Shares Begin/End patterns with .NET </li></ul>
    34. 35. Service Broker Asynchronous Architecture <ul><li>Loose coupling </li></ul><ul><li>Load distribution </li></ul><ul><li>Batch processing </li></ul><ul><li>Performance </li></ul><ul><li>Scale out </li></ul>
    35. 36. Service Broker Order Entry Data Flow Diagram
    36. 37. Service Broker Features <ul><li>Database objects and DML statements that enable asynchronous messaging operations </li></ul><ul><ul><li>Queues, Message Types, Contracts, Services </li></ul></ul><ul><ul><li>“ BEGIN DIALOG”, “SEND”, “RECEIVE” </li></ul></ul><ul><li>Fully integrated into the database engine </li></ul><ul><ul><li>Single Programming Model </li></ul></ul><ul><ul><li>Local Transactions </li></ul></ul><ul><ul><li>Integrated Management and Deployment </li></ul></ul>
    37. 38. Service Broker <ul><li>Both the client & stored procedure run in: </li></ul><ul><li>Same Transaction </li></ul><ul><li>Same Security Context </li></ul><ul><li>Same Thread of Execution </li></ul>Client App SQL Engine Stored Proc Without Service Broker Transaction
    38. 39. Service Broker Client App Launched Stored Proc <ul><li>Stored proc runs on: </li></ul><ul><li>Different transaction </li></ul><ul><li>Different thread </li></ul><ul><li>Different security </li></ul><ul><li>Different time </li></ul>With Service Broker Transaction Transaction Persisted Dialog
    39. 40. Service Broker Dialogs <ul><li>Two-way Messaging “Dialogs” </li></ul><ul><ul><li>Exactly-once, In-order </li></ul></ul><ul><ul><li>Reliable delivery to local and remote queues </li></ul></ul><ul><ul><li>Large Message Fragmentation </li></ul></ul>APP 1 SQL Engine SQL Engine APP 2
    40. 41. Service Broker New Commands <ul><ul><li>CREATE MESSAGE TYPE </li></ul></ul><ul><ul><li>CREATE CONTRACT </li></ul></ul><ul><ul><li>CREATE QUEUE </li></ul></ul><ul><ul><li>CREATE SERVICE </li></ul></ul><ul><ul><li>ALTER SERVICE </li></ul></ul><ul><ul><li>ALTER MESSAGE TYPE </li></ul></ul><ul><ul><li>ALTER QUEUE </li></ul></ul><ul><ul><li>ALTER CONTRACT </li></ul></ul><ul><ul><li>DROP MESSAGE TYPE </li></ul></ul><ul><ul><li>DROP CONTRACT </li></ul></ul><ul><ul><li>DROP QUEUE </li></ul></ul><ul><ul><li>DROP SERVICE </li></ul></ul><ul><ul><li>CREATE ROUTE </li></ul></ul><ul><ul><li>ALTER ROUTE </li></ul></ul><ul><ul><li>DROP ROUTE </li></ul></ul>New DDL BEGIN DIALOG CONVERSATION END CONVERSATION MOVE CONVERSATION GET SERVICE INSTANCE RECEIVE SEND BEGIN DIALOG TIMER New DML
    41. 42. Service Broker Impact on Performance Shipping as trigger during Order Shipping queued by trigger during Order
    42. 43. Service Broker <ul><li>Demo Scenario </li></ul>OrderService ShippingService AccountsService NewOrder
    43. 44. Service Broker
    44. 45. Call To Action <ul><li>Leverage SQL Server 2005 to connect with applications and partners using the ubiquitous XML format. </li></ul><ul><li>Be smart about building functionality using CLR in SQL Server. </li></ul><ul><li>Leverage the Service Broker to create loosely-coupled message queues for your applications. </li></ul>
    45. 46. Call To Action http://www.chrysalisevents.com/vs2005devcon/ Detroit – June 16
    46. 47. © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.