2010 ami levin. sql server implements three different physical operators to perform joins. in this...
DESCRIPTION
SELECTX,Y,Z… FROM[Table1] INNER JOIN [Table2] ON [Table1].[C1] = [Table2].[C1] AND [Table1].[C2] = [Table2].[C2] WHERE…TRANSCRIPT
![Page 1: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/1.jpg)
2010
PHYSICAL JOIN OPERATORS
Ami Levin
![Page 2: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/2.jpg)
SESSION GOALS• SQL Server implements three different physical
operators to perform joins. In this session we will see how each of these three operators work, its advantages and challenges.
• We will try to understand the logic behind the optimizer’s decisions on which operator to use for various joins using (semi) real life examples and see examples about how to avoid common pitfalls.
![Page 3: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/3.jpg)
EQUI-INNER-JOINSELECT X,Y,Z…FROM [Table1] INNER JOIN [Table2]
ON [Table1].[C1] = [Table2].[C1]
AND [Table1].[C2] = [Table2].[C2]WHERE …
![Page 4: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/4.jpg)
NEXT TIME• Outer Joins• Non Equi-Joins• Logical Processing Order• NULL Value Issues• Join Parallelism• Partitioned Joins
![Page 5: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/5.jpg)
WHAT IS A JOIN
![Page 6: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/6.jpg)
NESTED LOOPSFetch next row from blue input
No More Rows?Quit
Find matching rows in red input
False
True
Start
![Page 7: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/7.jpg)
CONSIDERATIONS• “Outer Loop” = The Number of Iterations• At Least One Small Input Preferable
• “Inner Operation” = Work for Each Iteration• Index/Table Scan• Index Seek with Lookup• Covering Index Seek
![Page 8: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/8.jpg)
FOREIGN KEY JOINS• Joins Parents and Childs• Most Common Relationship is One-to-Many• Parent IS Indexed Primary Key or Unique• Indexing Foreign Keys Enables Efficient Use
of Nested Loops
![Page 9: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/9.jpg)
NESTED LOOPS
DEMO
![Page 10: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/10.jpg)
MERG
E
Fetch next row from blue input
Fetch next row from red input
True
Start
Rows Match?
False
Quit No More Rows?
False
True
![Page 11: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/11.jpg)
CONSIDERATIONS• Input Must be Pre-Sorted• By All Join Expression(s)• Pre-Sorted in Plan, not necessarily in DB…
• Immediate & Sorted Match Outputs• FASTFIRSTROW Hint
• Very Efficient and Simple Operator
![Page 12: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/12.jpg)
MERGE
DEMO
![Page 13: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/13.jpg)
Fetch next row from blue input
No more rows?
Fetch next row from red input
False
Start
Apply “hash” function
Place row in “hash” bucket
No more rows?
Apply “hash” function
False
True
True
Probe bucket for matching rows
Quit
HASH- MATCH
![Page 14: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/14.jpg)
CONSIDERATIONS• Hash Function Selection• CPU, Memory and potential I/O Overhead• No Sorting Whatsoever• Probing Costs Not Revealed• May Indicate Sub-Optimal Indexing
![Page 15: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/15.jpg)
HASH MATCH
DEMO
![Page 16: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/16.jpg)
CONCLUSION Nested Loops Merge Hash
Good choice when Small outer inputInner input indexed
Pre-sorted inputsSorting required
Very large inputsNot well indexed
CPU consumption Low Low*Unless requires sorting High
Memory usage Low Low*Unless requires sorting High
Logical reads High Low Low* ‘Hidden’ cost of probes
Output matches Fast Fast Slow
![Page 17: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/17.jpg)
FOR MORE INFORMATION• Books On Line• Microsoft White Papers• “SQL Server 2008 Internals”• Kalen Delaney, Kimberly L.Tripp and more…
• Craig Freedman’s MSDN Blog• http://blogs.msdn.com/craigfr/about.aspx
![Page 18: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/18.jpg)
PHYSICAL JOIN OPERATORS
Q&A?
![Page 19: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/19.jpg)
THANK YOU
![Page 20: 2010 Ami Levin. SQL Server implements three different physical operators to perform joins. In this session we will see how each of these three operators](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b1f7f8b9ab059994c45/html5/thumbnails/20.jpg)
COMING UP…P/X001Designing High Scale OLTP systemsThomas KejserP/L001TSQL Techniques – Why and how to tune a routineDave BallantyneP/L002Implementing Common Business Calculations in DAXChris WebbP/L005Consolidating data collection with SQLDIAG and analysing it all with SQLNexus Christian BoltonP/T007Introduction to SQL Modelling Services Robert Hogg
#SQLBITS