27
#sqlsatPordenone #sqlsat367 February 28 th , 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni www.ugiss.org/sgovoni www.sqlblog.com/blogs/sergio_govoni speakerscore.com/sqlsat367-Execution-Plans-End-to-End

#sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

Embed Size (px)

Citation preview

Page 1: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Executions PlansEnd-to-End in SQL ServerSergio Govoni

@segovoniwww.ugiss.org/sgovoni

www.sqlblog.com/blogs/sergio_govoni

speakerscore.com/sqlsat367-Execution-Plans-End-to-End

Page 2: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Sponsors

Page 3: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Organizers

Page 4: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Speaker info

@segovoniSQL Server MVP

ugiss.org/sgovoni [email protected]

sqlblog.com/blogs/sergio_govoni

manning.com/delaney

Page 5: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Agenda

Execution Plan Fundamentals The Optimization Process How to influence Query Optimizer How to force a particular Execution Plan Plan Cache

Page 6: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Execution Plans- Fundamentals

Execution plan is generated by Query Optimizer It helps us to answer the question: Why this

query is slow?

It contains the instructions to execute a query Table access order Join operators Indexes

Page 7: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Execution Plans- Fundamentals

Two types Estimated Execution Plan Actual Execution Plan

Three formats Graphical Plans Text Plans (deprecated) XML Plans

Where you can find them? SSMS, SQL Profiler, DMVs

Page 8: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

The optimization process

Parsing and Binding Logical tree

Simplification Trivial Plan

Full optimization Search 0 (TP) Search 1 (QP) Search 2 (FULL)

• No data dependences• No access to statistics• No cost-based plan

• Using statistics• Cost-based plan• Each phase can return the execution plan

Page 9: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

The optimization process

Information about the optimization process sys.dm_exec_query_optimizer_info

Information on the use of transformation rules sys.dm_exec_query_transformation_stats

Cumulative values since the SQL Server Instance restart

Page 10: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

DEMOThe optimization process

Page 11: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

How to influence the Query Optimizer

Special directives called Hints are able to influence the choices of the Query Optimizer Advanced troubleshooting They can not be used to generate an invalid

execution plan Not completely documented

Restrict the search space of the Query Optimizer is dangerous! Do not use it massively

Page 12: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Hints

Query Hints The most numerous and prominent They have affect on the entire query

Join Hints They require the use of a particular join operator

Table Hints They allow you to control the table access

methods

Page 13: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Trace Flags

Trace Flags are special directives used for: Enable a specific feature Disable a particular behavior

Scope: Global, Session, Query Not completely documented DBCC TRACE* QUERYTRACEON

Page 14: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Before using Hints and Trace Flags (QO)

Has the Query Optimizer all the necessary information to produce a good execution plan? Are the queries deterministic? Are the tables indexed? Are the statistics accurate? Are the system resources appropriate?

I/O sub-system, CUP, RAM

Page 15: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

DEMOHow to influence the Query Optimizer

Page 16: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

How to force a particular Execution Plan

You can force the Query Optimizer to use a specific Execution Plan (for a query) with USE PLAN query hint Plan Giude

sp_create_plan_guide sp_create_plan_guide_from_handle sp_control_plan_guide Wizard on SSMS sys.plan_guides

Page 17: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

How to force a particular Execution Plan- Limitations!

You can not force the Query Optimizer to use a specific Execution Plan for DML statements (INSERT, UPDATE, DELETE) Queries that use dynamic cursors Distributed queries and Full Text queries Plans that are non considered during the

optimization process

Page 18: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

DEMOHow to force a particular Execution Plan

Page 19: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Plan Cache

SQL Server stores Execution Plans in Buffer Pool zone that is known as Plan Cache

Each Execution Plan (in cache) is divided in Query Plan

Read only No information about the user context Two copies (one for parallel plan)

Execution Context Stores information about the user context

Page 20: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Plan Cache

DMVs that showing cached plans sys.dm_exec_cached_plans sys.dm_exec_plan_attributes sys.dm_exec_query_plan

Page 21: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

sys.dm_exec_cached_plans

Six types of cached plans (cacheobjtype) Compiled Plan, Compiled Plan Stub Parse Tree, Extended Proc, CLR Proc, CLR

Function

Three types (objtype) of Compiled Plan Adhoc, Prepared, Proc

Look at usercounts to know if cached plan is reused or not

plan_handle

Page 22: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Optimize for Ad-hoc Workloads

Available since SQL Server 2008 Server level option Adhoc Query Plans are not cached on the

first use Better Memory Management One more recompilation is necessary sys.dm_exec_cached_plans

cacheobjtype «Compiled Plan Stub» objtype «Adhoc»

Page 23: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

DEMOPlan Cache

Page 24: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Tools

SQL Sentry Plan Explorer http://www.sqlsentry.net/plan-explorer/ x86, x64, Free & PRO Versions Add-in for SQL Server Management Studio

Page 25: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Resources

SQL Server Execution Plans (2nd Edition) – Grant Fritchey http://bit.ly/1cSiGWq

Inside the SQL Server Query Optimizer – Benjamin Nevarez http://bit.ly/1dqyM9h

Page 26: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

Q&A

Questions?

Page 27: #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio Govoni @segovoni

#sqlsatPordenone#sqlsat367February 28th, 2015

THANKS!Thanks for attending this session. If you have additional questions, please post them on UGISS forum at www.ugiss.org/forum

#sqlsatPordenone#sqlsat367@segovoni

Feedback form:http://speakerscore.com/sqlsat367-Execution-Plans-End-to-End