Docsity

Join Algorithms: Nested Loops, Block/Index Nested, Sort-Merge, Hash Join, Slides of Introduction to Database Management Systems

Partial preview of the text

Download Join Algorithms: Nested Loops, Block/Index Nested, Sort-Merge, Hash Join and more Slides Introduction to Database Management Systems in PDF only on Docsity!

Database Management Systems Design

Join Processing

• DBMS assume that all projections and selections

on single tables are take first

  • Need to project tuples need for join + tuples to be

projected in the actual results

• Then joins are computed

• We shall study 5 types of join algorithms

  • Nested Loops Join
  • Block Nested Loops Join
  • Index Nested Loops Join
  • Sort-merge Join
  • Hash Join

Nested Loops Join (2)

r r r r r r r r

R

s s s s s s

S

. . .

Need to fully scan S For each tuples in R

Nested Loops Joins (3)

  • Cost of the Join R S:
    • Cost = NPages(R) + |R|*NPages(S)
  • Usually you want the outer table to be the smallest table - But cost difference is marginal
  • Works any type of join (natural, equi-join, theta-join)
  • Example:
    • Driver (did:char(10), dname: char(20), dage: integer);
      • Cardinality = 100,000 NPages = 2,
    • Car(cid:char(6), owner: char(10), make: char(10), year: integer); - Cardinality = 40,000 NPages = 800

Block Nested Loops Join

• Idea

  • join a block from outer table with blocks of inner table
  • Two schemes
    • Page-at-a-Time
    • Block-oriented
  • Page-at-a-Time Algorithm: for each block C in R do for each block D in S do for each r ∈ C do for each s ∈ D do if r[i]=s[j] then add t = <r,s> result

Block Nested Loops Join (2)

r r r

r r r

r r

s s s

R S

s s s

Need to fully scan S For each page in R

Join 1 page of R With 1 page of S

Block Nested Loops Join (4)

  • We can do better by leveraging on Buffers
  • Load a bunch of pages from R on memory, call it T
    • T is a run of pages
  • Join this set of pages T with a page from S
  • Need B buffers for this
    • B - 2 for the run T , 1 for page of S, 1 for output page
  • Algorithm for Block Oriented NLJ: for each run T of size B - 2 Build in-memory hash table H for T using B – 2 buffers for each block D in S do for each s ∈ D do Iterator I = H.get(s[j]) // probe the hash table for each r ∈ I // iterate over matching tuples if r[i]=s[j] then add t = <r,s> result

Block Nested Loops Join (6)

r r r

r r r

s s s

S

T

s s s

r r r

r r r

R

Pages on disk

Buffer Pool

Pages on disk

Join a run of page of R With 1 page of S

Run of pages In hash table

Index Nested Loops Join

• Idea:

  • If a table has an index, and the search key K matches the join predicate, then index can be used to scan this table
  • The table that has the index becomes the inner table

• Algorithm:

Index I = S.getIndex() // get handler for index on S for each r ∈ R do Iterator T = I.search(r[i])

Index Nested Loops Join (2)

  • r
  • r
  • r
  • r
  • r
  • r
  • r
  • r
    • s
    • s
    • s
      • s R S
      • s
      • s

Index Nested Loops Join (4)

• Example:

• Join:

• Example:

  • Scenario 1: Clustered B+ tree on Car
    • Cost = 2000 + (100,000*4) = 402,000 I/Os (1.12 min)
  • Scenario 2: Clustered B+tree on Driver
    • Cost = 800 + (40,000 * 4) = 160,800 I/Os (26.8 secs)
  • Consider Scenario 1, Suppose Driver is sorted on join attribute. What happens?

Driver  did = owner Car

Sort-Merge Join

• Idea:

  • If tables are sorted on the join attribute, we can traverse them an join the matching tuples
  • In fact, it might be worth sorting the tables if not already sorted

• Algorithm has two stages:

  • Sorting phase
    • Both tables are sorted on join attribute
    • Use external sorting for this
  • Merging phase
    • Both tables are scanned and matching tuples are joinedDocsity.com

Sort-Merge Join(2)

  • Algorith:
    • Assume R is the smallest relation R2 = Sort table R; S2 = Sort table S; I1 = R2.scanIterator(); r = I1.next(); I2 = S2.scanIterator(); s = I2.next(); while there are tuples in R2 do while (r[i] < s[j]) r = I1.next(); while (s[j] < r[i]) s = I2.next();

while (s[j]==r[i]) sOld = s; while (s[j]==r[i]) add t = <r,s> result s = I2.next(); s = sOld; r = I1.next();

Sort-Merge Join (3)

• Cost of the Join R S, having B buffers for

sorting

  • Parameters:
  • Cost:
B

s

s

N
B

r

r

M

s

R NPages R

( 2 || ||(log 1 ( ) 1 )) ( 2 || ||(log 1 ( ) 1 ))

R S

R B M S B N

− +^ + − + +