SlideShare a Scribd company logo
Ami Levin, SolidQ
Presented to the Silicon Valley SQL Server User Group, April 2013
Nesting Merged Hash Loops
Ami Levin
CTO, DBSophic
SQL Server
Physical Join Operators
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
2
Not This Time
• Outer joins
• Non equi-joins
• Logical processing order
• NULL issues
• Join parallelism
• Partitioned joins
• …
3
Equi-Inner-Join
SELECT Foo, Bar, ...
FROM T1 INNER JOIN T2
ON T1.C1 = T2.C1
AND T1.C2 = T2.C2
AND ...
WHERE ...
4
Visual Join Simulator
5
Nested Loops
6
Fetch next row
from blue input
Row
exists
Quit
Find matching
rows in red input
True
False
Start
?
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
7
• 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
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
9
Nested Loops
10
Merge
11
Fetch next row
from blue input
Row
exists
Quit
Fetch next row
from red input
True
False
Start
Rows
matchTrue
False
? ?
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
12
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
Merge
14
Hash Match - Phase I (Build)
15
Fetch next row
from blue input
Row
exists
Phase II
Apply hash
function
True
False
Start
?
Hash Match - Phase II (Probe)
16
Fetch next row
from red input
Row
exists
Quit
Apply hash
function
True
False
Phase I
?
• 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
• 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
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
Hash Match
20
Summary
21
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
Low
* Excluding sorting
High
Memory Low
Low
* Excluding sorting
High
Physical IO Low / High Low Low / High
Logical reads High Low
Low
* Misleading
Output
Fast, unordered,
grouped*
Fast, ordered,
grouped
Slow, unordered,
ungrouped*
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
Physical Join Operators
23
Complete the Evaluation Form
to Win!
Win a Dell Mini Netbook – every day – just for handing
in your completed form. Each session evaluation form
represents 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
Thank you
Ami Levin, SolidQ

More Related Content

PDF
Cheat sheets for data scientists
PPT
Advanced Sql Training
PPTX
BTV PHP - Building Fast Websites
PDF
ITB2017 - Slaying the ORM dragons with cborm
PPTX
Top Ten Non-SharePoint Technical Issues that Can Doom Your Implementation
PDF
ICONUK 2016: Back From the Dead: How Bad Code Kills a Good Server
PDF
ORM Pink Unicorns
PPTX
Putting Kafka Into Overdrive
Cheat sheets for data scientists
Advanced Sql Training
BTV PHP - Building Fast Websites
ITB2017 - Slaying the ORM dragons with cborm
Top Ten Non-SharePoint Technical Issues that Can Doom Your Implementation
ICONUK 2016: Back From the Dead: How Bad Code Kills a Good Server
ORM Pink Unicorns
Putting Kafka Into Overdrive

Similar to Microsoft SQL Server Physical Join Operators (20)

PDF
Killing Shark-Riding Dinosaurs with ORM
PDF
IBM Connect 2017: Back from the Dead: When Bad Code Kills a Good Server
PDF
My site is slow
PPTX
Generating unit tests based on user logs
PDF
Outgrowing an internet startup: database administration in a fast growing com...
PDF
Navigating Transactions: ACID Complexity in Modern Databases
PDF
Navigating Transactions: ACID Complexity in Modern Databases- Mydbops Open So...
PPTX
2014 01-21-mpi-community-feedback
PPTX
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
PPTX
Hekaton introduction for .Net developers
PDF
My Site is slow - Drupal Camp London 2013
PPTX
lecture03_EmbeddedSoftware for Beginners
PPTX
Introduction to TokuDB v7.5 and Read Free Replication
PPT
It4 Coursework Help
PPTX
Data Ingestion Engine
PDF
White-box Cryptography -BayThreat 2013
PPTX
Why All the Buzz About Database Integration Solutions?
PPTX
Work Unit Analysis Tool
PDF
Hibernate ORM: Tips, Tricks, and Performance Techniques
PPTX
Do you queue
Killing Shark-Riding Dinosaurs with ORM
IBM Connect 2017: Back from the Dead: When Bad Code Kills a Good Server
My site is slow
Generating unit tests based on user logs
Outgrowing an internet startup: database administration in a fast growing com...
Navigating Transactions: ACID Complexity in Modern Databases
Navigating Transactions: ACID Complexity in Modern Databases- Mydbops Open So...
2014 01-21-mpi-community-feedback
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
Hekaton introduction for .Net developers
My Site is slow - Drupal Camp London 2013
lecture03_EmbeddedSoftware for Beginners
Introduction to TokuDB v7.5 and Read Free Replication
It4 Coursework Help
Data Ingestion Engine
White-box Cryptography -BayThreat 2013
Why All the Buzz About Database Integration Solutions?
Work Unit Analysis Tool
Hibernate ORM: Tips, Tricks, and Performance Techniques
Do you queue
Ad

