SlideShare a Scribd company logo
Performance Tuning: Intro to
  Execution Plan Analysis
    Presented by Kon Melamud
About me
• SQL Server Team Lead for RDX
• Lead code tuning team within RDX
• RDX works with over 200 customers on
  solutions within the SQL Server environment
  world wide.
Introduction to execution plan analysis
Introduction to execution plan analysis
Divide and conquer approach
• Eliminating non-problem areas
  –   Hardware - memory, CPU
  –   Disk/SAN performance
  –   Maintenance Tasks - index/statistics maintenance
  –   Other Applications on server causing performance bottleneck

      For more information on tools and techniques used to identify
      problem areas check out John Sterrett’s presentation, Performance
      tuning for Pirates, http://bit.ly/P3h5Hf
Objectives
• Execution plans
  – Additional tools
  – Things to check before start of analysis
• Indexes used by the SQL optimizer
  – Different type of index operations
  – Best practices
  – Determine what indexes are the optimal ones and the exceptions to
    the rules
• Join operations
  – Different join type and how they work
  – Exceptions to the general best practices
Execution Plans in a glance
• How to view an execution plan
  –   Profiler
  –   DMVs
  –   Extended Events (SQL2008+ only)
  –   Ad hoc queries

• Different views of execution plans
  – Graphical view
  – XML view
  – Text view
Before we begin…
• Additional query options (STATISTICS IO/TIME)
• Check optimization levels
• Check if optimizer timed out during plan
  generation
• How to regenerate a plan
• Right-to-left approach
Index Operations
• Four different base table operations
  –   Table Scan
  –   Index Scan - clustered/non-clustered
  –   Index Seek - clustered/non-clustered
  –   Lookups - RID/Key
Index Operations – Table Scans
• Table Scan – Heap (no clustered index)
    –   Generally not a best practice for data retrieval

• When Table Scans Are Acceptable
    –   Small tables
    –   Table variables
    –   CTE functionality
    –   Heavily inserted tables, minimal selects/updates/deletes
Index Operations – Index Scans
• Clustered Index Scans
    –   Same as a table scan but table is sorted on disk
    –   Generally slightly better than heap operations for CPU usage
    –   Query could most likely benefit from different index
    –   Could also mean the large range of data is been selected
Index Operations – Index Scans
• Non-Clustered Index Scan
   –   Less data scanned than a clustered index
   –   Causes for non-clustered index scan
          • Not an optimal index for the query
          • Seek predicate is not SARGable
          • Dataset returned represents most of the table
Index Operations – Index Seeks
• Clustered Index Seek
    –   Most preferred method for data retrieval
    –   Downside, only one clustered index can exist on the table
Index Operations – Index Seeks
• Non-Clustered Index Seek
   –   As good as clustered index seek
   –   Upside, can have multiple non-clustered indexes
   –   Downside, over indexing tables can cause slowdowns on
       writes
Index Operations - Lookups
• Lookups – What are they?
  – Only on non-clustered index operations
  – RID Lookup (Heap) - no clustered index present
  – Key Lookup (Clustered) - clustered key lookup

• Eliminating lookups
  – Pre SQL2005, create covering indexes
  – SQL2005 and later, INCLUDED columns option

• Exceptions to eliminating lookups
  – Select every column from the table
Index Operations - Lookups
RID Lookup      Key Lookup
Join Operations
• Three main logical join operators
  – Hash join
  – Nested Loop join
  – Merge join
Join Operations – Hash Join
• Hash joins - How it works…
  – Two phases - build phase, probe phase
      • Build phase - Scans or computes build input and builds hash table in memory (if
        possible)
      • Probe phase - For each row of probe input hash value is build or computed than
        compared against hash bucket of the build input, either producing matches or not
  – Requires at least one equality statement
  – Performed when at least one side of the join is not properly indexed
    (index scan/table scan)
  – Most common join operation
Join Operations – Hash Join
Graphic Look:
Join Operation – Nested Loop
• Nested loop join – How it works
  – Also called nested iteration, builds two inputs - inner and outer input
     • Outer input (displayed on top in the execution plan)
           – Processes row by row against inner output
           – A smaller table in the join predicate
      • Inner input (on the bottom of the execution plan)
           – Scanned for every row of the outer input to produce matches
           – A larger table of the two participating in the join
           – Has to be properly indexed ( index seeks)
  – Usually used when one table in the join is relatively small and the
    other is rather large, and at least one table (larger) is properly indexed.
     • If those conditions are met generally outperforms hash join
       operations
