Gail Shaw XpertEase DAT 305 Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use

Embed Size (px)

DESCRIPTION

Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use

Citation preview

Gail Shaw XpertEase DAT 305 Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use Background information Optimal index usage Minimal sorting Appropriate joins What makes a good plan Three different types of cached plan Procedure Ad-hoc Parameterised SQL tries as much as possible to reuse plans Plan reuse is not always good Many reasons for SQL to chose to recompile It is possible to force a recompile Background information Plan caching basics Query Hints Hints can be use to override the Query Optimiser In most cases, the optimiser knows best When using hints Use the one that has the smallest effect Test, test, test, test and retest Hints do not reduce compile time Overview Table hints Index hints Locking hints Join hints Query hints Recompile Optimise for MaxDop Use Plan Query Hints Types of hints Plan Cache Metadata Four main DMVs Sys.dm_exec_cached_plans Sys.dm_exec_query_stats Sys.dm_exec_sql_text Sys.dm_exec_query_plan Plan Cache Metadata Aggregate info is stored in sys.dm_exec_query_stats One row per statement in the batch/object Plan handle and sql handle are for entire batch New columns in SQL Server 2008 allow you to find similar queries and similar plans: query_hash query_plan_hash Plan Details Example: Retrieve query text -- Retrieve the text of all queries in cache SELECT st.text FROM sys.dm_exec_cached_plans cp INNER JOIN sys.dm_exec_query_stats qs on cp.plan_handle = qs.plan_handle CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st Example: Retrieve query plan -- Retrieve the execution plans of all queries in cache SELECT qp.query_plan FROM sys.dm_exec_cached_plans cp INNER JOIN sys.dm_exec_query_stats qs on cp.plan_handle = qs.plan_handle CROSS APPLY sys.dm_exec_query_plan(qs.sql_handle) qp Examining the plan cache Plan Guides For when hints are needed, but the query cannot be modified Three types of guide SQL Object Template Creating a Plan Guide = = N'SELECT * FROM Sales.Customer AS c INNER JOIN Sales.SalesTerritory t ON c.CustomerID = t.CustomerID WHERE= = = = N'OPTION ( OPTIMIZE FOR = N''US'') )' Plan Guides Apply to individual T-SQL batches and statements Statement must match exactly. Whitespace and case must match SQL plan guides For SQL statements that are part of T-SQL procedures, functions or triggers Text is parsed and normalised before matching Case for Keywords is not important Whitespace is ignored Plan Guides Object plan guides Same text matching rules as the Object plan guide Only accepts the parameterisation hint Requires that the statement be a parameterised template Plan Guides Template plan guides Creating a Template Plan GuideOUTPUT; EXEC = = == N'OPTION(PARAMETERIZATION FORCED)' Plan Guides Only query hints can be used in plan guides No table hints No join hints In SQL 2008, most of the table hints can be expressed as query hints The parameterisation hints can only be applied in a plan guide Query hints and plan guides Creating and Using Plan guides Plan Guide Metadata sp_create_plan_guide sp_control_plan_guide sys.plan_guides Script plan guide from SSMS (new in 2008) Plan freezing New in SQL 2008 The ability to create a plan guide from an existing plan in cache Plan Freezing SELECT City, StateProvinceID, PostalCode FROM Person.Address WHERE ; SELECT plan_handle FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st WHERE st.text LIKE N'SELECT City,%' EXEC = = 0x F19B1E1FC0A14C0A Freezing a plan Monitoring Plan Guides SHOWPLAN_XML Validation Function Tracing Successful and Unsuccessful Plan Guide Use events Captured at Compile Time PerfMon Successful and Unsuccessful Plan Guide Use events Captured at Execution Time Tools sys.fn_validate_plan_guide(Plan_guide_id) The statement or object associated with the plan is compiled with the plan hint If there are any errors, a resultset is returned with message and severity Monitoring Plan Guides Validation Monitoring Plan Guides Profiler Monitoring Plan Guides Performance Monitor Validating and monitoring plan guides Best Practices Let the optimizer do its job If the optimizer cant find a good plan, try to figure out why not Use the least invasive hint possible Least invasive: RECOMPILE Most invasive: USE PLAN Consider implementing all hints through plan guides So they can be switched off and on as needed International Content & CommunityResources for IT ProfessionalsResources for DevelopersMicrosoft Certification & Training Resources Resources Required Slide Speakers, TechEd 2009 is not producing a DVD. Please announce that attendees can access session recordings from Tech-Ed website. These will only be available after the event. Required Slide Speakers, TechEd 2009 is not producing a DVD. Please announce that attendees can access session recordings from Tech-Ed website. These will only be available after the event. Tech Ed Africa 2009 sessions will be made available for download the week after the event from: Related Content Microsoft SQL Server 2008: Performance Profiling and Troubleshooting with Extended Events (DAT401) Required Slide Speakers, please list the Breakout Sessions, TLC Interactive Theaters and Labs that are related to your session. Any queries, please check with your Track Owner. Required Slide Speakers, please list the Breakout Sessions, TLC Interactive Theaters and Labs that are related to your session. Any queries, please check with your Track Owner. Track Resources Blog series on Execution Plans:"SQL Server Execution plans" by Grant Fritchey:SQL Server 2008 Query Performance Tuning Distilled by Grant Fritchey and Sajal Dam Required Slide Track Owners to provide guidance. Please address any queries to your track owners. Required Slide Track Owners to provide guidance. Please address any queries to your track owners. South African SQL Server Usergroup Meet monthly, 3 rd Tuesday evening at Microsofts Offices in Bryanston Current leads: Paul Filmalter and Gail Shaw Required Slide Complete a session evaluation and enter to win! 10 pairs of MP3 sunglasses to be won 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. Required Slide