More from Mark Ginnebaugh (20)

PDF
Automating Microsoft Power BI Creations 2015
PDF
Microsoft SQL Server Analysis Services (SSAS) - A Practical Introduction
PDF
Platfora - An Analytics Sandbox In A World Of Big Data
PDF
Microsoft SQL Server Relational Databases and Primary Keys
PDF
DesignMind Microsoft Business Intelligence SQL Server
PDF
San Francisco Bay Area SQL Server July 2013 meetings
PDF
Silicon Valley SQL Server User Group June 2013
PDF
Microsoft SQL Server Continuous Integration
PDF
Hortonworks Big Data & Hadoop
PDF
Microsoft Data Warehouse Business Intelligence Lifecycle - The Kimball Approach
PDF
Fusion-io Memory Flash for Microsoft SQL Server 2012
PDF
Microsoft Data Mining 2012
PDF
Microsoft SQL Server PASS News August 2012
PDF
Business Intelligence Dashboard Design Best Practices
PDF
Microsoft Mobile Business Intelligence
PDF
Microsoft SQL Server 2012 Cloud Ready
PDF
Microsoft SQL Server 2012 Master Data Services
PDF
Microsoft SQL Server PowerPivot
PDF
Microsoft SQL Server Testing Frameworks
PDF
Microsoft SQL Server - How to Collaboratively Manage Excel Data
Automating Microsoft Power BI Creations 2015
Microsoft SQL Server Analysis Services (SSAS) - A Practical Introduction
Platfora - An Analytics Sandbox In A World Of Big Data
Microsoft SQL Server Relational Databases and Primary Keys
DesignMind Microsoft Business Intelligence SQL Server
San Francisco Bay Area SQL Server July 2013 meetings
Silicon Valley SQL Server User Group June 2013
Microsoft SQL Server Continuous Integration
Hortonworks Big Data & Hadoop
Microsoft Data Warehouse Business Intelligence Lifecycle - The Kimball Approach
Fusion-io Memory Flash for Microsoft SQL Server 2012
Microsoft Data Mining 2012
Microsoft SQL Server PASS News August 2012
Business Intelligence Dashboard Design Best Practices
Microsoft Mobile Business Intelligence
Microsoft SQL Server 2012 Cloud Ready
Microsoft SQL Server 2012 Master Data Services
Microsoft SQL Server PowerPivot
Microsoft SQL Server Testing Frameworks
Microsoft SQL Server - How to Collaboratively Manage Excel Data
Ad

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Approach and Philosophy of On baking technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
sap open course for s4hana steps from ECC to s4
PPT
Teaching material agriculture food technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
Machine Learning_overview_presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Spectral efficient network and resource selection model in 5G networks
Approach and Philosophy of On baking technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25-Week II
Unlocking AI with Model Context Protocol (MCP)
sap open course for s4hana steps from ECC to s4
Teaching material agriculture food technology
Mobile App Security Testing_ A Comprehensive Guide.pdf

Microsoft SQL Server Physical Join Operators

  • 1. Ami Levin, SolidQ Presented to the Silicon Valley SQL Server User Group, April 2013 Nesting Merged Hash Loops Ami Levin CTO, DBSophic SQL Server Physical Join Operators
  • 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 2
  • 3. Not This Time • Outer joins • Non equi-joins • Logical processing order • NULL issues • Join parallelism • Partitioned joins • … 3
  • 4. Equi-Inner-Join SELECT Foo, Bar, ... FROM T1 INNER JOIN T2 ON T1.C1 = T2.C1 AND T1.C2 = T2.C2 AND ... WHERE ... 4
  • 6. Nested Loops 6 Fetch next row from blue input Row exists Quit Find matching rows in red input True False Start ?
  • 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 7
  • 8. • 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
  • 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 9
  • 11. Merge 11 Fetch next row from blue input Row exists Quit Fetch next row from red input True False Start Rows matchTrue False ? ?
  • 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 12
  • 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 13
  • 15. Hash Match - Phase I (Build) 15 Fetch next row from blue input Row exists Phase II Apply hash function True False Start ?
  • 16. Hash Match - Phase II (Probe) 16 Fetch next row from red input Row exists Quit Apply hash function True False Phase I ?
  • 17. • 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
  • 18. • 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
  • 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 19
  • 21. Summary 21 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 Low * Excluding sorting High Memory Low Low * Excluding sorting High Physical IO Low / High Low Low / High Logical reads High Low Low * Misleading Output Fast, unordered, grouped* Fast, ordered, grouped Slow, unordered, ungrouped*
  • 22. 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
  • 24. Complete the Evaluation Form to Win! Win a Dell Mini Netbook – every day – just for handing in your completed form. Each session evaluation form represents 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