Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Gail Shaw XpertEase DAT 305 Topics Background Information Query Hints Plan Cache Metadata Plan Guides Plan Freezing Monitoring Plan Guide Use."— Presentation transcript:

1

2 Gail Shaw XpertEase DAT 305

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

4 Background information Optimal index usage Minimal sorting Appropriate joins What makes a good plan

5 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

6 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

7 Table hints Index hints Locking hints Join hints Query hints Recompile Optimise for MaxDop Use Plan Query Hints Types of hints

8 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

9 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

10 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

11 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

12 Examining the plan cache

13 Plan Guides For when hints are needed, but the query cannot be modified Three types of guide SQL Object Template

14 Creating a Plan Guide sp_create_plan_guide @name = N'Guide1', @stmt = N'SELECT * FROM Sales.Customer AS c INNER JOIN Sales.SalesTerritory t ON c.CustomerID = t.CustomerID WHERE CountryRegionCode = @Country_region', @type = N'OBJECT', @module_or_batch = N'Sales.GetSalesOrderByCountry', @params = NULL, @hints = N'OPTION ( OPTIMIZE FOR (@Country_region = N''US'') )'

15 Plan Guides Apply to individual T-SQL batches and statements Statement must match exactly. Whitespace and case must match SQL plan guides

16 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

17 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

18 Creating a Template Plan Guide EXEC sp_get_query_template @querytext, @templatetext OUTPUT, @parameters OUTPUT; EXEC sp_create_plan_guide @name = 'Guide1', @stmt = @templatetext, @type = N'Template', @module_or_batch = NULL, @parameters, @hints = N'OPTION(PARAMETERIZATION FORCED)'

19 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

20 Creating and Using Plan guides

21 Plan Guide Metadata sp_create_plan_guide sp_control_plan_guide sys.plan_guides Script plan guide from SSMS (new in 2008)

22 Plan freezing New in SQL 2008 The ability to create a plan guide from an existing plan in cache

23 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 sp_create_plan_guide_from_handle @name = N'Guide1_from_XML_showplan', @plan_handle = 0x06000600F19B1E1FC0A14C0A000000000000000000000000

24 Freezing a plan

25 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

26 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

27 Monitoring Plan Guides Profiler

28 Monitoring Plan Guides Performance Monitor

29 Validating and monitoring plan guides

30 Best Practices Let the optimizer do its job If the optimizer can’t 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…

31 www.microsoft.com/teched International Content & Community http://microsoft.com/technet Resources for IT Professionals http://microsoft.com/msdn Resources for Developers www.microsoft.com/learning Microsoft 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: www.tech-ed.co.zawww.tech-ed.co.za

32 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.

33 Track Resources Blog series on Execution Plans: http://sqlinthewild.co.za/index.php/2007/08/20/reading-execution-plans/ "SQL Server Execution plans" by Grant Fritchey: http://www.sqlservercentral.com/articles/books/65831/ “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.

34

35 South African SQL Server Usergroup Meet monthly, 3 rd Tuesday evening at Microsoft’s Offices in Bryanston Current leads: Paul Filmalter and Gail Shaw

36 Required Slide Complete a session evaluation and enter to win! 10 pairs of MP3 sunglasses to be won

37 © 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


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

Similar presentations


Ads by Google