Join Operation – Nested Loop

 Graphic Look:
Join Operations - Merge
• Merge join – How it works
  – Both tables participated in the join must be sorted on the join
    columns. If that condition is not met, merge join operation would
    require an explicit sort which could be very costly.
  – Both inputs are scanned once at the same time, one row at a time,
    and if the rows are not matched, the row is discarded. Since both
    inputs are sorted, we can guarantee we will not see another possible
    match.
Join Operations - Merge Join
Graphic Look:
Parallelism – Good or Bad?
• SQL Server Optimizer determines when to use
  – Can be seen in execution plan as one of three exchange operators
      • Distribute Streams
      • Repartition Streams
      • Gather Streams
  – When parallel execution is not an option
      • The query contains scalar or relational operators that cannot be run in parallel
      • Sequential cost of the query is not high enough
      • Sequential execution is faster than parallel
  – Cost threshold of parallelism and MAXDOP options
Parallelism -
Graphical View:
Putting it all Together
•   Again Right-to-left approach
•   Index operation priorities Seek-> Scan-> Heap
•   Join operation priorities Merge->NL-> Hash
•   Advanced options
    – MAXDOP
    – INDEX HINTS
    – OPTIMIZE FOR
    – JOIN HINTS
Q/A Time
• Any questions regarding what we have
  covered?
We are done 
Thanks for attending.

More Related Content

PPTX
Sql performance tuning
PPT
TCC14 tour hague optimising workbooks
PPTX
ACS DataMart_ppt
PDF
Efficient transaction processing in sap hana
PDF
Breaking data
PPTX
SQL - Structured Query Language
PPTX
Part 25 view vs. mv
PDF
Table Partitioning: Secret Weapon for Big Data Problems
Sql performance tuning
TCC14 tour hague optimising workbooks
ACS DataMart_ppt
Efficient transaction processing in sap hana
Breaking data
SQL - Structured Query Language
Part 25 view vs. mv
Table Partitioning: Secret Weapon for Big Data Problems

Similar to Introduction to execution plan analysis (20)

PDF
Brad McGehee Intepreting Execution Plans Mar09
PDF
Brad McGehee Intepreting Execution Plans Mar09
PDF
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
PPTX
DOAG: Visual SQL Tuning
PPTX
SQL Server 2008 Development for Programmers
PPTX
SQL Server 2012 Best Practices
PPT
Mssql
PPTX
Database Performance Tuning
PPTX
Oracle sql high performance tuning
PDF
Query Tuning for Database Pros & Developers
PDF
SQL Joins and Query Optimization
ODP
The PostgreSQL Query Planner
PPTX
Oracle performance tuning_sfsf
PDF
4 execution plans
PPTX
05_DP_300T00A_Optimize.pptx
PPTX
Query Optimization in SQL Server
PPTX
Adbms 39 algorithms for project and set operations
PPTX
Relational Algebra Operator With Example
PPTX
02 database oprimization - improving sql performance - ent-db
PPT
Sql server introduction to sql server
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
DOAG: Visual SQL Tuning
SQL Server 2008 Development for Programmers
SQL Server 2012 Best Practices
Mssql
Database Performance Tuning
Oracle sql high performance tuning
Query Tuning for Database Pros & Developers
SQL Joins and Query Optimization
The PostgreSQL Query Planner
Oracle performance tuning_sfsf
4 execution plans
05_DP_300T00A_Optimize.pptx
Query Optimization in SQL Server
Adbms 39 algorithms for project and set operations
Relational Algebra Operator With Example
02 database oprimization - improving sql performance - ent-db
Sql server introduction to sql server
Ad

More from John Sterrett (14)

