Download presentation
Presentation is loading. Please wait.
Published byAlexandra Singleton Modified over 9 years ago
1
1 Implementation of Relational Operations Chapters 12 ~ 14
2
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
3
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.
4
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)
5
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 R WHERE R.rname < ‘C%’
6
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?
7
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…
8
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
9
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 OR s (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 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
10
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.
11
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 could be used; –Then, day<8/9/94 must be checked. How about a B+tree on ? How about a Hash index on ?
12
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
13
13 External Sorting Before presenting projection operators, we here present external sorting schemes
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 Disk
15
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 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,48,75,63,1 2 3,4 5,62,64,97,8 1,32 2,3 4,6 4,7 8,9 1,3 5,62 2,3 4,4 6,7 8,9 1,2 3,5 6 1,2 2,3 3,4 4,5 6,6 7,8
16
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. B Main memory buffers INPUT 1 INPUT B-1 OUTPUT Disk INPUT 2... * More than 3 buffer pages. How can we utilize them?
17
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
18
18 Number of Passes of External Sort ( I/O cost is 2N times number of passes)
19
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
20
20 Projection (DupElim) Issue is removing duplicates. Basic approach is to use sorting –1. Scan R, extract only the needed attrs (why do this 1 st ?) –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, so 1000 +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.bid FROM Reserves R
21
21 DupElim Based on Hashing Partition Phase for 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 Phase for 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 Disk Original Relation OUTPUT 2 INPUT 1 hash function h B-1 Partitions 1 2 B-1...
22
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
23
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)*f B > sqrt(f*T + B) Approximately, B > sqrt(f*T)
24
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.
25
25 Commercial Systems Informix hashing DB2, Oracle 8, Sybase ASE sorting MS SQL Server, Sybase ASIQ both
26
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
27
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
28
28 Joins Joins are very common Joins are very expensive (worst case: cross product!) Many approaches to reduce join cost
29
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, p R tuples per page –N pages in S, p S 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 S1 WHERE R1.sid=S1.sid
30
30 foreach tuple r in R do foreach tuple s in S do if r i == s j then add 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? (p R * 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?
31
31 Page-Oriented Nested Loops Join For each page of R, get each page of S, and write out matching pairs of tuples, 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 b R in R do foreach page b S in S do foreach tuple r in b R do foreach tuple s in b S do if r i == s j then add to result
32
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*p R ) * 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 do foreach tuple s in S where r i == s j do add to result
33
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
34
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 to result. Then read next R-chunk, scan S, etc.
35
35 “Block” Nested Loops Join Algorithm for each block of B-2 pages of R for each page of S for all matching in-memory tuples r R-block and s S-block add to result... R & S chunk of R tuples (k < B-1 pages) Input buffer for S Output buffer... Join Result
36
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
37
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
38
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 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.)
39
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. ( BNL cost: 2500 to 15000 I/Os )
40
40 Sort-Merge Join Cost : With 35 buffers (or 100, 300 buffers) –Sorting Reserves : 2*(1+log 33 1000/35 ) *1,000 = 4,000 –Sorting Sailors : 2*(1+log 33 500/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
41
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)
42
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.
43
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. Partitions of R & S Input buffer for Si Hash table for partition R i (k < B-1 pages) B main memory buffers Disk Output buffer Disk Join Result hash fn h2 B main memory buffers Disk Original Relation OUTPUT 2 INPUT 1 hash function h B-1 Partitions 1 2 B-1...
44
44 Grace Hash Join Algorithm // Partition R into k partitions foreach tuple r in R do read r and add it to buffer page h(r i ) // Partition S into k partitions foreach tuple s in R do read s and add it to buffer page h(s j ) // Probing phase for m = 1, …, k do { // Build in-memory hash table for R m, using h2 foreach tuple r in partition R m do read r and insert into hash table using h2(r i ) // Scan S m and probe for matching R m tuples foreach tuple s in partition S m do { read s and insert into hash table using h2(s j ) for matching R tuples r, output }
45
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)
46
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.
47
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.
48
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
49
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
50
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 (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.
51
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.
52
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?
53
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.
54
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 – C k n-p /C k n ]
55
55 Random variables X : random variable representing the number of blocks hit I j : random variable where I j = 1 when at least one record in the jth block is selected, and I j = 0 otherwise
56
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 –C k n-p /C k n or C k nd /C k n, where d = 1 – 1/m Expectation of I j : E(I j ) = 1*(1 – C k n*d /C k n ) + 0 * C k n*d /C k n Expected number of blocks hit –E(X) = E(I 1 ) + E(I 2 ) + E(I 3 ) + … + E(I m ) = m * E(I j )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.