1 implementation of relational operations chapters 12 ~ 14

56
Implementation of Relational Operations Chapters 12 ~ 14

Upload: alexandra-singleton

Post on 27-Dec-2015

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1 Implementation of Relational Operations Chapters 12 ~ 14

1

Implementation of Relational

OperationsChapters 12 ~ 14

Page 2: 1 Implementation of Relational Operations Chapters 12 ~ 14

2

Introduction

• We’ve covered the basic underlying storage, buffering, and indexing technology.– Now we can move on to query processing.

• Some database operations are EXPENSIVE• Can greatly improve performance by being “smart”

– e.g., can speed up 1,000,000x over naïve approach• Main weapons are:

– clever implementation techniques for operators– exploiting “equivalencies” of relational operators– using statistics and cost models to choose among these.

• First: basic operators• Then: join• After that: optimizing multiple operators

Page 3: 1 Implementation of Relational Operations Chapters 12 ~ 14

3

Relational Operations• We will consider how to implement:

– Selection ( ) Selects a subset of rows from relation.

– Projection ( ) Deletes unwanted columns from relation.– Join ( ) Allows us to combine two relations.– Set-difference ( — ) Tuples in reln. 1, but not in reln. 2.

– Union ( ) Tuples in reln. 1 and in reln. 2.– Aggregation (SUM, MIN, etc.) and GROUP BY

• Since each op returns a relation, ops can be composed! After we cover the operations, we will discuss how to optimize queries formed by composing them.

Page 4: 1 Implementation of Relational Operations Chapters 12 ~ 14

4

Schema for Examples

• Similar to old schema; rname added for variations.

• Reserves:– Each tuple is 40 bytes long, 100 tuples per page,

1000 pages.• Sailors:

– Each tuple is 50 bytes long, 80 tuples per page, 500 pages.

Sailors (sid: integer, sname: string, rating: integer, age: real)Reserves (sid: integer, bid: integer, day: dates, rname: string)

Page 5: 1 Implementation of Relational Operations Chapters 12 ~ 14

5

Simple Selections

• Of the form• Question: how best to perform? Depends on:

– what indexes/access paths are available– what is the expected size of the result (in terms of

number of tuples and/or number of pages)• Size of result (cardinality) approximated as

size of R * reduction factor – “reduction factor” is usually called selectivity.– estimate of reduction factors is based on statistics

– we will discuss later.

SELECT *FROM Reserves RWHERE R.rname < ‘C%’

R attr valueop R. ( )

Page 6: 1 Implementation of Relational Operations Chapters 12 ~ 14

6

Simple Selections (cont)