PDF
Database Management Myths for Developers
PDF
DBA Basics: Getting Started with Performance Tuning.pdf
PDF
Getting Started with SQL Server Performance Tuning.pdf
PDF
Workload Replay in the Cloud: Secret Weapon for Cloud Migrations
PDF
PowerPivot for DBAs
PPTX
Introduction to High Availability with SQL Server
PPTX
Introduction to PowerShell for DBA's
PPTX
SQL Server Performance Root Cause Analysis in 10 Minutes
PDF
12 Steps to Workload Tuning
PDF
Performance Tuning for Pirates!
PPTX
Can You Host a SQL Saturday?
PPTX
Evaluating Daily Checklist Against 1000 Servers using Policy Based Management
PPTX
SQL Server 2008 For Developers
PPTX
Evaluate Daily Checklist with PBM and CMS
Database Management Myths for Developers
DBA Basics: Getting Started with Performance Tuning.pdf
Getting Started with SQL Server Performance Tuning.pdf
Workload Replay in the Cloud: Secret Weapon for Cloud Migrations
PowerPivot for DBAs
Introduction to High Availability with SQL Server
Introduction to PowerShell for DBA's
SQL Server Performance Root Cause Analysis in 10 Minutes
12 Steps to Workload Tuning
Performance Tuning for Pirates!
Can You Host a SQL Saturday?
Evaluating Daily Checklist Against 1000 Servers using Policy Based Management
SQL Server 2008 For Developers
Evaluate Daily Checklist with PBM and CMS
Ad

Recently uploaded (20)

PPTX
A slide for students with the advantagea
PPTX
The-Scope-of-Food-Quality-and-Safety.pptx managemement
PPTX
Job-opportunities lecture about it skills
PPT
APPROACH TO DEVELOPMENTALlllllllllllllllll
PPTX
Principles of Inheritance and variation class 12.pptx
PPTX
Overview Planner of Soft Skills in a single ppt
PDF
Blue-Modern-Elegant-Presentation (1).pdf
PPTX
STS CHAP 4 human development as reflected
DOCX
mcsp232projectguidelinesjan2023 (1).docx
PDF
Sheri Ann Lowe Compliance Strategist Resume
PPTX
Definition and Relation of Food Science( Lecture1).pptx
PPTX
Theory of Change. AFH-FRDP OCEAN ToCpptx
PPTX
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
PDF
Understanding the Rhetorical Situation Presentation in Blue Orange Muted Il_2...
PPT
Gsisgdkddkvdgjsjdvdbdbdbdghjkhgcvvkkfcxxfg
PDF
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf
PPTX
microtomy kkk. presenting to cryst in gl
PPTX
CYBER SECURITY PPT.pptx CYBER SECURITY APPLICATION AND USAGE
PDF
iTop VPN Crack Latest Version 2025 Free Download With Keygen
PPTX
DPT-MAY24.pptx for review and ucploading
A slide for students with the advantagea
The-Scope-of-Food-Quality-and-Safety.pptx managemement
Job-opportunities lecture about it skills
APPROACH TO DEVELOPMENTALlllllllllllllllll
Principles of Inheritance and variation class 12.pptx
Overview Planner of Soft Skills in a single ppt
Blue-Modern-Elegant-Presentation (1).pdf
STS CHAP 4 human development as reflected
mcsp232projectguidelinesjan2023 (1).docx
Sheri Ann Lowe Compliance Strategist Resume
Definition and Relation of Food Science( Lecture1).pptx
Theory of Change. AFH-FRDP OCEAN ToCpptx
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
Understanding the Rhetorical Situation Presentation in Blue Orange Muted Il_2...
Gsisgdkddkvdgjsjdvdbdbdbdghjkhgcvvkkfcxxfg
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf
microtomy kkk. presenting to cryst in gl
CYBER SECURITY PPT.pptx CYBER SECURITY APPLICATION AND USAGE
iTop VPN Crack Latest Version 2025 Free Download With Keygen
DPT-MAY24.pptx for review and ucploading

