Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "#sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio"— Presentation transcript:

1 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio speakerscore.com/sqlsat367-Execution-Plans-End-to-End

2 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Sponsors

3 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Organizers

4 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Speaker Server MVP sqlblog.com/blogs/sergio_govoni manning.com/delaney

5 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Agenda  Execution Plan Fundamentals  The Optimization Process  How to influence Query Optimizer  How to force a particular Execution Plan  Plan Cache

6 #sqlsatPordenone #sqlsat367 February 28 th, 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

7 #sqlsatPordenone #sqlsat367 February 28 th, 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

8 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Cost-based Plan > From David DeWitt keynote at PASS Summit - Select o_year, sum(case when nation = 'BRAZIL' then volume else 0 end) / sum(volume) from ( select YEAR(O_ORDERDATE) as o_year, L_EXTENDEDPRICE * (1 - L_DISCOUNT) as volume, n2.N_NAME as nation from PART, SUPPLIER, LINEITEM, ORDERS, CUSTOMER, NATION n1, NATION n2, REGION where P_PARTKEY = L_PARTKEY and S_SUPPKEY = L_SUPPKEY and L_ORDERKEY = O_ORDERKEY and O_CUSTKEY = C_CUSTKEY and C_NATIONKEY = n1.N_NATIONKEY and n1.N_REGIONKEY = R_REGIONKEY and R_NAME = 'AMERICA‘ and S_NATIONKEY = n2.N_NATIONKEY and O_ORDERDATE between ' ' and ' ' and P_TYPE = 'ECONOMY ANODIZED STEEL' and S_ACCTBAL <= constant-1 and L_EXTENDEDPRICE <= constant-2 ) as all_nations group by o_year order by o_year

9 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Heap table (no clustered index) IAM index_id = 0 first_iam_page Data rows

10 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Clustered index Prev | Next Index rows index_id = 1 root_page Prev | Next Data rows Prev | Next Index rows Prev | Next Data rows Prev | Next Data rows Prev | Next Data rows

11 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Non-Clustered Index Prev | Next Index rows index_id > 1 root_page Prev | Next RIDs or CI Keys, Included cols Prev | Next Index rows Prev | Next … Prev | Next … Prev | Next …

12 #sqlsatPordenone #sqlsat367 February 28 th, 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

13 #sqlsatPordenone #sqlsat367 February 28 th, 2015 The optimization process  Information about the optimization process  sys.dm_exec_query_optimizer_info sys.dm_exec_query_optimizer_info  Information on the use of transformation rules  sys.dm_exec_query_transformation_stats sys.dm_exec_query_transformation_stats  Cumulative values since the SQL Server Instance restart

14 #sqlsatPordenone #sqlsat367 February 28 th, 2015 DEMO The optimization process

15 #sqlsatPordenone #sqlsat367 February 28 th, 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

16 #sqlsatPordenone #sqlsat367 February 28 th, 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

17 #sqlsatPordenone #sqlsat367 February 28 th, 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* DBCC TRACE*  QUERYTRACEON QUERYTRACEON

18 #sqlsatPordenone #sqlsat367 February 28 th, 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

19 #sqlsatPordenone #sqlsat367 February 28 th, 2015 DEMO How to influence the Query Optimizer

20 #sqlsatPordenone #sqlsat367 February 28 th, 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  sp_create_plan_guide_from_handle sp_create_plan_guide_from_handle  sp_control_plan_guide sp_control_plan_guide  Wizard on SSMS  sys.plan_guides sys.plan_guides

21 #sqlsatPordenone #sqlsat367 February 28 th, 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

22 #sqlsatPordenone #sqlsat367 February 28 th, 2015 DEMO How to force a particular Execution Plan

23 #sqlsatPordenone #sqlsat367 February 28 th, 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

24 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Plan Cache  DMVs that showing cached plans  sys.dm_exec_cached_plans sys.dm_exec_cached_plans  sys.dm_exec_plan_attributes sys.dm_exec_plan_attributes  sys.dm_exec_query_plan sys.dm_exec_query_plan

25 #sqlsatPordenone #sqlsat367 February 28 th, 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

26 #sqlsatPordenone #sqlsat367 February 28 th, 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»

27 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Parameterized Plans  Obytype shown as “Prepared”  Auto-parameterization  SIMPLE by default applied to small class of queries  FORCED allows to parametrize more queries  Explicit parameterization with  Prepare/Execute through the application  sp_executesql

28 #sqlsatPordenone #sqlsat367 February 28 th, 2015 DEMO Plan Cache

29 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Tools  SQL Sentry Plan Explorer   x86, x64, Free & PRO Versions  Add-in for SQL Server Management Studio

30 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Resources  SQL Server Execution Plans (2 nd Edition) – Grant Fritchey   Inside the SQL Server Query Optimizer – Benjamin Nevarez 

31 #sqlsatPordenone #sqlsat367 February 28 th, 2015 Q&A  Questions?

32 #sqlsatPordenone #sqlsat367 February 28 th, 2015 THANKS! Thanks for attending this session. If you have additional questions, please post them on UGISS forum at #sqlsatPordenone Feedback form:


Download ppt "#sqlsatPordenone #sqlsat367 February 28 th, 2015 Executions Plans End-to-End in SQL Server Sergio"

Similar presentations


Ads by Google