relational operator evaluation. overview projection two steps remove unwanted attributes eliminate...

16
Relational Operator Evaluation

Upload: fay-hall

Post on 18-Jan-2018

226 views

Category:

Documents


0 download

DESCRIPTION

Projection Two steps –Remove unwanted attributes –Eliminate any duplicate tuples The expensive part is removing duplicates. –SQL systems don’t remove duplicates unless the keyword DISTINCT is specified in a query. Sorting Approach: Sort on and remove duplicates. (Can optimize this by dropping unwanted information while sorting.) Hashing Approach: Hash on to create partitions. Load partitions into memory one at a time, build in-memory hash structure, and eliminate duplicates. If there is an index with both R.sid and R.bid in the search key, may be cheaper to sort data entries! SELECT DISTINCT R.sid, R.bid FROM Reserves R

TRANSCRIPT

Page 1: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Relational Operator Evaluation

Page 2: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

overview

Page 3: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Projection• Two steps

– Remove unwanted attributes– Eliminate any duplicate tuples

• The expensive part is removing duplicates.– SQL systems don’t remove duplicates unless the keyword DISTINCT is

specified in a query.• Sorting Approach: Sort on <sid, bid> and remove duplicates. (Can

optimize this by dropping unwanted information while sorting.)• Hashing Approach: Hash on <sid, bid> to create partitions. Load

partitions into memory one at a time, build in-memory hash structure, and eliminate duplicates.

• If there is an index with both R.sid and R.bid in the search key, may be cheaper to sort data entries!

SELECT DISTINCT R.sid, R.bidFROM Reserves R

Page 4: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Projection Based on Sorting• Modify Pass 0 of external sort to eliminate unwanted

fields. Thus, runs of about 2B pages are produced, but tuples in runs are smaller than input tuples. (Size ratio depends on # and size of fields that are dropped.)

• Modify merging passes to eliminate duplicates. Thus, number of result tuples smaller than input. (Difference depends on # of duplicates.)

• Cost: In Pass 0, read original relation (size M), write out same number of smaller tuples. In merging passes, fewer tuples written out in each pass. Using Reserves example, 1000 input pages reduced to 250 in Pass 0 if size ratio is 0.25

Page 5: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Projection Based on Hashing• Partitioning phase: Read R using one input buffer. For

each tuple, discard unwanted fields, apply hash function h1 to choose one of B-1 output buffers.– Result is B-1 partitions (of tuples with no unwanted fields). 2

tuples from different partitions guaranteed to be distinct.• Duplicate elimination phase: For each partition, read it

and build an in-memory hash table, using hash fn h2(<> h1) on all fields,– If a new tuple hashes to the same value as some existing tuple,

compare the two to check the new tuple is a duplicate– If partition does not fit in memory, can apply hash-based

projection algorithm recursively to this partition.• Cost: For partitioning, read R, write out each tuple, but

with fewer fields. This is read in next phase.

Page 6: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing
Page 7: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Discussion of Projection• Sort-based approach is the standard; better

handling of skew and result is sorted; • If the key of an index includes all attributes in

selection, 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,

Page 8: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Example of Join

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 rname31 101 10/11/96 lubber58 103 11/12/96 dustin

sid sname rating age bid day rname31 lubber 8 55.5 101 10/11/96 lubber58 rusty 10 35.0 103 11/12/96 dustin

SELECT *FROM Sailors R, Reserve SWHERE R.sid=S.sid

Page 9: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Equality Joins With One Join Column

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

• 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.

Page 10: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Simple Nested Loops Join• For each tuple in the outer relation R, we scan the entire

inner relation S. – Cost: M + pR * M * N = 1000 + 100*1000*500 I/Os.

• 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 Spage.– Cost: M + M*N = 1000 + 1000*500, If smaller relation (S) is

outer, cost = 500 + 500*1000

foreach tuple r in R doforeach tuple s in S do

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

Page 11: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Simple Nested Loop Join

R S

Tuple

1 scan per R tuple

N pages per scanM*pR tuples

Page 12: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Block Nested Loops Join• Basic idea: effectively utilizing buffer pages• Suppose there are B buffer pages, B-2 buffer are used to

load the smaller relation R, one buffer for output, one buffer for scanning the larger relation S

• To find matching pairs, a main-memory hash table is built for the block of R

• Cost: M+M/ (B – 2)N

foreach block of B-2 pages in R doforeach page of S do

for all matching in-memory tuples r and s, add <r, s> to output buffer

Page 13: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Block Nested Loop Join

R S

B – 2 pages

1 scan per R block

N pages per scanM / (B – 2) blocks

Page 14: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Buffer usage in Block-Nested Loops Join

Page 15: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

Examples of Block Nested Loops

• Assume there are 100 buffer pages available to load outer relation

• With Reserves (R) as outer– Cost of scanning R is 1000 I/Os; a total of 10 blocks.– Per block 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 Sailors as outer:– Cost of scanning S is 500 I/Os; a total of 5 blocks.– Per block of S, we scan Reserves; 5*1000 I/Os.

• With sequential reads considered, analysis changes: may be best to divide buffers evenly between R and S.

Page 16: Relational Operator Evaluation. overview Projection Two steps Remove unwanted attributes Eliminate any duplicate tuples The expensive part is removing

summary