Presentation is loading. Please wait.

Presentation is loading. Please wait.

Physical Join Operators

Similar presentations


Presentation on theme: "Physical Join Operators"— Presentation transcript:

1 Physical Join Operators
Nesting Merged Hash Loops Ami Levin Think Big. Move Fast.

2 Session Goals SQL Server uses three physical join operators: Nested loops, Merge, and Hash Match. In this session we will: See how each of these operators work Review their advantages and drawbacks Understand some of the logic behind the optimizer’s decisions on which operator to use Learn to identify common join-related pitfalls AD-400|Physical Join Operators

3 Not This Time Outer joins Non equi-joins Logical processing order
NULL issues Join parallelism Partitioned joins AD-400|Physical Join Operators

4 Equi-Inner-Join SELECT Foo, Bar, ... FROM T1 INNER JOIN T2 ON T1.C1 = T2.C1 AND T1.C2 = T2.C2 AND ... WHERE ... AD-400|Physical Join Operators

5 Visual Join Simulator AD-400|Physical Join Operators

6 Nested Loops ? Fetch next row from blue input Start Row exists Quit
False True Find matching rows in red input AD-400|Physical Join Operators

7 Nested Loops I Outer loop determines number of iterations
At least one input should be (relatively) small Inner operation is performed for every iteration of the outer loop Index or table scan (naïve) Index seek + lookup Covering index seek Index spool AD-400|Physical Join Operators

8 Nested Loops II Data pages may be accessed repeatedly
Risky a-sequential page access path Output of matching row sets is fast Unordered, but typically grouped Physical resources CPU Very low Physical IO low to very high Memory low AD-400|Physical Join Operators

9 Nested Loops with Foreign Key Joins
Foreign keys join parent and child Most common relationship is one-to-many Often parent input is significantly smaller Parent must already be indexed Either primary key or unique constraint Therefore, indexing foreign keys often enables efficient use of nested loops AD-400|Physical Join Operators

10 Nested Loops DEMO AD-400|Physical Join Operators

11 Merge ? ? Fetch next row from blue input Start False Row exists
Rows match Quit ? ? False True True Fetch next row from red input AD-400|Physical Join Operators

12 Merge I Inputs must be sorted prior to merge
Sorted by (all?) join expression(s) Pre-sorted in plan, but not necessarily in DB Preferred when sorting supports additional plan operations Merge join types One to many Many to many - requires temporary worktable AD-400|Physical Join Operators

13 Merge II Residual predicates Fast, ordered and grouped output
Physical resources CPU Very low Physical IO Very low Memory Very low * Excluding sorting costs AD-400|Physical Join Operators

14 Merge DEMO AD-400|Physical Join Operators

15 Hash Match - Phase I (Build)
Start Fetch next row from blue input Row exists Phase II ? False True Apply hash function AD-400|Physical Join Operators

16 Hash Match - Phase II (Probe)
Fetch next row from red input Row exists Quit ? False True Apply hash function AD-400|Physical Join Operators

17 Hash Match I Hash function selection Build and probe costs are hidden
Extremely complex CPU intensive Build and probe costs are hidden Do not constitute logical reads Output of matching row sets is slow Unordered and typically ungrouped AD-400|Physical Join Operators

18 Hash Match II In memory hash join Grace hash join Recursive hash join
Hash bailout Hash warnings event class Update Statistics Add more RAM Role reversal AD-400|Physical Join Operators

19 Hash Match III May indicate sub-optimal indexing
Best for very large, non covered joins Physical resources CPU Very high Physical IO Low to very high Memory Very high AD-400|Physical Join Operators

20 Hash Match DEMO AD-400|Physical Join Operators

21 Summary Nested Loops Merge Hash Good when CPU Memory Physical IO
Nested Loops Merge Hash Good when Small outer input Inner input indexed Pre-sorted inputs Sorting needed Very large inputs Not well indexed CPU Low * Excluding sorting High Memory Physical IO Low / High Logical reads * Misleading Output Fast, unordered, grouped* Fast, ordered, grouped Slow, unordered, ungrouped* AD-400|Physical Join Operators

22 For More Information Books on line White papers
“Inside Microsoft SQL server” books Craig Freedman’s blog AD-400|Physical Join Operators

23 Physical Join Operators
Q&A AD-400|Physical Join Operators

24 Session Code | Session Title

25 Thank you


Download ppt "Physical Join Operators"

Similar presentations


Ads by Google