Introduction to execution plan analysis

  • 1. Performance Tuning: Intro to Execution Plan Analysis Presented by Kon Melamud
  • 2. About me • SQL Server Team Lead for RDX • Lead code tuning team within RDX • RDX works with over 200 customers on solutions within the SQL Server environment world wide.
  • 5. Divide and conquer approach • Eliminating non-problem areas – Hardware - memory, CPU – Disk/SAN performance – Maintenance Tasks - index/statistics maintenance – Other Applications on server causing performance bottleneck For more information on tools and techniques used to identify problem areas check out John Sterrett’s presentation, Performance tuning for Pirates, http://bit.ly/P3h5Hf
  • 6. Objectives • Execution plans – Additional tools – Things to check before start of analysis • Indexes used by the SQL optimizer – Different type of index operations – Best practices – Determine what indexes are the optimal ones and the exceptions to the rules • Join operations – Different join type and how they work – Exceptions to the general best practices
  • 7. Execution Plans in a glance • How to view an execution plan – Profiler – DMVs – Extended Events (SQL2008+ only) – Ad hoc queries • Different views of execution plans – Graphical view – XML view – Text view
  • 8. Before we begin… • Additional query options (STATISTICS IO/TIME) • Check optimization levels • Check if optimizer timed out during plan generation • How to regenerate a plan • Right-to-left approach
  • 9. Index Operations • Four different base table operations – Table Scan – Index Scan - clustered/non-clustered – Index Seek - clustered/non-clustered – Lookups - RID/Key
  • 10. Index Operations – Table Scans • Table Scan – Heap (no clustered index) – Generally not a best practice for data retrieval • When Table Scans Are Acceptable – Small tables – Table variables – CTE functionality – Heavily inserted tables, minimal selects/updates/deletes
  • 11. Index Operations – Index Scans • Clustered Index Scans – Same as a table scan but table is sorted on disk – Generally slightly better than heap operations for CPU usage – Query could most likely benefit from different index – Could also mean the large range of data is been selected
  • 12. Index Operations – Index Scans • Non-Clustered Index Scan – Less data scanned than a clustered index – Causes for non-clustered index scan • Not an optimal index for the query • Seek predicate is not SARGable • Dataset returned represents most of the table
  • 13. Index Operations – Index Seeks • Clustered Index Seek – Most preferred method for data retrieval – Downside, only one clustered index can exist on the table
  • 14. Index Operations – Index Seeks • Non-Clustered Index Seek – As good as clustered index seek – Upside, can have multiple non-clustered indexes – Downside, over indexing tables can cause slowdowns on writes
  • 15. Index Operations - Lookups • Lookups – What are they? – Only on non-clustered index operations – RID Lookup (Heap) - no clustered index present – Key Lookup (Clustered) - clustered key lookup • Eliminating lookups – Pre SQL2005, create covering indexes – SQL2005 and later, INCLUDED columns option • Exceptions to eliminating lookups – Select every column from the table
  • 16. Index Operations - Lookups RID Lookup Key Lookup
  • 17. Join Operations • Three main logical join operators – Hash join – Nested Loop join – Merge join
  • 18. Join Operations – Hash Join • Hash joins - How it works… – Two phases - build phase, probe phase • Build phase - Scans or computes build input and builds hash table in memory (if possible) • Probe phase - For each row of probe input hash value is build or computed than compared against hash bucket of the build input, either producing matches or not – Requires at least one equality statement – Performed when at least one side of the join is not properly indexed (index scan/table scan) – Most common join operation
  • 19. Join Operations – Hash Join Graphic Look:
  • 20. Join Operation – Nested Loop • Nested loop join – How it works – Also called nested iteration, builds two inputs - inner and outer input • Outer input (displayed on top in the execution plan) – Processes row by row against inner output – A smaller table in the join predicate • Inner input (on the bottom of the execution plan) – Scanned for every row of the outer input to produce matches – A larger table of the two participating in the join – Has to be properly indexed ( index seeks) – Usually used when one table in the join is relatively small and the other is rather large, and at least one table (larger) is properly indexed. • If those conditions are met generally outperforms hash join operations
  • 21. Join Operation – Nested Loop Graphic Look:
  • 22. Join Operations - Merge • Merge join – How it works – Both tables participated in the join must be sorted on the join columns. If that condition is not met, merge join operation would require an explicit sort which could be very costly. – Both inputs are scanned once at the same time, one row at a time, and if the rows are not matched, the row is discarded. Since both inputs are sorted, we can guarantee we will not see another possible match.
  • 23. Join Operations - Merge Join Graphic Look:
  • 24. Parallelism – Good or Bad? • SQL Server Optimizer determines when to use – Can be seen in execution plan as one of three exchange operators • Distribute Streams • Repartition Streams • Gather Streams – When parallel execution is not an option • The query contains scalar or relational operators that cannot be run in parallel • Sequential cost of the query is not high enough • Sequential execution is faster than parallel – Cost threshold of parallelism and MAXDOP options
  • 26. Putting it all Together • Again Right-to-left approach • Index operation priorities Seek-> Scan-> Heap • Join operation priorities Merge->NL-> Hash • Advanced options – MAXDOP – INDEX HINTS – OPTIMIZE FOR – JOIN HINTS
  • 27. Q/A Time • Any questions regarding what we have covered?
  • 28. We are done  Thanks for attending.