Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... ·...

29

Transcript of Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... ·...

Page 1: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted
Page 2: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Amit BansalCTOeDominer Systems P Ltd | Peopleware India

Page 3: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Who is Amit Bansal?

CTO, eDominer Systems & Peopleware India

Conducted more than 200 workshops on SQL Server & BI for top notch IT companies world wide

Microsoft MVP for SQL Server

Microsoft Certified Trainer Advisory Council member

Speaker at TechED India, TechED US & TechED Europe

Technical Reviewer – MSL courses on SQL Server

SME – SQL Server 2008 certifications

UG Lead (Delhi NCR) – Culminis

Manager – www.WeTogether.in, www.DelhiDevs.com, www.BlogBoard.in

Page 4: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Agenda & Session takeaways

Query Optimizer characteristics

Using Query & Table Hints

Plan Freezing concept

Scenarios from the wild

What are Plan Guides & different types

Implementing Plan Guides

Managing & Validating Plan Guides

Plan Guide considerations

Summary

Page 5: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Query Optimizer characteristics

Cost based optimization

Does the Query Optimizer always do a fine job?Does it always select the best query plan?

Use Hints to affect the query plan selectionUse Hints as a last resort, Use with caution

To be used only by experienced DBDs & DBAs

Is there really a problem?Check if there are other issues

Identify the real cause of a poor plan

What if SQL Server fails to generate a valid plan?

(error 8622 is raised)

Page 6: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Join, Query & Table Hints

FAST N Hint

RECOMPILE

OPTIMIZE FOR Hint

OPTIMIZE FOR UNKNOWN Hint

JOIN Hints

FORCE ORDER Hint

INDEX Hint

FORCESEEK Hint

READPAST Hint

USE PLAN Hint

Functionality

Index Hints

Join Hints

Parallelism

Locking

Compilation

Table Hints

….

Page 7: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Plan Freezing concepts

Forcing / Locking down a query plan

Provides greater query performance stability & predictability

Enables organizations to promote stable query plansHardware Server replacements

Server upgrades

Production Deployments

Plan Freezing canOptimize query performance

Copy query plans between servers

Prevent the plan cache from growing excessively with large compiled plans that are not reused

Page 8: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Scenarios from the wild…

A query started mis-behaving after SQL Server upgrade

Query text cannot be modified to use hints

Eg: Application/vendor code, ISV, etc

Query performance degraded after hardware upgrade

You need to copy plans from one server to the other

Overcoming ‘Parameter Sniffing’ issues

After a service pack upgrade – “my query just starting running 4 times slower today and the plan is different from yesterday” ……sigh

Page 9: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

What are Plan Guides?

Plan Freezing concept is implemented with Plan Guides

A DB object that associates a set of query hints with the query

Can be created using:

sp_create_plan_guide

sp_create_plan_guide_from_handle

Fixed query plan (XML Showplan output) can be also be applied

Available in Standard, Enterprise, Evaluation & Developer edition

Page 10: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Types of Plan Guides

Object Plan Guidesmatches queries that execute in the context of a SQL Server object (SP, UDF, DML triggers etc)

SQL Plan Guidesmatches queries that execute in the context of stand-alone Transact-SQL statements and batches ( not part of a database object)

Template Plan Guidesmatches stand-alone queries that parameterize to a specified form

To override the current PARAMETERIZATION setting

Page 11: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Object Plan Guides

You are interested in a T-SQL statement appearing in the context of a SQL Server object

SQL Server object can be:

Stored Procedure

User Defined Function (Scalar)

Multi-statement table-valued user-defined functions

DML Triggers

Page 12: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Object Plan Guidessp_create_plan_guide@name = N'Guide1',@stmt = N'SELECT *FROM Sales.SalesOrderHeader AS h,

Sales.Customer AS c,Sales.SalesTerritory AS tWHERE h.CustomerID = c.CustomerID

