Presentation is loading. Please wait.

Presentation is loading. Please wait.

Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE

Similar presentations


Presentation on theme: "Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE"— Presentation transcript:

1

2 Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE
Michael Rys Principal Program Manager Lead Microsoft Corporation Session Code: DAT 406

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

4 Passing a set of data to SQL Server
N rows = N executed statements N rows = 1 executed statement

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

6 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 2100)

7 Examples In the examples we will be passing a set of items to the database for storage Example - “Store the following 1000 items” Examples will use Stored Procedures C# & ADO.NET

8 Pass the data as a delimited list
// C# cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = …|…|… …|…|…"); cmd.Execute…; -- What happens on the server? EXEC = '…|…|… …|…|…';

9 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 created one TVF per “list type”

10 Pass the data as XML // C#
cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Test.spXML"; doc.OuterXml); cmd.Execute…; -- What happens on the server?: EXEC = N'<Orders><Order…

11 Pass the data as XML Pros Cons
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

12 Pass the data as a Table Valued Parameter
// C# cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Test.spTVP"; var p = SqlDbType.Structured); p.TypeName = "Test.OrderTableType"; p.Value = dataTable; cmd.Execute…; -- What happens on the server?: Test.OrderTableType; EXEC

13 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

14 Table Valued Parameters
Similar to Table Valued Variables Live in tempdb No limit on numbers of rows Parameter sniffing will get table statistics! Limitations No sparse columns No indexes (except through constraints)

15 Pass the data as a Table Valued Parameter  Streaming
// C# class MyStreamingTvp : IEnumerable<SqlDataRecord> { … } … cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Test.spTVP"; var p = SqlDbType.Structured); p.TypeName = "Test.OrderTableType"; p.Value = new MyStreamingTvp(…); cmd.Execute…; -- What happens on the server?: Test.OrderTableType; EXEC

16 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

17 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”

18 What happens? And what about performance?
Initial parsing of the data on the Server Querying the data Insert the data into a table

19 1. Initial parsing on the Server

20 1. Initial parsing on the Server

21 2. Querying the data

22 2. Querying the data

23 3. Insert the arguments into a table

24 3. Insert the data into a table

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

26 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!

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

28 Adding MERGE to the equation
MERGE Test.Orders AS o AS v ON v.OrderId = o.OrderId WHEN MATCHED THEN UPDATE SET CustomerId = v.CustomerId ,OrderDate = v.OrderDate ,DueDate = v.DueDate WHEN NOT MATCHED BY SOURCE THEN DELETE WHEN NOT MATCHED THEN INSERT (OrderId, CustomerId, OrderDate) VALUES(v.OrderId, v.CustomerId, v.OrderDate);

29 question & answer Meet me in the Ask-the-Experts pavilion!

30 Resources Required Slide Speakers, www.microsoft.com/teched
TechEd 2009 is not producing a DVD. Please announce that attendees can access session recordings at TechEd Online. Resources Sessions On-Demand & Community Microsoft Certification & Training Resources Resources for IT Professionals Resources for Developers

31 Complete an evaluation on CommNet and enter to win an Xbox 360 Elite!

32 Required Slide © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.


Download ppt "Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE"

Similar presentations


Ads by Google