SQL Server Query Optimization, Execution and Debugging Query Performance
1.
2. SQL Server Query Optimization, Execution and Debugging Query Performance Vinod Kumar M Technology Evangelist – Microsoft @vinodk_sql www.ExtremeExperts.com http://blogs.sqlxml.org/vinodkumar
3. Session Objectives And Takeaways Takeaways (Learn…): why estimated row counts affect plan selection how to find out “estimated” and “actual” row counts in the query plans challenges of parameterized queries how to influence query plan choice tips on design and testing of parameterized queries and stored procedures bunch of useful demos you can try on your own
4. Agenda Introduction SQL Server Optimizer Query Debugging Tools Estimated and Actual Query Plan Parameters and Query Plan Testing Considerations Hints and Other Workarounds
5. Agenda Introduction SQL Server Optimizer Query Debugging Tools Estimated and Actual Query Plan Parameters and Query Plan Testing Considerations Hints and Other Workarounds
7. Query Optimization Query Optimization is cost based selection of good enough (potentially not the best) Query Plan Input to Query Optimization is Operator Tree produced by Parsing SQL statement (Syntax) Semantic analysis and checking (Algebrizing – this includes type derivation, type checking, binding variables, table and column names, etc.) Output is Query Plan
8. How is the plan cost determined? Demo – watch query plan costs for two similar queries… selectod.OrderID,od.ProductID FROM [Order Details] od WHERE od.ProductID IN (9,15) SELECT od.OrderID,od.ProductID FROM [Order Details] od WHERE od.ProductID IN (59,31) Conclusions SQL Server is using cardinality estimates to derive cost of operators Cardinality estimates are derived from the statistics
9. Agenda Introduction SQL Server Optimizer Query Debugging Tools Estimated and Actual Query Plan Parameters and Query Plan Testing Considerations Hints and Other Workarounds
17. DMVs sys.dm_exec_query_stats - returns performance statistics and SQL handle for each query plan in cache sys.dm_exec_sql_text - Returns the text of the sql statement based on the SQL handle sys.dm_exec_query_plan - Returns the showplan in XML format for a batch or module based on the SQL handle sys.dm_exec_plan_attributes – TVF returns one row per plan attribute for the plan specified by the plan handle.
18. Agenda Introduction SQL Server Optimizer Query Debugging Tools Estimated and Actual Query Plan Parameters and Query Plan Testing Considerations Hints and Other Workarounds
19. Demo – Stored Procedure CREATE PROCEDURE myp @ShipRegion NVARCHAR(15) as SELECT o.Orderid,c.CompanyName,od.Quantity FROM Orders o, [Order Details] od,Customers c WHERE o.Orderid=od.Orderid AND o.CustomerID=c.CustomerID AND ((o.ShipRegion=@ShipRegion) OR (@ShipRegion is NULL) AND (o.ShipRegion is NULL))
20. Estimated and Actual Query Plan Actual Query Plan contains additional information Actual Number of Rows Actual Rebinds and Rewinds Number of Executions Parameter values (compile time and runtime) Estimated rowcount is per single execution When comparing the estimated and actual rowcounts multiply the estimated rowcountby estimated number of executions Sometimes (rarely!) the shape of the Actual Query Plan may differ from the Estimated
21. How to Obtain Actual Plan? SSMS (as it was shown) SQLTRACE Showplan XML Statistics Profile event SET STATISTICS XML ON Returns the Actual Plan in XML format following the result SET STATISTICS PROFILE ON Returns text format of the query plan with two leading columns: ROWS and EXECUTES Using DMV But does not contain “actual” row counts or “actual parameter values”
22. Agenda Introduction SQL Server Optimizer Query Debugging Tools Estimated and Actual Query Plan Parameters and Query Plan Testing Considerations Hints and Other Workarounds
23. Two Different Days… -- Day One – NULL query comes first DBCC FREEPROCCACHE EXECUTE myp NULL EXECUTE myp 'BC' -- Day Two – a BC query comes first DBCC FREEPROCCACHE EXECUTE myp 'BC' EXECUTE myp NULL Observe: Query plan is reused for all parameter values if found in the cache If used for “different” value the discrepancies between “estimated” and “actual” row counts are higher
24. Agenda Introduction SQL Server Optimizer Query Debugging Tools Estimated and Actual Query Plan Parameters and Query Plan Testing Considerations Hints and Other Workarounds
25. One Testing Scenario A story… Developer created parameterized stored proc Tester developed a driver Tests were successful and sp moved to production After 4 weeks w/out any problems the sp now takes forever to finish and blocks the production Disaster declared, all hands… A likely cause:The tests were run always with the same query plan created for the first set of parameter values
26. Test Results If the plans are equal (watch for plan reuse! Run DBCC FREEPROCCACHE) you are ok (most likely) 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) Else you need to use more than one plan to get desirable performance…
27. Agenda Introduction SQL Server Optimizer Query Debugging Tools Estimated and Actual Query Plan Parameters and Query Plan Testing Considerations Hints and Other Workarounds
28. One Query Plan is Goodbut some parameter values may generate different plan Workarounds If optimize for “unknown” gives desired plan -> use OPTION (OPTIMIZE FOR UNKNOWN) If optimization for a specific value produces the plan -> use OPTION (OPTIMIZE FOR (@ShipRegion=‘BC’)) Use Plan Guides to enforce the same plan Rewrite the sp and “hide” the passed in parameter (example follows)
29. Hiding Parameter Value ALTER PROCEDURE myp @ShipRegion NVARCHAR(15) as DECLARE @LocalShipRegion NVARCHAR (15) SET @LocalShipRegion=@ShipRegion SELECT o.Orderid,c.CompanyName,od.Quantity FROM Orders o, [Order Details] od,Customers c WHERE o.Orderid=od.Orderid AND o.CustomerID=c.CustomerID AND ((o.ShipRegion=@LocalShipRegion) OR (@LocalShipRegionis NULL) and (o.ShipRegion is NULL))
30. Session Takeaways Takeaways (We have learned…): why estimated row counts affect plan selection (cost) how to find out “estimated” and “actual” row counts in the query plans (tools) challenges of parameterized queries (query plan reuse) how to influence query plan choice(sp rewrite, hints, plan guides) tips on design and testing of parameterized queries and stored procedures (several)
31. References 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. 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.