AND c.TerritoryID = t.TerritoryIDAND CountryRegionCode =

@Country_region',@type = N'OBJECT',@module_or_batch = N'Sales.GetSalesOrderByCountry',@params = NULL,@hints = N'OPTION (OPTIMIZE FOR (@Country_region= N''US''))'

Page 13: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Implementing Object Plan GuidesNameTitleCompany

Page 14: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

SQL Plan Guides

You are interested in a particular T-SQL statement

Stand-alone

Or part of a batch

T-SQL statements sent by CLR objects, extended SPs & Dynamic SQL (EXEC (sql_string)) are processed as batches

The text has to be exactly the same:

Case, whitespace, etc.

Page 15: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

SQL Plan Guidessp_create_plan_guide@name = N'Guide2', @stmt = N'SELECT TOP 1 * FROM Sales.SalesOrderHeader ORDER BY OrderDate DESC',@type = N'SQL',@module_or_batch = NULL, @params = NULL, @hints = N'OPTION (MAXDOP 1)';

Page 16: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Implementing SQL Plan GuidesNameTitleCompany

Page 17: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Template Plan Guides

To override PARAMETERIZATION database setting

Can only use the following hints:

PARAMETERIZATION FORCED

PARAMETERIZATION SIMPLE

Little tricky

SQL Server has to construct a template of the query in the same format that it will be in once it is parameterized

Special stored procedure: sp_get_query_template

@params is only used for TEMPLATE plan guide

Page 18: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Template Plan GuidesEXEC sp_create_plan_guide

@name = N'TemplateGuide1',@stmt = N'SELECT * FROM

AdventureWorks.Sales.SalesOrderHeader AS hINNER JOIN

AdventureWorks.Sales.SalesOrderDetail AS d ON h.SalesOrderID =

d.SalesOrderIDWHERE h.SalesOrderID = @0',

@type = N'TEMPLATE',@module_or_batch = NULL,@params = N'@0 int',@hints = N'OPTION(PARAMETERIZATION FORCED)';

Page 19: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Implementing Template Plan GuidesNameTitleCompany

Page 20: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Applying a Fixed Query Plan to a Plan Guide

Useful when you know about an existing execution plan that performs better than the one selected by the optimizer for a particular query

Can be applied to OBJECT or SQL plan guides

Extract the XML Showplan from the cache and supply it to the sp_create_plan_guide

Page 21: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Applying a Fixed Query Plan to a Plan Guide

Page 22: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Creating Plan Guides directly from the plan cache

Creates one or more plan guides from a query plan in the plan cache

Can be applied to OBJECT or SQL plan guides

Extract the plan handle from the cache and supply it to the sp_create_plan_guide_from_handle

Page 23: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Creating Plan Guides directly from the plan cache

Page 24: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Managing & Validating Plan Guides

Managing Plan Guides

sp_control_plan_guide

ENABLE, DISABLE, DROP

Validating Plan guides

sys.fn_validate_plan_guide

Page 25: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Plan Guides considerations

The statement text in the plan guide must exactly match the query text

To verify that your plan guide was used, see for specific elements in XML Showplan output

PlanGuideDB & PlanGuideName (or)

TemplatePlanGuideDB & TemplatePlanGuideName

Query Plan (properties window)

Page 26: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

Summary

There are many scenarios where you cannot use hints

Plan Guides come to rescue

Three different types (OBJECT, SQL, TEMPLATE)

SQL Server 2008 enhancements

Applying a fixed query to a plan guide using XML Showplan

Creating a plan directly from a plan in the plan cache

Validating Plan Guides

Plan Guide Considerations

Page 27: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted
Page 29: Amit Bansal CTO Peopleware Indiadownload.microsoft.com/download/F/3/3/F33D91DF-D6E3-442A... · 2018-10-16 · Who is Amit Bansal? CTO, eDominer Systems & Peopleware India Conducted

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