#sqlsatpordenone #sqlsat367 february 28 th, 2015 executions plans end-to-end in sql server sergio...
TRANSCRIPT
#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
#sqlsatPordenone#sqlsat367February 28th, 2015
Sponsors
#sqlsatPordenone#sqlsat367February 28th, 2015
Organizers
#sqlsatPordenone#sqlsat367February 28th, 2015
Speaker info
@segovoniSQL Server MVP
ugiss.org/sgovoni [email protected]
sqlblog.com/blogs/sergio_govoni
manning.com/delaney
#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
#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
#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
#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
#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
#sqlsatPordenone#sqlsat367February 28th, 2015
DEMOThe optimization process
#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
#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
#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
#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
#sqlsatPordenone#sqlsat367February 28th, 2015
DEMOHow to influence the Query Optimizer
#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
#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
#sqlsatPordenone#sqlsat367February 28th, 2015
DEMOHow to force a particular Execution Plan
#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
#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
#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
#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»
#sqlsatPordenone#sqlsat367February 28th, 2015
DEMOPlan Cache
#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
#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
#sqlsatPordenone#sqlsat367February 28th, 2015
Q&A
Questions?
#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