• With no index, unsorted: – Must essentially scan the whole relation– cost is M (#pages in R). For “reserves” = 1000 I/Os.

• With no index, sorted:– cost of binary search + number of pages containing

results. – For reserves = 10 I/Os + selectivity*#pages

• With an index on selection attribute: – Use index to find qualifying data entries, – then retrieve corresponding data records. – Cost?

Page 7: 1 Implementation of Relational Operations Chapters 12 ~ 14

7

Using an Index for Selections• Cost depends on #qualifying tuples, and

clustering.– Cost:

• finding qualifying data entries (typically small) • plus cost of retrieving records (could be large w/o

clustering).– In example “reserves” relation, if 10% of tuples

qualify (100 pages, 10000 tuples). • With a clustered index, cost is little more than

100 I/Os;• If unclustered, could be up to 10000 I/Os!

– Unless you get fancy…

Page 8: 1 Implementation of Relational Operations Chapters 12 ~ 14

8

Selections using Index (cont)• Important refinement for unclustered indexes:

1. Find qualifying data entries.2. Sort the rid’s of the data records to be retrieved.3. Fetch rids in order. This ensures that each data

page is looked at just once (though # of such pages likely to be higher than with clustering).

Index entries

Data entries

direct search for

(Index File)

(Data file)

Data Records

data entries

Data entries

Data Records

CLUSTERED

Page 9: 1 Implementation of Relational Operations Chapters 12 ~ 14

9

General Selection Conditions

• Such selection conditions are first converted to conjunctive normal form (CNF): – (day<8/9/94 OR bid=5 OR sid=3 ) AND

(rname=‘Paul’ OR bid=5 OR sid=3) • We only discuss the case with no ORs (a conjunction

of terms of the form attr op value).• A B-tree index matches (a conjunction of) terms that

involve only attributes in a prefix of the search key.– Index on <a, b, c> matches a=5 AND b= 3, but not b=3.

• (For Hash index, must have all attrs in search key)

(day<8/9/94 AND rname=‘Paul’) OR bid=5 OR sid=3

Page 10: 1 Implementation of Relational Operations Chapters 12 ~ 14

10

Two Approaches to General Selections

• First approach: Find the most selective access path, retrieve tuples using it, and apply any remaining terms that don’t match the index:– Most selective access path: An index or file scan

that we estimate will require the fewest page I/Os.– Terms that match this index reduce the number of

tuples retrieved; other terms are used to discard some retrieved tuples, but do not affect number of tuples/pages fetched.

Page 11: 1 Implementation of Relational Operations Chapters 12 ~ 14

11

Most Selective Index - Example

• Consider day<8/9/94 AND bid=5 AND sid=3.• A B+ tree index on day can be used;

– then, bid=5 and sid=3 must be checked for each retrieved tuple.

• Similarly, a hash index on <bid, sid> could be used; – Then, day<8/9/94 must be checked.

• How about a B+tree on <rname,day>?• How about a B+tree on <day, rname>?• How about a Hash index on <day, rname>?

Page 12: 1 Implementation of Relational Operations Chapters 12 ~ 14

12

Intersection of Rids• Second approach: if we have 2 or more matching

indexes (w/Alternatives (2) or (3) for data entries):– Get sets of rids of data records using each matching

index.– Then intersect these sets of rids.– Retrieve the records and apply any remaining terms.– Consider day<8/9/94 AND bid=5 AND sid=3. With a B+

tree index on day and an index on sid, we can retrieve rids of records satisfying day<8/9/94 using the first, rids of recs satisfying sid=3 using the second, intersect, retrieve records and check bid=5.

– Note: commercial systems use various tricks to do this:• bit maps, bloom filters, index joins

Page 13: 1 Implementation of Relational Operations Chapters 12 ~ 14

13

External Sorting

• Before presenting projection operators, we here present external sorting schemes

Page 14: 1 Implementation of Relational Operations Chapters 12 ~ 14

14

2-Way Sort

• Pass 0: Read a page, sort it, write it.– only one buffer page is used (as in previous slide)

• Pass 1, 2, …, etc.:– requires 3 buffer pages– merge pairs of runs into runs twice as long– three buffer pages used.

Main memory buffers

INPUT 1

INPUT 2

OUTPUT

DiskDisk

Page 15: 1 Implementation of Relational Operations Chapters 12 ~ 14

15

Two-Way External Merge Sort

• Each pass we read + write each page in file.

• N pages in the file => the number of passes

• So total cost is:

• Idea: Divide and conquer: sort subfiles and merge

log2 1N

2 12N Nlog

Input file

1-page runs

2-page runs

4-page runs

8-page runs

PASS 0

PASS 1

PASS 2

PASS 3

9

3,4 6,2 9,4 8,7 5,6 3,1 2

3,4 5,62,6 4,9 7,8 1,3 2

2,34,6

4,7

8,91,35,6 2

2,3

4,46,7

8,9

1,23,56

1,22,3

3,4

4,56,6

7,8

Page 16: 1 Implementation of Relational Operations Chapters 12 ~ 14

16

General External Merge Sort (B-1 way external merge sort)

• To sort a file with N pages using B buffer pages:– Pass 0: use B buffer pages. Produce sorted

runs of B pages each. – Pass 1, 2, …, etc.: merge B-1 runs.

N B/

B Main memory buffers

INPUT 1

INPUT B-1

OUTPUT

DiskDisk

INPUT 2

. . . . . .

. . .

More than 3 buffer pages. How can we utilize them?

Page 17: 1 Implementation of Relational Operations Chapters 12 ~ 14

17

Cost of External Merge Sort

• Number of passes:• Cost = 2N * (# of passes)• E.g., with 5 buffer pages, to sort 108 page

file:– Pass 0: = 22 sorted runs of 5 pages

each (last run is only 3 pages) • Now, do four-way (B-1) merges

– Pass 1: = 6 sorted runs of 20 pages each (last run is only 8 pages)

– Pass 2: 2 sorted runs, 80 pages and 28 pages– Pass 3: Sorted file of 108 pages

1 1 log /B N B

108 5/

22 4/

Page 18: 1 Implementation of Relational Operations Chapters 12 ~ 14

18

Number of Passes of External Sort

N B=3 B=5 B=9 B=17 B=129 B=257100 7 4 3 2 1 11,000 10 5 4 3 2 210,000 13 7 5 4 2 2100,000 17 9 6 5 3 31,000,000 20 10 7 5 3 310,000,000 23 12 8 6 4 3100,000,000 26 14 9 7 4 41,000,000,000 30 15 10 8 5 4

( I/O cost is 2N times number of passes)

Page 19: 1 Implementation of Relational Operations Chapters 12 ~ 14

19

Internal Sort Algorithm• Quicksort is a fast way to sort in memory.• Alternative: “tournament sort” (a.k.a. “heapsort”,

“replacement selection”)• Keep two heaps in memory, H1 and H2

read B-2 pages of records, inserting into H1;while (records left) {

m = H1.removemin(); put m in output buffer;if (H1 is empty)

H1 = H2; H2.reset(); start new output run; else

read in a new record r (use 1 buffer for input pages);if (r < m) H2.insert(r);else H1.insert(r);

}H1.output(); start new run; H2.output();

•average length of a run is 2B

Page 20: 1 Implementation of Relational Operations Chapters 12 ~ 14

20

Projection (DupElim)

• Issue is removing duplicates.• Basic approach is to use sorting

– 1. Scan R, extract only the needed attrs (why do this 1st?)– 2. Sort the resulting set– 3. Remove adjacent duplicates– Cost: Reserves with size ratio 0.25 = 250 pages. With 20

buffer pages can sort in 2 passes, so1000 +250 + 2 * 2 * 250 + 250 = 2500 I/Os

• Can improve by modifying external sort algorithm– Modify Pass 0 of external sort to eliminate unwanted fields. – Modify merging passes to eliminate duplicates.– Cost: for above case: read 1000 pages, write out 250 in runs

of 40(=2B) (pages, merge runs = 1000 + 250 +250 = 1500.

SELECT DISTINCT R.sid, R.bidFROM Reserves R

Page 21: 1 Implementation of Relational Operations Chapters 12 ~ 14

21

DupElim Based on Hashing

• Partition Phasefor each page p in R read page p for each tuple t in p project out the unwanted attributes apply a hash function h to the combination of all remaining attributes write t to the output buffer page that it is hashed to by h (B-1 partitions)

• Duplication Elimination Phasefor each partition q in B-1 partitions for each page p in q read page p for each tuple t in p hash t by applying hash function h2 and insert it into an in-memory hash table discard duplicate as they are detected

B main memory buffers DiskDisk

Original Relation OUTPUT

2INPUT

1

hashfunction

h B-1

Partitions

1

2

B-1. . .

Page 22: 1 Implementation of Relational Operations Chapters 12 ~ 14

22

Main Problem in Hash-Based Duplicate Elimination

• Partition overflow– The size of the in-memory hash table for a

partition is greater than the number of available buffer pages B

• Solution– Recursively apply the hash-based projection

technique to eliminate the duplicates in each partition that overflows

Page 23: 1 Implementation of Relational Operations Chapters 12 ~ 14

23

How many buffer pages should be allocated to prevent the partition overflow problem?• T: # of pages of tuples after the

projection• T/(B-1) : sizeof(partition)• T/(B-1)*f: sizeof(in-memory hash table)

B > T/(B-1)*fB > sqrt(f*T + B)Approximately, B > sqrt(f*T)

Page 24: 1 Implementation of Relational Operations Chapters 12 ~ 14

24

• Cost for Hashing? – assuming partitions fit in memory (i.e.

#bufs >= square root of the #of pages of projected tuples)

– read 1000 pages and write out partitions of projected tuples (250 pages)

– Do dup elim on each partition (total 250 page reads)

– Total : 1500 I/Os.

Page 25: 1 Implementation of Relational Operations Chapters 12 ~ 14

25

Commercial Systems

• Informix hashing• DB2, Oracle 8, Sybase ASE sorting• MS SQL Server, Sybase ASIQ both

Page 26: 1 Implementation of Relational Operations Chapters 12 ~ 14

26

Sorting vs Hashing for Projections• Sorting-based approach is superior to hashing

– If we have many duplicates– If the distribution of hash values is very

nonuniform– The result of sorting is sorted (interesting order)– Sorting utility is a standard package in DBMSs

• If B> sqrt(T) buffer pages, two schemes have the same I/O cost

Page 27: 1 Implementation of Relational Operations Chapters 12 ~ 14

27

DupElim & Indexes• If an index on the relation contains all wanted

attributes in its search key, can do index-only scan.– Apply projection techniques to data entries (much

smaller!)• If an ordered (i.e., tree) index contains all wanted

attributes as prefix of search key, can do even better:– Retrieve data entries in order (index-only scan),

discard unwanted fields, compare adjacent tuples to check for duplicates.

• Same tricks apply to GROUP BY/Aggregation

Page 28: 1 Implementation of Relational Operations Chapters 12 ~ 14

28

Joins

• Joins are very common• Joins are very expensive (worst case:

cross product!)• Many approaches to reduce join cost

Page 29: 1 Implementation of Relational Operations Chapters 12 ~ 14

29

Equality Joins With One Join Column

• In algebra: R S. Common! Must be carefully optimized. R S is large; so, R S followed by a selection is inefficient.

• Note: join is associative and commutative.• Assume:

– M pages in R, pR tuples per page– N pages in S, pS tuples per page.– In our examples, R is Reserves and S is Sailors.

• We will consider more complex join conditions later.• Cost metric : # of I/Os. We will ignore output costs.

SELECT *FROM Reserves R1, Sailors S1WHERE R1.sid=S1.sid

Page 30: 1 Implementation of Relational Operations Chapters 12 ~ 14

30

foreach tuple r in R doforeach tuple s in S do

if ri == sj then add <r, s> to result

Simple Nested Loops Join

• For each tuple in the outer relation R, we scan the entire inner relation S.

• How much does this Cost?

• (pR * M) * N + M = 100*1000*500 + 1000 I/Os.

– At 10ms/IO, Total: ???• What if smaller relation (S) was outer?

– (80*500)*1000+500 = 40,000,500• What assumptions are being made here?

Q: What is cost if one relation can fit entirely in memory?

Page 31: 1 Implementation of Relational Operations Chapters 12 ~ 14

31

Page-Oriented Nested Loops Join

• For each page of R, get each page of S, and write out matching pairs of tuples <r, s>, where r is in R-page and S is in S-page.

• What is the cost of this approach?

• M*N + M= 1000*500 + 1000– If smaller relation (S) is outer, cost = 500*1000 +

500

foreach page bR in R do foreach page bS in S do foreach tuple r in bR do

foreach tuple s in bSdoif ri == sj then add <r, s> to result

Page 32: 1 Implementation of Relational Operations Chapters 12 ~ 14

32

Index Nested Loops Join

• If there is an index on the join column of one relation (say S), can make it the inner and exploit the index.

– Cost: M + ( (M*pR) * cost of finding matching S tuples)

• For each R tuple, cost of probing S index is about 2-4 IOs for B+ tree. Cost of then finding S tuples (assuming Alt. (2) or (3) for data entries) depends on clustering.

• Clustered index: 1 I/O per page of matching S tuples.• Unclustered: up to 1 I/O per matching S tuple.

foreach tuple r in R doforeach tuple s in S where ri == sj do

add <r, s> to result

Page 33: 1 Implementation of Relational Operations Chapters 12 ~ 14

33

Examples of Index Nested Loops• B+-tree index (Alt. 2) on sid of Sailors (as inner):

– Scan Reserves: 1000 page I/Os, 100*1000 tuples.– For each Reserves tuple: 1.2 I/Os to get data entry in index,

plus 1 I/O to get (the exactly one) matching Sailors tuple. – Total: 1000+100*1000*2.2

• B+-Tree index (Alt. 2) on sid of Reserves (as inner):– Scan Sailors: 500 page I/Os, 80*500 tuples.– For each Sailors tuple: 1.2 I/Os to find index page with data

entries, plus cost of retrieving matching Reserves tuples. – Assuming uniform distribution, 2.5 reservations per sailor

(100,000 / 40,000). Cost of retrieving them is 1 or 2.5 I/Os depending on whether the index is clustered.

– Totals: • Clustered index: 500 + 40,000 * 1.2 + 40,000 * 1 = 88,500 I/Os• Nonclustered index: 500 + 40,000 * 1.2 + 40,000 * 2.5 =

148,500 I/Os

Page 34: 1 Implementation of Relational Operations Chapters 12 ~ 14

34

“Block” Nested Loops Join• Page-oriented NL doesn’t exploit extra buffers.• Alternative approach: Use one page as an input

buffer for scanning the inner S, one page as the output buffer, and use all remaining pages to hold ``block’’ (think “chunk”) of outer R.

• For each matching tuple r in R-chunk, s in S-page, add <r, s> to result. Then read next R-chunk, scan S, etc.

Page 35: 1 Implementation of Relational Operations Chapters 12 ~ 14

35

“Block” Nested Loops Join Algorithm

for each block of B-2 pages of Rfor each page of S

for all matching in-memory tuples rR-block and s S-block

add <r,s> to result

. . .

. . .

R & Schunk of R tuples

(k < B-1 pages)

Input buffer for S Output buffer

. . .

Join Result

Page 36: 1 Implementation of Relational Operations Chapters 12 ~ 14

36

Examples of Block Nested Loops• Cost: Scan of outer + #outer chunks * scan of inner =

(M+N*M/(B-2))– #outer chunks =

• With Reserves (R) as outer, and 100 pages of R:– Cost of scanning R is 1000 I/Os; a total of 10 chunks.– Per chunk of R, we scan Sailors (S); 10*500 I/Os.– If space for just 90 pages of R, we would scan S 12 times.

• With 100-page chunk of Sailors as outer:– Cost of scanning S is 500 I/Os; a total of 5 chunks.– Per chunk of S, we scan Reserves; 5*1000 I/Os.

• If you consider seeks, it may be best to divide buffers evenly between R and S.– Disk arm “jogs” between read of S and write of output– If output is not going to disk, this is not an issue

#of pagesof outer /chunksize

Page 37: 1 Implementation of Relational Operations Chapters 12 ~ 14

37

Sort-Merge Join (R S)

• Sort R and S on the join column, then scan them to do a ``merge’’ (on join col.), and output result tuples.

• Useful if– One or both inputs already sorted on join attribute(s)– Output should be sorted on join attribute(s)

i=j

Page 38: 1 Implementation of Relational Operations Chapters 12 ~ 14

38

• General scheme:– Do { Advance scan of R until current R-tuple >= current S tuple;

Advance scan of S until current S-tuple >= current R tuple; } Until current R tuple = current S tuple.– At this point, all R tuples with same value in Ri (current R

group) and all S tuples with same value in Sj (current S group) match; output <r, s> for all pairs of such tuples.

• Like a mini nested loops

– Then resume scanning R and S.• R is scanned once; each S group is scanned once per

matching R tuple. (Multiple scans of an S group will probably find needed pages in buffer.)

Page 39: 1 Implementation of Relational Operations Chapters 12 ~ 14

39

Example of Sort-Merge Join

• Cost: M log M + N log N + (M+N)– The cost of scanning, M+N, could be M*N (very unlikely!)

• With 35, 100 or 300 buffer pages, both Reserves and Sailors can be sorted in 2 passes; total join cost: 7500.

sid sname rating age22 dustin 7 45.028 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0

sid bid day rname

28 103 12/4/96 guppy28 103 11/3/96 yuppy31 101 10/10/96 dustin31 102 10/12/96 lubber31 101 10/11/96 lubber58 103 11/12/96 dustin

(BNL cost: 2500 to 15000 I/Os)

Page 40: 1 Implementation of Relational Operations Chapters 12 ~ 14

40

• Sort-Merge Join Cost : With 35 buffers (or 100, 300 buffers)– Sorting Reserves : 2*(1+log331000/35) *1,000 =

4,000– Sorting Sailors : 2*(1+log33500/35) *500 = 2,000– Merging: 1000 + 500= 7,500

• Block Nested Loop Join Cost:– With 35 buffers : 16,500– With 100 buffers: 6,500– With 300 buffers: 2,500

Page 41: 1 Implementation of Relational Operations Chapters 12 ~ 14

41

Refinement of Sort-Merge Join

• Goal: • First, we produce sorted runs of size B for both R and S• If B > sqrt(L), where L is the size of the larger relation,

the number of runs per relation is less than sqrt(L)• We allocate one buffer page for each run of R and one

for each run of S (thus, # of buffers needed is 2*sqrt(L))• We then merge the runs of R (to generate the sorted

version of R), merge the runs of S, and merge the resulting R and S streams as they are generated

This idea increases # of buffers required to 2*sqrt(L)

Page 42: 1 Implementation of Relational Operations Chapters 12 ~ 14

42

• By using the aggressive algorithm, we can produce sorted runs of size approximately 2*B for both R and S # of runs per relation < sqrt(L)/2

• Thus, we can combine the merge phases with no need for additional buffers

• We can perform a sort-merge join at the cost of 3*(M+N)– 2*(M+N) : reading and writing R and S in the first pass– M+N : reading R and S in the second pass

• In example, cost goes down from 7500 to 4500 I/Os.

Page 43: 1 Implementation of Relational Operations Chapters 12 ~ 14

43

Hash-Join• Partition both

relations using hash fn h: R tuples in partition i will only match S tuples in partition i.

• Read in a partition of R, hash it using h2 (<> h!). Scan matching partition of S, probe hash table for matches.

Partitionsof R & S

Input bufferfor Si

Hash table for partitionRi (k < B-1 pages)

B main memory buffersDisk

Output buffer

Disk

Join Result

hashfnh2

h2

B main memory buffers DiskDisk

Original Relation OUTPUT

2INPUT

1

hashfunction

h B-1

Partitions

1

2

B-1

. . .

Page 44: 1 Implementation of Relational Operations Chapters 12 ~ 14

44

Grace Hash Join Algorithm// Partition R into k partitionsforeach tuple r in R do read r and add it to buffer page h(ri)

// Partition S into k partitionsforeach tuple s in R do read s and add it to buffer page h(sj)

// Probing phasefor m = 1, …, k do { // Build in-memory hash table for Rm, using h2 foreach tuple r in partition Rm do read r and insert into hash table using h2(ri) // Scan Sm and probe for matching Rm tuples foreach tuple s in partition Sm do { read s and insert into hash table using h2(sj) for matching R tuples r, output <r,s> }}

Page 45: 1 Implementation of Relational Operations Chapters 12 ~ 14

45

Memory Requirements

• B: the total number of buffers• The size of each R partition: M/(B-1)

• During the probing phase,B > f*(M/(B-1)) + 1 + 1

hash table for the R partition

A buffer for scanning the S partition

an output buffer

• Approximately, B > sqrt(f*M)

Page 46: 1 Implementation of Relational Operations Chapters 12 ~ 14

46

Overflow Handling

• If the hash function does not partition uniformly, one or more R partitions may not fit in memory.

• Can apply hash-join technique recursively to do the join of this R-partition with corresponding S-partition.

Page 47: 1 Implementation of Relational Operations Chapters 12 ~ 14

47

Cost of Hash-Join

• In partitioning phase, read+write both relns; 2(M+N). In matching phase, read both relns; M+N I/Os.

• In our running example, this is a total of 4500 I/Os.

• Sort-Merge Join vs. Hash Join:– Given a minimum amount of memory (what is this, for

each?) both have a cost of 3(M+N) I/Os. Hash Join superior on this count if relation sizes differ greatly. Also, Hash Join shown to be highly parallelizable.

– Sort-Merge less sensitive to data skew; result is sorted.

Page 48: 1 Implementation of Relational Operations Chapters 12 ~ 14

48

Utilizing Extra Memory: Hybrid Hash Join

• If B > (k+1) + f*(M/k), // k : # of partitions– We have enough extra memory (>f*(M/k)) during the

partitioning phase to hold an in-memory hash table for a partition of R (smaller relation)

• To build an in-memory hash table for the first partition of R during the partition phase, which means we do not write this partition to disk

• While partitioning S, we can directly probe the in-memory table for the first R partition

• At the end of the partitioning phase, we have completed the first partitions of R and S, in addition to partitioning the two relations; in the probing phase, we join the remaining partitions as in hash join

Page 49: 1 Implementation of Relational Operations Chapters 12 ~ 14

49

• Cost– B = 300 and size(partition) = 250– Partition phase,

• scan R and write out one partition : 500 + 250• Scan S and write out one partition : 1000 + 500

– Probing phase,• Scan the second partition of R and of S: 250 +

500

– Total cost: 750+1500+750 = 3,000 I/Os

Page 50: 1 Implementation of Relational Operations Chapters 12 ~ 14

50

General Join Conditions• Equalities over several attributes (e.g., R.sid=S.sid

AND R.rname=S.sname):– For Index NL, build index on <sid, sname> (if S is inner);

or use existing indexes on sid or sname.– For Sort-Merge and Hash Join, sort/partition on

combination of the two join columns.• Inequality conditions (e.g., R.rname < S.sname):

– For Index NL, need (clustered!) B+ tree index.• Range probes on inner; # matches likely to be much higher than

for equality joins.

– Hash Join not applicable!– Block NL and Sort Merge Join quite likely to be the best

join method here.

Page 51: 1 Implementation of Relational Operations Chapters 12 ~ 14

51

Set Operations• Intersection and cross-product special cases of

join.• Union (Distinct) and Except similar; we’ll do union.• Sorting based approach to union:

– Sort both relations (on combination of all attributes).– Scan sorted relations and merge them.– Alternative: Merge runs from Pass 0 for both relations.

• Hash based approach to union:– Partition R and S using hash function h.– For each S-partition, build in-memory hash table (using

h2), scan corr. R-partition and add tuples to table while discarding duplicates.

Page 52: 1 Implementation of Relational Operations Chapters 12 ~ 14

52

Impact of Buffering• If several operations are executing

concurrently, estimating the number of available buffer pages is guesswork.

• Repeated access patterns interact with buffer replacement policy.– e.g., Inner relation is scanned repeatedly in

Simple Nested Loop Join. With enough buffer pages to hold inner, replacement policy does not matter. Otherwise, pinning a few pages is best, LRU is worst (sequential flooding).

– Does replacement policy matter for Block Nested Loops?

– What about Index Nested Loops? Sort-Merge Join?

Page 53: 1 Implementation of Relational Operations Chapters 12 ~ 14

53

Summary• A virtue of relational DBMSs: queries are

composed of a few basic operators; the implementation of these operators can be carefully tuned (and it is important to do this!).

• Many alternative implementation techniques for each operator; no universally superior technique for most operators.

• Must consider available alternatives for each operation in a query and choose best one based on system statistics, etc. This is part of the broader task of optimizing a query composed of several ops.

Page 54: 1 Implementation of Relational Operations Chapters 12 ~ 14

54

Appendix: Yao’s formula

• Given a records grouped into m blocks (1 < m n), each contains n/m records. If k records (k n - n/m) are randomly selected from the n records, the expected number of blocks hit (blocks with at least one record selected) is given by :

m * [1 – Ckn-p/Ck

n]

Page 55: 1 Implementation of Relational Operations Chapters 12 ~ 14

55

Random variables

• X : random variable representing the number of blocks hit

• Ij: random variable where Ij = 1 when at least one record in the jth block is selected, and Ij = 0 otherwise

Page 56: 1 Implementation of Relational Operations Chapters 12 ~ 14

56

• The jth block has p = n/m records, and there are n – p records not in the jth block

• Probability that no records are selected from the jth block

– Ckn-p/Ck

n or Cknd/Ck

n, where d = 1 – 1/m

• Expectation of Ij : E(Ij) = 1*(1 – Ckn*d/Ck

n) + 0 * Ckn*d/Ck

n

• Expected number of blocks hit

– E(X) = E(I1) + E(I2) + E(I3) + … + E(Im)

= m * E(Ij)