SQL Server Query Optimization, Execution and Debugging Query Performance<br />Vinod Kumar M<br />Technology Evangelist – M...
Session Objectives And Takeaways<br />Takeaways (Learn…):  <br />why estimated row counts affect plan selection<br />how t...
Agenda	<br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />P...
Agenda	<br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />P...
SQL Server Relational ServerHigh-Level Architecture <br />Language Processing (Parse/Bind)<br />Query Optimization <br />(...
Query Optimization<br />Query Optimization is cost based selection of good enough (potentially not the best) Query Plan<br...
How is the plan cost determined?<br />Demo – watch query plan costs for two similar queries…<br />selectod.OrderID,od.Prod...
Agenda	<br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />P...
Query Debugging Tools<br />SSMS – Management Studio<br />Showplan<br />DMVs – Dynamic Management Views<br />Trace<br />Per...
Optimizer Perfmon Counters<br /><ul><li>Use “SQL Server:SQL Statistics” object counters:
Auto-Param Attempts/sec
Forced Parameterizations/sec
Guided Plan Executions/sec
SQL Compilations/sec
SQL Re-Compilations/sec </li></li></ul><li>Trace<br />Execution events may be result of query plan selection<br />The most...
DMVs<br />sys.dm_exec_query_stats<br />- returns performance statistics and SQL handle for each query plan in cache<br />s...
Agenda	<br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />P...
Demo – Stored Procedure<br />CREATE PROCEDURE myp @ShipRegion NVARCHAR(15)<br />as<br />SELECT o.Orderid,c.CompanyName,od....
Estimated and Actual Query Plan <br />Actual Query Plan contains additional information<br />Actual Number of Rows<br />Ac...
How to Obtain Actual Plan?<br />SSMS (as it was shown)<br />SQLTRACE<br />Showplan XML Statistics Profile event<br />SET S...
Agenda	<br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />P...
Two Different Days…<br />-- Day One – NULL query comes first<br />DBCC FREEPROCCACHE<br />EXECUTE myp NULL<br />EXECUTE my...
Agenda	<br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />P...
One Testing Scenario<br />A story…<br />Developer created parameterized stored proc<br />Tester developed a driver<br />Te...
Test Results<br />If the plans are equal (watch for plan reuse! Run DBCC FREEPROCCACHE) you are ok (most likely)<br />Else...
Agenda	<br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />P...
One Query Plan is Goodbut some parameter values may generate different plan<br />Workarounds <br />If optimize for “unknow...
Upcoming SlideShare
Loading in...5
×

SQL Server Query Optimization, Execution and Debugging Query Performance

3,864

Published on

SQL Server Query Execution Session at TechEd India 2010. Basic session on tuning ...

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,864
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

SQL Server Query Optimization, Execution and Debugging Query Performance

  1. 1.
  2. 2. SQL Server Query Optimization, Execution and Debugging Query Performance<br />Vinod Kumar M<br />Technology Evangelist – Microsoft<br />@vinodk_sql<br />www.ExtremeExperts.com<br />http://blogs.sqlxml.org/vinodkumar<br />
  3. 3. Session Objectives And Takeaways<br />Takeaways (Learn…): <br />why estimated row counts affect plan selection<br />how to find out “estimated” and “actual” row counts in the query plans<br />challenges of parameterized queries <br />how to influence query plan choice<br />tips on design and testing of parameterized queries and stored procedures<br />bunch of useful demos you can try on your own<br />
  4. 4. Agenda <br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />Parameters and Query Plan<br />Testing Considerations<br />Hints and Other Workarounds<br />
  5. 5. Agenda <br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />Parameters and Query Plan<br />Testing Considerations<br />Hints and Other Workarounds<br />
  6. 6. SQL Server Relational ServerHigh-Level Architecture <br />Language Processing (Parse/Bind)<br />Query Optimization <br />(Plan Generation, View Matching, Statistics, Costing)<br />Statement/Batch Execution<br />Query Execution<br />(Query Operators, Memory Grants, Parallelism)<br />Metadata, Type system, Expression Services<br />Utilities (DBCC, <br />Backup / Restore, BCP, …)<br />Plan Cache Management<br />Storage Engine (Access Methods, Database Page Cache, Locking, Transactions, …)<br />SQL-OS (Schedulers, Buffer Pool, Memory Management, Synchronization Primitives, …)<br />SQL Relational Engine<br />
  7. 7. Query Optimization<br />Query Optimization is cost based selection of good enough (potentially not the best) Query Plan<br />Input to Query Optimization is Operator Tree produced by<br />Parsing SQL statement (Syntax)<br />Semantic analysis and checking (Algebrizing – this includes type derivation, type checking, binding variables, table and column names, etc.)<br />Output is Query Plan<br />
  8. 8. How is the plan cost determined?<br />Demo – watch query plan costs for two similar queries…<br />selectod.OrderID,od.ProductID<br />FROM [Order Details] od<br />WHERE od.ProductID IN (9,15)<br />SELECT od.OrderID,od.ProductID<br />FROM [Order Details] od<br />WHERE od.ProductID IN (59,31)<br />Conclusions<br />SQL Server is using cardinality estimates to derive cost of operators<br />Cardinality estimates are derived from the statistics<br />
  9. 9. Agenda <br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />Parameters and Query Plan<br />Testing Considerations<br />Hints and Other Workarounds<br />
  10. 10. Query Debugging Tools<br />SSMS – Management Studio<br />Showplan<br />DMVs – Dynamic Management Views<br />Trace<br />Perfmon counters<br />
  11. 11. Optimizer Perfmon Counters<br /><ul><li>Use “SQL Server:SQL Statistics” object counters:
  12. 12. Auto-Param Attempts/sec
  13. 13. Forced Parameterizations/sec
  14. 14. Guided Plan Executions/sec
  15. 15. SQL Compilations/sec
  16. 16. SQL Re-Compilations/sec </li></li></ul><li>Trace<br />Execution events may be result of query plan selection<br />The most useful execution event categories <br />Errors and Warnings (e.g. Exchange Spill or Sort Warning)<br />Performance (e.g. Plan Guide Successful or Performance Statistics)<br />Query Optimization<br />Errors and Warnings – Missing Column Statistics and Missing Join Predicate<br />Performance - 8 different Showplan Events; the most useful are<br />Showplan XML for Query Compile – with each compile<br />Showplan XML Statistics Profile – with each execution<br />
  17. 17. DMVs<br />sys.dm_exec_query_stats<br />- returns performance statistics and SQL handle for each query plan in cache<br />sys.dm_exec_sql_text<br />- Returns the text of the sql statement based on the SQL handle <br />sys.dm_exec_query_plan<br />- Returns the showplan in XML format for a batch or module based on the SQL handle<br />sys.dm_exec_plan_attributes<br />– TVF returns one row per plan attribute for the plan specified by the plan handle. <br />
  18. 18. Agenda <br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />Parameters and Query Plan<br />Testing Considerations<br />Hints and Other Workarounds<br />
  19. 19. Demo – Stored Procedure<br />CREATE PROCEDURE myp @ShipRegion NVARCHAR(15)<br />as<br />SELECT o.Orderid,c.CompanyName,od.Quantity<br />FROM Orders o, [Order Details] od,Customers c <br />WHERE <br />o.Orderid=od.Orderid AND<br />o.CustomerID=c.CustomerID AND<br /> ((o.ShipRegion=@ShipRegion) OR <br /> (@ShipRegion is NULL) AND <br /> (o.ShipRegion is NULL))<br />
  20. 20. Estimated and Actual Query Plan <br />Actual Query Plan contains additional information<br />Actual Number of Rows<br />Actual Rebinds and Rewinds<br />Number of Executions<br />Parameter values (compile time and runtime)<br />Estimated rowcount is per single execution<br />When comparing the estimated and actual rowcounts multiply the estimated rowcountby estimated number of executions<br />Sometimes (rarely!) the shape of the Actual Query Plan may differ from the Estimated<br />
  21. 21. How to Obtain Actual Plan?<br />SSMS (as it was shown)<br />SQLTRACE<br />Showplan XML Statistics Profile event<br />SET STATISTICS XML ON<br />Returns the Actual Plan in XML format following the result<br />SET STATISTICS PROFILE ON<br />Returns text format of the query plan with two leading columns: ROWS and EXECUTES<br />Using DMV<br />But does not contain “actual” row counts or “actual parameter values”<br />
  22. 22. Agenda <br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />Parameters and Query Plan<br />Testing Considerations<br />Hints and Other Workarounds<br />
  23. 23. Two Different Days…<br />-- Day One – NULL query comes first<br />DBCC FREEPROCCACHE<br />EXECUTE myp NULL<br />EXECUTE myp 'BC'<br />-- Day Two – a BC query comes first<br />DBCC FREEPROCCACHE<br />EXECUTE myp 'BC'<br />EXECUTE myp NULL<br />Observe:<br />Query plan is reused for all parameter values if found in the cache<br />If used for “different” value the discrepancies between “estimated” and “actual” row counts are higher<br />
  24. 24. Agenda <br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />Parameters and Query Plan<br />Testing Considerations<br />Hints and Other Workarounds<br />
  25. 25. One Testing Scenario<br />A story…<br />Developer created parameterized stored proc<br />Tester developed a driver<br />Tests were successful and sp moved to production <br />After 4 weeks w/out any problems the sp now takes forever to finish and blocks the production<br />Disaster declared, all hands…<br />A likely cause:The tests were run always with the same query plan created for the first set of parameter values<br />
  26. 26. Test Results<br />If the plans are equal (watch for plan reuse! Run DBCC FREEPROCCACHE) you are ok (most likely)<br />Else it still may be ok to use one of the generated plans for all parameter values (even those that would not pick the plan)<br />Else you need to use more than one plan to get desirable performance…<br />
  27. 27. Agenda <br />Introduction<br />SQL Server Optimizer<br />Query Debugging Tools<br />Estimated and Actual Query Plan<br />Parameters and Query Plan<br />Testing Considerations<br />Hints and Other Workarounds<br />
  28. 28. One Query Plan is Goodbut some parameter values may generate different plan<br />Workarounds <br />If optimize for “unknown” gives desired plan -> use OPTION (OPTIMIZE FOR UNKNOWN)<br />If optimization for a specific value produces the plan -> use OPTION (OPTIMIZE FOR (@ShipRegion=‘BC’))<br />Use Plan Guides to enforce the same plan<br />Rewrite the sp and “hide” the passed in parameter (example follows)<br />
  29. 29. Hiding Parameter Value<br />ALTER PROCEDURE myp @ShipRegion NVARCHAR(15)<br />as<br />DECLARE @LocalShipRegion NVARCHAR (15)<br />SET @LocalShipRegion=@ShipRegion<br />SELECT o.Orderid,c.CompanyName,od.Quantity<br />FROM Orders o, [Order Details] od,Customers c <br />WHERE <br />o.Orderid=od.Orderid AND<br />o.CustomerID=c.CustomerID AND<br /> ((o.ShipRegion=@LocalShipRegion) OR <br /> (@LocalShipRegionis NULL) and (o.ShipRegion is NULL))<br />
  30. 30. Session Takeaways<br />Takeaways (We have learned…): <br />why estimated row counts affect plan selection (cost)<br />how to find out “estimated” and “actual” row counts in the query plans (tools)<br />challenges of parameterized queries (query plan reuse)<br />how to influence query plan choice(sp rewrite, hints, plan guides)<br />tips on design and testing of parameterized queries and stored procedures (several)<br />
  31. 31. References<br />http://blogs.msdn.com/sqltips/archive/2005/10/05/Top-N-costly-query-plans.aspxUmachandar’s blog “Find Top N costly query plans in adhoc batches or modules...” has some great DMV queries. You can track various times including cumulative time of many executions of the same statement or stored procedure. UC has example how to use the attributes in the XML showplan to correlate the plan costs with actual execution times. <br />http://msdn.microsoft.com/en-us/library/dd672789.aspx Troubleshooting Performance Problems in SQL Server 2008 – white paper produced by MS developers and CSS. The paper is addressing most common performance problems as encountered by the CSS team and it is covering tools new in 2008.<br />
  32. 32. © 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.<br />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.<br />

×