Mul,-way Joins J(a,b,c) :- R(a,b) S(b,c) T(a,c) - - PowerPoint PPT Presentation

mul way joins
SMART_READER_LITE
LIVE PREVIEW

Mul,-way Joins J(a,b,c) :- R(a,b) S(b,c) T(a,c) - - PowerPoint PPT Presentation

Worst Case Op,mal Joins CompSci 590.04 Instructor: Ashwin Machanavajjhala Lecture 19 : 590.04 Fall 15 1 Mul,-way Joins


slide-1
SLIDE 1

Worst ¡Case ¡Op,mal ¡Joins ¡

CompSci ¡590.04 ¡ Instructor: ¡Ashwin ¡Machanavajjhala ¡ ¡

1 ¡ Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡

slide-2
SLIDE 2

Mul,-­‑way ¡Joins ¡

¡ ¡ ¡J(a,b,c) ¡:-­‑ ¡R(a,b) ¡S(b,c) ¡T(a,c) ¡ ¡

  • Historically ¡databases ¡designers ¡decided ¡that ¡the ¡best ¡way ¡to ¡

handle ¡mul,-­‑way ¡joins ¡is ¡to ¡do ¡them ¡one ¡pair ¡at ¡a ¡,me. ¡ ¡

– For ¡efficiency ¡reasons. ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 2 ¡

  • R3

R1 R2 R4 R3 R1 R4 R2 Left-deep plan Bushy plan

slide-3
SLIDE 3

How ¡fast ¡is ¡this ¡approach? ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 3 ¡

R “ {a0} ˆ {b0, . . . , bm} Y {a0, . . . , am} ˆ {b0} S “ {b0} ˆ {c0, . . . , cm} Y {b0, . . . , bm} ˆ {c0} T “ {a0} ˆ {c0, . . . , cm} Y {a0, . . . , am} ˆ {c0}

C A B

slide-4
SLIDE 4

How ¡fast ¡is ¡this ¡approach? ¡

  • Each ¡instance ¡has ¡2m+1 ¡rows. ¡ ¡
  • J(a, ¡b, ¡c) ¡has ¡3m+1 ¡rows ¡
  • Any ¡pairwise ¡join ¡(e.g., ¡J1(a,b,c) ¡= ¡R(a,b), ¡S(b,c)) ¡has ¡size ¡m2 ¡+ ¡m ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 4 ¡

R “ {a0} ˆ {b0, . . . , bm} Y {a0, . . . , am} ˆ {b0} S “ {b0} ˆ {c0, . . . , cm} Y {b0, . . . , bm} ˆ {c0} T “ {a0} ˆ {c0, . . . , cm} Y {a0, . . . , am} ˆ {c0}

slide-5
SLIDE 5

What ¡does ¡this ¡mean ¡for ¡triangle ¡ coun,ng? ¡

  • Every ¡database ¡system ¡necessarily ¡takes ¡O(N2) ¡

– Ignoring ¡log ¡terms ¡

  • Find ¡all ¡pairs ¡(b,c) ¡are ¡connected ¡with ¡a ¡
  • Check ¡if ¡(b,c) ¡is ¡an ¡edge. ¡ ¡
  • Is ¡this ¡the ¡best ¡we ¡can ¡do? ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 5 ¡

slide-6
SLIDE 6

Detour: ¡Can ¡Sampling ¡Help ¡Joins? ¡

  • Sample(Join(R,S)) ¡≠ ¡Join(Sample(R), ¡Sample(S)) ¡
  • In ¡R ¡x ¡S: ¡Half ¡the ¡records ¡have ¡‘a’ ¡and ¡half ¡the ¡records ¡have ¡‘b’ ¡
  • In ¡Sample(R): ¡probability ¡‘a’ ¡appears ¡is ¡very ¡small. ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 6 ¡

slide-7
SLIDE 7

Back ¡to ¡triangle ¡coun,ng? ¡

  • Every ¡database ¡system ¡necessarily ¡takes ¡O(N2) ¡

– Ignoring ¡log ¡terms ¡

  • Find ¡all ¡pairs ¡(b,c) ¡are ¡connected ¡with ¡a ¡
  • Check ¡if ¡(b,c) ¡is ¡an ¡edge. ¡ ¡
  • Is ¡this ¡the ¡best ¡we ¡can ¡do? ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 7 ¡

slide-8
SLIDE 8

We ¡can ¡do ¡becer! ¡

¡

  • … ¡not ¡only ¡for ¡triangle ¡counBng, ¡but ¡it ¡seems ¡database ¡systems ¡

have ¡been ¡doing ¡mulB-­‑way ¡joins ¡subopBmally ¡for ¡40 ¡years!!! ¡

  • Triangle ¡coun,ng ¡can ¡be ¡solved ¡in ¡O(N1.5), ¡and ¡so ¡can ¡any ¡join ¡of ¡

the ¡form ¡R(a,b) ¡S(b,c) ¡T(a,c). ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 8 ¡

