Microsoft SQL Server Physical Join Operators
-
Upload
mark-ginnebaugh -
Category
Technology
-
view
1.841 -
download
0
description
Transcript of Microsoft SQL Server Physical Join Operators
![Page 1: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/1.jpg)
Ami Levin, SolidQPresented to the Silicon Valley SQL Server User Group, April 2013
Nesting Merged Hash Loops
Ami LevinCTO, DBSophic
SQL ServerPhysical Join Operators
![Page 2: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/2.jpg)
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 theoptimizer’s decisions on which operator to use
• Learn to identify common join-related pitfalls
2
![Page 3: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/3.jpg)
Not This Time
• Outer joins
• Non equi-joins
• Logical processing order
• NULL issues
• Join parallelism
• Partitioned joins
• …
3
![Page 4: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/4.jpg)
Equi-Inner-Join
SELECT Foo, Bar, ...
FROM T1 INNER JOIN T2
ON T1.C1 = T2.C1
AND T1.C2 = T2.C2
AND ...
WHERE ...
4
![Page 5: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/5.jpg)
Visual Join Simulator
5
![Page 6: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/6.jpg)
Nested Loops
6
Fetch next rowfrom blue input
Rowexists
Quit
Find matchingrows in red input
True
False
Start
?
![Page 7: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/7.jpg)
Nested Loops I
• Outer loop determines number of iterations
• At least one input should be (relatively) small
• Inner operation is performed for everyiteration of the outer loop
• Index or table scan (naïve)
• Index seek + lookup
• Covering index seek
• Index spool
7
![Page 8: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/8.jpg)
• 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
Nested Loops II
8
![Page 9: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/9.jpg)
Nested Loopswith 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 oftenenables efficient use of nested loops
9
![Page 10: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/10.jpg)
Nested Loops
10
![Page 11: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/11.jpg)
Merge
11
Fetch next rowfrom blue input
Rowexists
Quit
Fetch next rowfrom red input
True
False
Start
RowsmatchTrue
False
? ?
![Page 12: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/12.jpg)
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 additionalplan operations
• Merge join types
• One to many
• Many to many - requires temporary worktable
12
![Page 13: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/13.jpg)
Merge II
• Residual predicates
• Fast, ordered and grouped output
• Physical resources
• CPU Very low
• Physical IO Very low
• Memory Very low
• * Excluding sorting costs
13
![Page 14: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/14.jpg)
Merge
14
![Page 15: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/15.jpg)
Hash Match - Phase I (Build)
15
Fetch next rowfrom blue input
Rowexists
Phase II
Apply hashfunction
True
False
Start
?
![Page 16: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/16.jpg)
Hash Match - Phase II (Probe)
16
Fetch next rowfrom red input
Rowexists
Quit
Apply hashfunction
True
False
Phase I
?
![Page 17: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/17.jpg)
• Hash function selection
• 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
Hash Match I
17
![Page 18: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/18.jpg)
• In memory hash join
Grace hash join
Recursive hash join
• Hash bailout
• Hash warnings event class
• Update Statistics
• Add more RAM
• Role reversal
Hash Match II
18
![Page 19: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/19.jpg)
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
19
![Page 20: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/20.jpg)
Hash Match
20
![Page 21: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/21.jpg)
Summary
21
Nested Loops Merge Hash
Good whenSmall outer input
Inner input indexed
Pre-sorted inputs
Sorting needed
Very large inputs
Not well indexed
CPU LowLow
* Excluding sortingHigh
Memory LowLow
* Excluding sortingHigh
Physical IO Low / High Low Low / High
Logical reads High LowLow
* Misleading
OutputFast, unordered,
grouped*
Fast, ordered,
grouped
Slow, unordered,
ungrouped*
![Page 22: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/22.jpg)
For More Information
• Books on line
• White papers
• “Inside Microsoft SQL server” books
• Craig Freedman’s blog
• http://blogs.msdn.com/craigfr/about.aspx
22
![Page 23: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/23.jpg)
Physical Join Operators
23
![Page 24: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/24.jpg)
Complete the Evaluation Formto Win!Win a Dell Mini Netbook – every day – just for handingin your completed form. Each session evaluation formrepresents a chance to win.
Pick up your evaluation form:• In each presentation room
• Online on the PASS Summit website
Drop off your completed form:• Near the exit of each presentation room
• At the Registration desk
• Online on the PASS Summit website
Sponsored by Dell
24
![Page 25: Microsoft SQL Server Physical Join Operators](https://reader034.fdocuments.in/reader034/viewer/2022051411/54708af3b4af9fb40a8b481a/html5/thumbnails/25.jpg)
Thank youAmi Levin, SolidQ