Execution plans Eugene Meidinger @sqlgene www.sqlgene.com/powerbi/ emeidinger@all-lines-tech.com
About me Business Intelligence developer Worked for All-Lines for 5 years Spoken at Pittsburgh SQL User Group and various SQL Saturdays Help lead the Pittsburgh Power BI User Group Pluralsight Author Went from SQL newb to SQL pro
Why are execution plans important? They are simple to use They are key to improving query performance They give you insight to the database engine
What is an execution plan? Instructions for how to process a query Can think of it as a recipe Created by the query optimizer at run time Execution plans are cached after the first run Many different ways to process the same query For trivial queries, a simple plan is used Can be forced with hints Don’t try to outsmart the database engine
How to view the execution plan? Go to Query Click Include Actual Execution Plan Or, Press Ctrl + M Or click the execution plan icon on the toolbar
What does a plan look like?
How to read an execution plan Read from right to left and top to bottom Icons represent operators Operators have cost below them Arrows represent data flow Bigger arrows represent more data Hover over icons and arrows for more detail
Reading the Arrows
Reading the Popup box Focus on the top, bottom, cost and estimated versus actual Top explains the operator Bottom is the important detail Est vs. Act can be a problem The rest isn’t important yet
Missing index SQL Server will suggest an index if needed Use with caution
Comparing Queries Query Cost – Relative to Batch Run multiple queries to compare them
Types of operators Gathering Data Joining Data Manipulating Data MISC
Gather data Table scan Clustered index scan Clustered index seek Nonclustered index scan Nonclustered index seek RID lookup Key lookup
Scans and seeks Scan versus seek Clustered versus nonclustered Scan – Going through all of the rows Seek – Going to a specific row Clustered versus nonclustered Clustered – using the whole table Nonclustered – taking advantage of the index Tables versus heaps Key Lookup – uses the primary key to find a row RID Lookup – uses the row number in a heap to find a row
Joins Merge Join – Joins two sorted datasets Nested loop join – Loops a small data set over a larger one Hash join – A hash table is made with the smaller input https://technet.microsoft.com/en-us/library/ms189313(v=sql.105).aspx
Manipulate data Filter Top Sort Compute Scalar
Demo
SQL Server Execution Plans, 2nd edition https://www.red-gate.com/library/sql-server-execution-plans-2nd-edition
SentryOne Plan Explorer https://sentryone.com/plan-explorer