slide-9
SLIDE 9

How? ¡ ¡

  • Is ¡there ¡an ¡O(N) ¡algorithm ¡for ¡the ¡following ¡join ¡problem: ¡ ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 9 ¡

R “ {a0} ˆ {b0, . . . , bm} Y {a0, . . . , am} ˆ {b0} S “ {b0} ˆ {c0, . . . , cm} Y {b0, . . . , bm} ˆ {c0} T “ {a0} ˆ {c0, . . . , cm} Y {a0, . . . , am} ˆ {c0}

slide-10
SLIDE 10

Power ¡of ¡Two ¡Choices: ¡Heavy ¡vs ¡Light ¡

  • Consider ¡acribute ¡A ¡
  • For ¡all ¡ai ¡not ¡equal ¡to ¡a0, ¡there ¡is ¡exactly ¡one ¡tuple ¡in ¡R ¡(ai, ¡b0) ¡

and ¡one ¡tuple ¡in ¡T ¡(ai, ¡c0) ¡

  • The ¡above ¡strategy ¡is ¡bad ¡for ¡a0 ¡

– Joining ¡tables ¡R ¡and ¡T ¡on ¡a0 ¡results ¡in ¡an ¡intermediate ¡of ¡N2. ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 10 ¡

(i) Compute σA“aipRq σA“aipTq and filter the re- sults by probing against S or

slide-11
SLIDE 11

Power ¡of ¡Two ¡Choices: ¡Heavy ¡vs ¡Light ¡

  • Consider ¡acribute ¡A ¡
  • For ¡all ¡ai ¡not ¡equal ¡to ¡a0, ¡there ¡is ¡exactly ¡one ¡tuple ¡in ¡R ¡(ai, ¡b0) ¡

and ¡one ¡tuple ¡in ¡T ¡(ai, ¡c0) ¡

  • For ¡ai ¡= ¡a0: ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 11 ¡

(i) Compute σA“aipRq σA“aipTq and filter the re- sults by probing against S or

(ii) Consider each tuple in pb, cq P S and check if pai, bq P R and pai, cq P T.

There ¡are ¡O(N) ¡values ¡ai, ¡each ¡resul,ng ¡in ¡a ¡ single ¡join ¡record ¡(ai, ¡b0, ¡c0). ¡Checking ¡whether ¡ (b0, ¡c0) ¡is ¡in ¡S ¡is ¡O(1) ¡… ¡assuming ¡an ¡index ¡ There ¡are ¡N ¡rows ¡in ¡S. ¡Again, ¡checking ¡(ai, ¡b) ¡is ¡in ¡ R ¡and ¡(ai, ¡c) ¡is ¡in ¡T ¡takes ¡O(1) ¡… ¡ ¡ assuming ¡an ¡index ¡

slide-12
SLIDE 12

Power ¡of ¡Two ¡Choices: ¡Heavy ¡vs ¡Light ¡

  • Consider ¡acribute ¡A ¡
  • For ¡all ¡ai ¡not ¡equal ¡to ¡a0, ¡there ¡is ¡exactly ¡one ¡tuple ¡in ¡R ¡(ai, ¡b0) ¡

and ¡one ¡tuple ¡in ¡T ¡(ai, ¡c0) ¡

  • For ¡ai ¡= ¡a0: ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 12 ¡

(i) Compute σA“aipRq σA“aipTq and filter the re- sults by probing against S or

(ii) Consider each tuple in pb, cq P S and check if pai, bq P R and pai, cq P T.

Such ¡ai’s ¡are ¡called ¡light ¡ ¡nodes. ¡Tradi,onal ¡join ¡ processing ¡works ¡here. ¡ ¡ ¡ Such ¡ai’s ¡are ¡called ¡heavy ¡nodes. ¡Need ¡to ¡ compute ¡the ¡join ¡jointly. ¡ ¡

slide-13
SLIDE 13

Power ¡of ¡Two ¡Choices ¡Algorithm ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 13 ¡

Algorithm 1 Computing Q with power of two choices. Input: RpA, Bq, S pB,Cq, TpA,Cq in sorted order

1: Q H 2: L πApRq X πApTq 3: For each a P L do 4:

If |σA“aR| ¨ |σA“aT| |S | then

5:

For each pb, cq P S do

6:

If pa, bq P R and pa, cq P T then

7:

Add pa, b, cq to Q

8:

else

9:

For each b P πBpσA“aRq ^ c P πCpσA“aTq do

10:

If pb, cq P S then

11:

Add pa, b, cq to Q

12: Return Q

Heavy ¡value ¡ Light ¡value ¡

slide-14
SLIDE 14

Run,me ¡Analysis ¡

  • Compu,ng ¡L ¡takes: ¡ ¡
  • Rest ¡of ¡the ¡algorithm ¡takes: ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 14 ¡

  • aPL

min |σA“aR| ¨ |σA“aT|, |S | .

