480 likes | 696 Views
TL42. SQL Server 2008: Powering MSDN. Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com. Agenda SQL Server 2008: Powering MSDN. Introduction Challenge Solution Additional Uses Of The Solution Questions?. Introduction Within MSDN. MSDN.
E N D
TL42 SQL Server 2008: Powering MSDN Mark Johnston Development Lead - MSDN Microsoft Corporationjohnston.mark@microsoft.com
AgendaSQL Server 2008: Powering MSDN • Introduction • Challenge • Solution • Additional Uses Of The Solution • Questions?
IntroductionWithin MSDN MSDN This talk is only about this part of MSDN Web Sites Subscriptions MSDN Magazine Offline Help
IntroductionLogical Overview Content Providers Online Services MSDN • Visual Studio • SQL Server • Exchange • Windows • Office • KB Articles • … • Web Sites • Web Services • RSS Feeds • …
IntroductionMSDN Topology Overview SQL Servers IIS/Web Transactional Replication Content Providers Datacenter 1 Visual Studio SQL Server Exchange Office Windows KB Articles … Datacenter 2 All Content is Stored in SQL Server
IntroductionContent By The Numbers • Count of content stored in SQL Server: 12 million • Content providers peak submission of content per day: 1.25 million • Most rows replicated in one day: 30 million • On average each content is normalized into 40 rows per content • Average web server hits per day: 40 million
Agenda • Introduction • Challenge • Solution • Additional Uses Of The Solution • Questions?
ChallengeMinimize The Number of Rows That Are Replicated • Minimize the number of rows replicated when content is submitted to MSDN • Note that an update of a row where no values are changed will replicate the row
Agenda • Introduction • Challenge • Solution • Additional Uses Of The Solution • Questions?
SolutionAgenda • Describe two new features of SQL Server 2008 • MERGE statement • Table-Valued Parameters • Combine those two new features to implement the solution
MERGE StatementWhat Is It? • The MERGE statement is a new feature of SQL Server 2008 • It is a combination of INSERT, UPDATE, and/or DELETE in one statement So, you don’t have to write code like if not exists (select * from tbl where name = @name) insert tbl (…) select … else update tbl set … where name = @name
MERGE StatementCoding Example • MERGE employee as target • USINGemployee_changesassrc • ONsrc.loginid = target.loginid • WHENMATCHEDTHEN/* UPDATE */ • UPDATESET first = src.first • ,last = src.last • WHENNOT MATCHEDTHEN/* INSERT */ • INSERT (loginid,first,last) • VALUES(src.loginid,src.first,src.last) • WHENNOTMATCHEDBYSOURCETHEN • DELETE; /* DELETE */
MERGE StatementWhy use MERGE? • Performance • The row(s) are read only once • MERGE is atomic • Help avoid race conditions
MERGEDelta Processing Employee_changes “source” table Employee “Target“ Table Applied to This process is known as Delta Processing Employee “Target” result after MERGE UPDATE untouched INSERT DELETE
demo MERGE Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com
MERGE Phone Number DemoProblem Definition • Temp table #t holds a list of phone numbers for a given employee • Replace the employee’s phone numbers with the phone numbers in the temp table Create table #t (loginidvarchar(15) not null ,Type char(4) not null ,phoneNumvarchar(15) not null )
demo MERGE Phone Numbers Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com
MERGE Phone Numbers DEMO Problem #1: Error: target row matches more than one source row Source Target Need to fix the ON clause of the MERGE statement
MERGE Phone Numbers DEMO Problem #2: Inadvertent rows are being deleted Source Target UPDATE untouched INSERT DELETE DELETE Need to properly define the scope of the target
MERGE Phone Demo Summarized • ;WITH target • AS (SELECT ep.* FROMEmployeePhoneASep • JOIN (SELECT distinct loginId from #t) t • ON t.loginId = ep.loginId) • MERGE target • USING (SELECT * FROM #t) AS SRC • ONSRC.loginid = target.loginid • AND SRC.Type = target.type • WHENMATCHED • AND (target.phoneNum <> SRC.phoneNum) • THENUPDATESETtarget.phoneNum=SRC.phoneNum • WHENNOTMATCHEDTHEN • INSERT (loginid, type, phoneNum) • VALUES (SRC.loginid, SRC.type, SRC.phoneNum) • WHENNOTMATCHEDBYSOURCETHENDELETE; Untouched
MERGE Statement • VENN Diagrams Source Target Insert Delete Update Subset Subset Insert Update Delete
MERGE Statement SummaryKey Points and Takeaways • Define the scope of the source • Define the scope of the target to match the scope of the source • Define the ON clause between source and target • Add conditions to the “UPDATE” section to handle the “untouched” • MERGE statement can contain any combination of INSERT, UPDATE, and DELETE • Under the covers, MERGE uses FULL JOINS
Table-Valued ParametersDefinition • Table-Valued Parameters (TVP) are a new parameter type in SQL Server 2008 that enable multiple rows to be sent to a routine • TVP’s are like parameter arrays
Table-Valued ParametersBenefits of Table-Valued Parameters • Performance • XML is a bloated transport • Delimited lists have to be parsed • Calling a stored procedure many times is inefficient • Strongly typed • A given routine can accept zero or more TVP’s • Provides a simple programming model
demo TVP and C# Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com
Table-Valued Parameters What TVP Generated Code Looks Like • declare @p1 dbo.EmployeeTableType • insertinto @p1 values • (N’JohnA’,N’John’, N’Adams’) • declare @p2 dbo.EmployeePhoneTableType • insertinto @p2 values (N’JohnA’,N’cell’, N’1-555-456-7890’) • insertinto @p2 values (N’JohnA’,N’home’, N’1-555-425-8080’) • insertinto @p2 values (N’JohnA’,N’work’, N’1-555-112-1234’) • execdbo.mergeEmployeeAndPhoneWithTVP @employee=@p1,@employeePhone=@p2
Table-Valued ParametersKey Points and Takeaways • TVP’s and MERGE work well together • TVP’s are easy to use from C# • Any object derived from DbDataReader can stream rows of data to a TVP
SolutionOriginal MSDN Content Submission • execcontentInsertUpdate … • /*if update content then delete rows in • related tables associated with content */ • execcontentPrimaryDocInsert … • execcontentPrimaryDocInsert … • … • execcontentImageInsert … • execcontentImageInsert … • … • execcontentMetaDataInsert … • execcontentMetaDataInsert … • … • /* there are 10 content related tables */
SolutionNew Content Submission With MERGE and TVP • execcontentMerge • @contentInfo/* contentTableType */ • ,@primaryDoc/* primaryDocTableType */ • ,@images /* imageTableType */ • ,@metaData/* metaDataTableType */ • … • /* All data related to the content is passed into one stored procedure */
SolutionSummary • TVP’s and MERGE look great separately • Together they are intriguing…
Agenda • Introduction • Challenge • Solution • Additional Uses Of The Solution • Questions?
Additional Uses Of The Solution • Data Migration or Transfer Tool • Data Diff Tool • New Paradigm? • …
demo Transfer Tool Mark Johnston Development Lead - MSDN Microsoft Corporation johnston.mark@microsoft.com
Additional Uses Of The SolutionData Diff Tool • Data Diff Tool could be used to compare data across servers/databases • For instance, check if a specific employee on one database has the same data in another database • The implementation would be like the transfer tool but FULL JOINS would be used rather than MERGE
Additional Uses Of The SolutionNew Paradigm? Traditional Method for App Design • Each table has Insert, Update, and Delete stored procedures EmployeeInsert EmployeePhoneInsert Employee EmployeePhone EmployeeUpdate EmployeeDelete EmployeePhoneUpdate EmployeePhoneDelete
Additional Uses Of The SolutionNew Paradigm? • Each table has one MERGE stored procedure that takes a table-valued parameter EmployeeMerge EmployeePhoneMerge Employee EmployeePhone
Additional Uses Of The SolutionNew Paradigm? • Each logical entity has a stored procedure doing MERGE on all the related tables EmployeeMerge Employee EmployeePhone EmployeeAddress
Additional Uses Of The SolutionNew Paradigm? MSDN Implementation contentMerge content contentPrimaryDoc contentImage contentMetaData execcontentMerge @contentInfo/* contentTableType */ ,@primaryDoc/* primaryDocTableType */ ,@images /* imageTableType */ ,@metaData/* metaDataTableType */ …
Additional Uses Of The SolutionSummary One final thing… I provided ideas and possible solutions but the bottom line is… DESIGN TO YOUR NEEDS/REQUIREMENTS Consider the concepts a tool and appropriately use the tool for your needs
Resources MERGEMERGE (Transact-SQL)http://msdn.microsoft.com/library/bb510625.aspxOptimizing MERGE Statement Performance http://msdn.microsoft.com/library/cc879317.aspxInserting, Updating, and Deleting Data with MERGE http://msdn.microsoft.com/library/bb522522.aspx Table-Valued ParametersTable-Valued Parameters (Database Engine)http://msdn.microsoft.com/library/bb510489.aspxTable-Valued Parameters in SQL Server 2008 (ADO.NET)http://msdn.microsoft.com/library/bb675163.aspx
Questions? Mark Johnston Development Lead – MSDN Microsoft Corporation johnston.mark@microsoft.com
Evals & Recordings Please fill out your evaluation for this session at: This session will be available as a recording at: www.microsoftpdc.com
© 2008 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.
Appendix Mark Johnston Development Lead – MSDN Microsoft Corporation johnston.mark@microsoft.com
MERGECan you spot the race condition? • IF exists (select * from Product whereproductNumber = ‘AR-5381’) • BEGIN • update product • set … • whereproductNumber = ‘AR-5381’ • END • ELSE • BEGIN • INSERT product (…) • SELECT ‘AR-5381’, … • END Race condition
MERGEWays to avoid the race condition To avoid the race condition, the statement needs to be atomic with either • Use MERGE statement • Code up your own atomic statements INSERT Product (…) SELECT … WHERE NOT EXISTS (SELECT * FROM product WHERE partNumber = @partNumber ) UPDATE product SET …