• Save
SQL Server Query Optimization, Execution and Debugging Query Performance
Upcoming SlideShare
Loading in...5
×
 

SQL Server Query Optimization, Execution and Debugging Query Performance

on

  • 4,233 views

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

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

Statistics

Views

Total Views
4,233
Views on SlideShare
4,226
Embed Views
7

Actions

Likes
3
Downloads
0
Comments
0

2 Embeds 7

http://www.slideshare.net 5
http://localhost 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

SQL Server Query Optimization, Execution and Debugging Query Performance SQL Server Query Optimization, Execution and Debugging Query Performance Presentation Transcript

  • 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
  • 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
  • Agenda
    Introduction
    SQL Server Optimizer
    Query Debugging Tools
    Estimated and Actual Query Plan
    Parameters and Query Plan
    Testing Considerations
    Hints and Other Workarounds
  • Agenda
    Introduction
    SQL Server Optimizer
    Query Debugging Tools
    Estimated and Actual Query Plan
    Parameters and Query Plan
    Testing Considerations
    Hints and Other Workarounds
  • SQL Server Relational ServerHigh-Level Architecture
    Language Processing (Parse/Bind)
    Query Optimization
    (Plan Generation, View Matching, Statistics, Costing)
    Statement/Batch Execution
    Query Execution
    (Query Operators, Memory Grants, Parallelism)
    Metadata, Type system, Expression Services
    Utilities (DBCC,
    Backup / Restore, BCP, …)
    Plan Cache Management
    Storage Engine (Access Methods, Database Page Cache, Locking, Transactions, …)
    SQL-OS (Schedulers, Buffer Pool, Memory Management, Synchronization Primitives, …)
    SQL Relational Engine
  • 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
  • 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
  • Agenda
    Introduction
    SQL Server Optimizer
    Query Debugging Tools
    Estimated and Actual Query Plan
    Parameters and Query Plan
    Testing Considerations
    Hints and Other Workarounds
  • Query Debugging Tools
    SSMS – Management Studio
    Showplan
    DMVs – Dynamic Management Views
    Trace
    Perfmon counters
  • Optimizer Perfmon Counters
    • 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
  • Trace
    Execution events may be result of query plan selection
    The most useful execution event categories
    Errors and Warnings (e.g. Exchange Spill or Sort Warning)
    Performance (e.g. Plan Guide Successful or Performance Statistics)
    Query Optimization
    Errors and Warnings – Missing Column Statistics and Missing Join Predicate
    Performance - 8 different Showplan Events; the most useful are
    Showplan XML for Query Compile – with each compile
    Showplan XML Statistics Profile – with each execution
  • 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.
  • Agenda
    Introduction
    SQL Server Optimizer
    Query Debugging Tools
    Estimated and Actual Query Plan
    Parameters and Query Plan
    Testing Considerations
    Hints and Other Workarounds
  • 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))
  • 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
  • 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”
  • Agenda
    Introduction
    SQL Server Optimizer
    Query Debugging Tools
    Estimated and Actual Query Plan
    Parameters and Query Plan
    Testing Considerations
    Hints and Other Workarounds
  • 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
  • Agenda
    Introduction
    SQL Server Optimizer
    Query Debugging Tools
    Estimated and Actual Query Plan
    Parameters and Query Plan
    Testing Considerations
    Hints and Other Workarounds
  • 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
  • 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…
  • Agenda
    Introduction
    SQL Server Optimizer
    Query Debugging Tools
    Estimated and Actual Query Plan
    Parameters and Query Plan
    Testing Considerations
    Hints and Other Workarounds
  • 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)
  • 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))
  • 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)
  • 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.
  • © 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.