min |σA“aR| ¨ |σA“aT|, |S | ,

  • |S | ¨
  • | ¨
  • |R| ¨
  • |T|.
slide-15
SLIDE 15

Can ¡we ¡do ¡becer? ¡ ¡

  • NO! ¡ ¡
  • A ¡matching ¡lower ¡bound ¡by ¡Atserias ¡Grohe ¡and ¡Marx ¡(or ¡the ¡

AGM ¡bound) ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 15 ¡

slide-16
SLIDE 16

AGM ¡Bound ¡

  • Let ¡V ¡denote ¡the ¡set ¡of ¡rela,ons ¡
  • Every ¡rela,on ¡is ¡a ¡subset ¡of ¡acributes ¡F ¡(or ¡a ¡hyper ¡edge) ¡
  • Let ¡x ¡be ¡a ¡vector ¡of ¡weights ¡associated ¡

with ¡each ¡rela,on ¡(hyperedge) ¡

  • Frac%onal ¡Edge ¡Cover: ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 16 ¡

A B C

T R S

xT “ 1

2

xR “ 1

2

xS “ 0 xS “ 1

2

xS ` xT “ 1 xS ` xT “ 1 xT “ 1 xR “ 1

Q

E

  • x |
  • F:vPF

xF 1, @v P V, x 0

slide-17
SLIDE 17

AGM ¡Bound ¡

|Q| “ | FPE RF|

  • FPE

|RF|xF.

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 17 ¡

slide-18
SLIDE 18

Examples ¡

  • Triples ¡query ¡
  • Best ¡frac,onal ¡cover ¡assigns ¡weight ¡0.5 ¡to ¡each ¡rela,on ¡
  • Join ¡size ¡is ¡at ¡most ¡(|R|. ¡|S|. ¡|T|)0.5 ¡
  • Another ¡frac,onal ¡cover ¡assings ¡ ¡

0 ¡to ¡rela,on ¡S ¡and ¡1 ¡each ¡to ¡R ¡and ¡T ¡

  • Join ¡size ¡is ¡at ¡most ¡|R|.|T| ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 18 ¡

A B C

T R S

xT “ 1

2

xR “ 1

2

xS “ 0 xS “ 1

2

xS ` xT “ 1 xS ` xT “ 1 xT “ 1 xR “ 1

Q

slide-19
SLIDE 19

Examples ¡

  • J(a,b,c,d) ¡:-­‑ ¡R(a,b,) ¡S(b,c) ¡T(c,d) ¡U(a,c) ¡X(a,d) ¡Y(b,d) ¡Z(c,d) ¡
  • One ¡cover ¡is ¡assigning ¡weight ¡of ¡1/(n-­‑1) ¡to ¡all ¡rela,ons ¡
  • If ¡all ¡rela,ons ¡have ¡size ¡N, ¡ ¡

Join ¡size ¡is ¡at ¡most ¡Nn/2 ¡ ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 19 ¡

A1 A2 A3 A4

R1,2 R2,4 R3,4 R2,3 R1,3 R1,4

1

slide-20
SLIDE 20

Tightest ¡AGM ¡Bound ¡

  • Answer ¡to ¡the ¡following ¡program ¡
  • Answer ¡is ¡called ¡the ¡fracBonal ¡edge ¡cover ¡number ¡

¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 20 ¡

min

  • FPE

plog2 |RF|q ¨ xF s.t.

  • F:vPF

xF 1, v P V x 0

ρ˚pQ, Dq linear program.

[19]. The AGM’s by |Q| 2ρ˚pQ,Dq.

slide-21
SLIDE 21

Mul,-­‑way ¡Joins ¡in ¡Parallel ¡Systems ¡

¡ ¡ ¡J(a,b,c) ¡:-­‑ ¡R(a,b) ¡S(b,c) ¡T(a,c) ¡ ¡

  • Historically ¡databases ¡designers ¡decided ¡that ¡the ¡best ¡way ¡to ¡

handle ¡mul,-­‑way ¡joins ¡is ¡to ¡do ¡them ¡one ¡pair ¡at ¡a ¡,me. ¡ ¡

– For ¡efficiency ¡reasons. ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 21 ¡

  • R3

R1 R2 R4 R3 R1 R4 R2 Left-deep plan Bushy plan

? ¡

slide-22
SLIDE 22

Summary ¡

  • We ¡have ¡been ¡doing ¡mul,way ¡joins ¡wrong ¡for ¡4 ¡decades. ¡
  • Worstcase ¡op,mal ¡joins ¡work ¡by ¡carefully ¡iden,fying ¡skew ¡in ¡the ¡

data ¡and ¡using ¡different ¡algorithms ¡depending ¡on ¡the ¡skew ¡of ¡the ¡

  • tuple. ¡
  • Bushy ¡mul,way ¡joins ¡maybe ¡useful ¡in ¡parallel ¡serngs. ¡ ¡

Lecture ¡19 ¡: ¡590.04 ¡Fall ¡15 ¡ 22 ¡