Optimizing Microsoft SQL Server 2008 Applications Using Table


Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE Tobias Ternstrom Program Mgr. , SQL Server Engine Microsoft Corporation DAT 320

Agenda Passing a set of data to SQL Server Adding MERGE to the equation

Passing a Set of Data to SQL Server N rows = N executed statements N rows = 1 executed statement

Passing a Set of Data to SQL Server N rows = N executed statements One client server roundtrip per execution All executions in one batch

Passing a Set of Data to SQL Server N rows = 1 executed statement Pass the data as a delimited list Pass the data as XML Pass the data as Table Valued Parameter Other options Managed bulk copy to a table Pass data as separate arguments (current limit is 2, 100)

Examples In the examples, we will be passing a set of items to the database for storage Example – “Store the following 1, 000 items” Examples we’ll use: Stored Procedures C# & ADO. NET

Pass the Data as a Delimited List // C# cmd. Command. Type = Command. Type. Stored. Procedure; cmd. Command. Text = "Test. sp. Delimited. String"; cmd. Parameters. Add. With. Value("@Values", @"…|…|…"); cmd. Execute…; -- What happens on the server? EXEC Test. sp. Delimited. String @Values = '…|…|…';

Pass the Data as a Delimited List To get the best performance, we need to use a SQLCLR Table Valued Function Pros: Performance is good No exposure to SQL Injection Cons: Requires SQLCLR to be enabled on the instance The set of data is not strongly typed Cumbersome implementation Can be simplified by creating one TVF per “list type”

Pass the Data as XML // C# cmd. Command. Type = Command. Type. Stored. Procedure; cmd. Command. Text = "Test. sp. XML"; cmd. Parameters. Add. With. Value("@Values", doc. Outer. Xml); cmd. Execute…; -- What happens on the server? : EXEC Test. sp. XML @Values = N'<Orders><Order…

Pass the Data as XML Pros Strongly typed (if you use an XML Schema Collection) Performance is OK No exposure to SQL Injection A very good option if your data is already XML! Great flexibility; remember XML allows for hierarchies Cons Performance is good but not the best Requires knowledge about XML Less cumbersome than the delimited list but still somewhat cumbersome

Pass the Data as a Table Valued Parameter // C# cmd. Command. Type = Command. Type. Stored. Procedure; cmd. Command. Text = "Test. sp. TVP"; var p = cmd. Parameters. Add("@Values", Sql. Db. Type. Structu red); p. Type. Name = "Test. Order. Table. Type"; p. Value = data. Table; cmd. Execute…; -- What happens on the server? : DECLARE @Values Test. Order. Table. Type; INSERT @Values … EXEC Test. sp. TVP @Values = @Values;

Pass the Data as Table Valued Parameter Pros Strongly typed No exposure to SQL Injection Performance is great! Very easy to use, both on client and server side Cons Less flexible than XML; may require you to pass multiple TVPs where one XML parameter would have been enough Allows for streaming but only to the server

Pass the Data as a Table Valued Parameter Streaming // C# class My. Streaming. Tvp : IEnumerable<Sql. Data. Record> { … } … cmd. Command. Type = Command. Type. Stored. Procedure; cmd. Command. Text = "Test. sp. TVP"; var p = cmd. Parameters. Add("@Values", Sql. Db. Type. Structured); p. Type. Name = "Test. Order. Table. Type"; p. Value = new My. Streaming. Tvp(…); cmd. Execute…; -- What happens on the server? : DECLARE @Values Test. Order. Table. Type; INSERT @Values … EXEC Test. sp. TVP @Values = @Values;

Pass the Data as a Table Valued Parameter Streaming Pros No need for staging the data in memory on the client side Cons Doesn’t stream all the way, stages the data on the server side Requires a type to handle the streaming

A Few More Words on Streaming If you stream, how “far” do you stream? N rows = N client server round trips & N proc. executions Streams “all” the way to the destination table Streaming TVP Streams from client to just before the procedure begins execution, i. e. , stages the data on the server side The rest Stages the data both on the client and server side Any solution can implement streaming “manually”

What Happens? And What About Performance? Initial parsing of the data on the server Querying the data Insert the data into a table

1. Initial Parsing on the Server

1. Initial Parsing on the Server Data passed as delimited string Data passed as XML Data passed as TVP 266 19 32 Execution time (ms. , lower is better)

2. Querying the Data

2. Querying the Data passed as delimited string Data passed as XML Data passed as TVP 969 307 33 Execution time (ms. , lower is better)

3. Insert the Arguments into a Table

3. Insert the Data into a Table Data passed as delimited string Data passed as XML Data passed as TVP 1, 074 412 88 Execution time (ms. , lower is better)

Agenda Passing a set of data to SQL Server Adding MERGE to the equation

Adding MERGE to the Equation Also referred to as UPSERT Allows for inserting, updating and deleting data in one statement It is part of ANSI …with one addition!

Adding MERGE to the Equation Events MATCHED NOT MATCHED BY SOURCE Type of event $action

Adding MERGE to the Equation MERGE Test. Orders AS o USING @Values AS v ON v. Order. Id = o. Order. Id WHEN MATCHED THEN UPDATE SET Customer. Id = v. Customer. Id , Order. Date = v. Order. Date , Due. Date = v. Due. Date WHEN NOT MATCHED BY SOURCE THEN DELETE WHEN NOT MATCHED THEN INSERT (Order. Id, Customer. Id, Order. Date) VALUES(v. Order. Id, v. Customer. Id, v. Order. Date);

question & answer

Resources www. microsoft. com/teched www. microsoft. com/learning Sessions On-Demand & Community Microsoft Certification & Training Resources http: //microsoft. com/technet http: //microsoft. com/msdn Resources for IT Professionals Resources for Developers www. microsoft. com/learning Microsoft Certification and Training Resources

Related Content DAT 313 Inside T-SQL: Enhancements, Techniques, Tips & Tricks DAT 305 Best Practices for Exception Handling and Defensive Programming in Microsoft SQL Server DAT 04 -INT Using the HIERARCHYID Datatype in Microsoft SQL Server 2008 to Maintain and Query Hierarchies 300 - Advanced, Database Platform, Developer Tools, Languages and Frameworks, Hands -on Lab, Middle Tier Platform and Tools

SQL Server Community Resources The Professional Association for SQL Server (PASS) is an independent, not-for-profit association, dedicated to supporting, educating, and promoting the Microsoft SQL Server community. • Connect: Local Chapters, Special Interest Groups, Online Community • Share: PASSPort Social Networking, Community Connection Event • Learn: PASS Summit Annual Conference, Technical Articles, Webcasts a FREE Member: www. sqlpass. org/Registerfor. SQLPASS. aspx • More. Become about the PASS organization Learn more about the PASS organization www. sqlpass. org/ Additional Community Resources SQL Server Community Center www. microsoft. com/sqlserver/2008/en/us/community-center. aspx Tech. Net Community for IT Professionals http: //technet. microsoft. com/en-us/sqlserver/bb 671048. aspx Developer Center http: //msdn. microsoft. com/en-us/sqlserver/bb 671064. aspx SQL Server 2008 Learning Portal http: //www. microsoft. com/learning/sql/2008/default. mspx

Additional Resources • • Team Forum: Speaker URL #2 Other: Speaker URL #3 External Resources TVPs http: //msdn. microsoft. com/en-us/library/bb 510489. aspx MERGE http: //technet. microsoft. com/enus/library/bb 510625. aspx SQL Server 2008 Business Value Calculator: www. moresqlserver. com

Complete an evaluation on Comm. Net and enter to win!

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U. S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
- Slides: 34