dive into the query optimizer dive into the query optimizer: undocumented insight benjamin nevarez...
TRANSCRIPT
![Page 1: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/1.jpg)
Dive into the Query Optimizer
Dive into the Query Optimizer: Undocumented Insight
Benjamin NevarezBlog: benjaminnevarez.comTwitter: @BenjaminNevarez
![Page 2: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/2.jpg)
About the SpeakerBenjamin Nevarez
Author of “Inside the SQL Server Query Optimizer” and “SQL Server 2014 Query Tuning& Optimization”
SQL Server 2012 Internals
Working with SQL Server for 15 years
PASS Summit/SQL Server Connections speaker
![Page 3: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/3.jpg)
Dive into the Query Optimizer
SQL is a high-level declarative language
![Page 4: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/4.jpg)
Dive into the Query Optimizer
Query processing steps
![Page 5: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/5.jpg)
Dive into the Query Optimizer
The Optimization Process
Parsing / Binding (before optimization)SimplificationInitial set of Join OrdersTrivial PlanOptimization Phases search 0search 1search 2
![Page 6: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/6.jpg)
Parsing / Binding
Parsing first makes sure that the T-SQL query has a valid syntaxBinding is mostly concerned with name resolutionUses the query information to build a tree of relational operators
![Page 7: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/7.jpg)
Demo
Logical Trees
![Page 8: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/8.jpg)
Simplification
Reduces the query tree into a simpler form in order to make the optimization process easierSome of the simplifications include:
• Redundant inner and outer joins may be removed. A typical example is the Foreign Key Join elimination
• Filters in WHERE clauses are pushed down in the query tree in order to enable early data filtering (predicate pushdown)
• Contradictions are detected and remove
![Page 9: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/9.jpg)
Demo
Simplification
![Page 10: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/10.jpg)
Transformation Rules
Used to explore the search spaceExploration rules (logical transformation rules)
Generate logical equivalent alternativesCommutativity
A join B – > B join AAssociativity
(A join B) join C – > A join (B join C)Implementation rules (physical transformation rules)
Obtain physical alternativesJoin to Sort Merge Join
A join B – > A Merge Join B
![Page 11: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/11.jpg)
Transformation Rules
Applying transformations does not necessarily reduce the cost of the generated alternatives
Cost will be estimated later (only physical alternatives are costed)
![Page 12: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/12.jpg)
Demo
Transformation Rules
![Page 13: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/13.jpg)
The Memo
Search data structure that is used to store the alternatives which are generated and analyzed by the Query OptimizerA new memo structure is created for each optimizationThe Query Optimizer copies the original query tree's logical expressions into the memo structure
![Page 14: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/14.jpg)
The Memo
After Optimization
![Page 15: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/15.jpg)
Demo
The Memo
![Page 16: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/16.jpg)
Optimization Phases – Full Optimization
Search 0, Transaction Processing phaseSearch 1, Quick Plan phaseSearch 2, Full Optimization
![Page 17: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/17.jpg)
Demo
The Optimization Phases
![Page 18: Dive into the Query Optimizer Dive into the Query Optimizer: Undocumented Insight Benjamin Nevarez Blog: benjaminnevarez.com Twitter: @BenjaminNevarez](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649ee75503460f94bf785e/html5/thumbnails/18.jpg)
Dive into the Query Optimizer
Thank You!