understanding graphical execution plans level 200
DESCRIPTION
Understanding Graphical Execution Plans Level 200. Ashwani Roy . Agenda. Query Processing lifecycle by Database Engine Elements in a Execution Plans Important Execution Plan Operators. What Happens when a Query is submitted. Query Parsing Query Optimization Query Execution . - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/1.jpg)
Ashwani Roy
Understanding Graphical Execution Plans Level 200
![Page 2: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/2.jpg)
• Query Processing lifecycle by Database Engine
• Elements in a Execution Plans
• Important Execution Plan Operators
Agenda
![Page 3: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/3.jpg)
• Query Parsing
• Query Optimization
• Query Execution
What Happens when a Query is submitted
![Page 4: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/4.jpg)
Query Plan
![Page 5: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/5.jpg)
• Logical and Physical Operators • Parallelism Physical Operators• Cursor Operators• Language Elements
Operators in an Execution Plan
![Page 6: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/6.jpg)
Columns in a Plan
Rows EstimateIO
Executes EstimateCPU
StmtId AvgRowSize
NodeId TotalSubtreeCost
Parent OutputList
PhysicalOp Warnings
LogicalOp Type
Argument Parallel
DefinedValues EstimateExecutions
EstimateRows
![Page 7: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/7.jpg)
• If a Cached Plan exists then SQL Server will use this cached plan
DEMO 01
Cached Query Plans
![Page 8: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/8.jpg)
Important Operators in Execution Plans
Select (Result)
Sort Clustered Index Seek
Clustered Index Scan
Non-clustered Index Scan
Non-clustered Index Seek
Table Scan RID Lookup Key Lookup Hash Match
Nested Loops
Merge Join Top Compute Scalar
Constant Scan
Filter Lazy Spool Spool Eager Spool Stream Aggregate
Distribute Streams
Repartition Streams
Gather Streams
Bitmap Split
![Page 9: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/9.jpg)
Index Seek
• Reads B-tree entries to determine the data page• The Argument column contains the name of the
nonclustered index being used• Prefered for highly selective queries
![Page 10: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/10.jpg)
Index Seek
![Page 11: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/11.jpg)
Index Scan
• Horizontal traversal of the leaf level of the index from the first page to the last
• Retrieves all rows from the nonclustered index• The Argument column contains the name of the
nonclustered index being used
![Page 12: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/12.jpg)
Clustered Index Scan
• The clustered index scan’s logical and physical operator scans the clustered index
• The Argument column contains the name of the clustered index
• If the table does not have Clustered Index the same Query will produce Table Scan
![Page 13: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/13.jpg)
DEMO 02
![Page 14: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/14.jpg)
Clustered Index Seek
• Cluster index seek • Uses the seeking ability of indexes to retrieve rows• The Argument column contains the name of the
clustered index being used• Seek() predicate contains the columns used for
seeking
![Page 15: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/15.jpg)
Bookmark Lookups
• Uses a bookmark to look up a row in a clustered index or table
• The Argument column contains the bookmark label
• Can be removed by covering columns• May have a performance improvement
![Page 16: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/16.jpg)
KEY LOOKUP
• A Key Lookup is a bookmark lookup on a table with a clustered index.
• Means that the optimizer cannot retrieve the rows in a single operation, and has to use a clustered key (or a row ID) to return the corresponding rows from a clustered index (or from the table itself).
• Performance can be improved by making Non-Clustered Index or Covering Index
![Page 17: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/17.jpg)
DEMO 03
![Page 18: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/18.jpg)
RID Lookup
• A type of bookmark lookup
• Occurs on a heap table (a table that doesn't have a clustered index)
• Uses a row identifier to find the rows to return.
![Page 19: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/19.jpg)
DEMO 04
![Page 20: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/20.jpg)
Nested Loop
• The top input to the nested loop is the outer table• The bottom input to the nested loop is the inner table• For each outer row, searches for matching rows are in
the inner input table• Effective if the outer input is very small and the inner
input is preindexed and very large• Optimizer sometimes sorts the outer input to improve
locality of the searches on the index over the inner input
• Best when search exploits an index (indexes on join columns are used)
• Low memory requirement
![Page 21: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/21.jpg)
Hash Join
• The top input is build input, the smaller of the two inputs
• The bottom input is probe input• The hash join first scans or computes the whole
build input• Requires at least one equality clause in the join
predicate• Good for ad-hoc queries
![Page 22: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/22.jpg)
Merge Join
• Both inputs should be sorted on the merge column keys
• An index on a correct set of columns is useful• A many-to-many merge join uses a temporary
table to store rows• Very fast if the data that you want can be
obtained presorted from existing B-tree indexes
![Page 23: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/23.jpg)
WHICH JOIN IS GOOD
• NONE AND ALL• A Merge Join is an efficient way to join two tables,#
• when the join columns are pre sorted • if the join columns are not pre sorted, the query
optimizer has the option of• a) sorting the join columns first, then performing
a Merge Join, or • b) performing a less efficient Hash Join. The
query optimizer considers all the options and generally chooses the execution plan that uses the least resources.
![Page 24: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/24.jpg)
DEMO 05
![Page 25: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/25.jpg)
Stream Aggregation
• The argument column of the plan output shows the list of columns of the GROUP BY or DISTINCT clause
• The list of aggregate expressions will appear in the Defined Values column of the plan output
• Best for smaller sets or sets already sorted• Input is sorted and output is ordered
![Page 26: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/26.jpg)
Hash Aggregation
• Used with large sets• Aggregations are evaluated while building the
hash• Input can be in random order; output is always
in random order
![Page 27: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/27.jpg)
DEMO 06
![Page 28: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/28.jpg)
Rewinds and Rewinds
![Page 29: Understanding Graphical Execution Plans Level 200](https://reader036.vdocuments.mx/reader036/viewer/2022062520/568165ed550346895dd912ce/html5/thumbnails/29.jpg)
Questions THANK YOU