Download presentation
Presentation is loading. Please wait.
Published bySydney Poole Modified over 8 years ago
1
Database Management Systems 1 Raghu Ramakrishnan Evaluation of Relational Operations Chpt 14
2
Database Management Systems 2 Raghu Ramakrishnan Relational Operations v 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. v 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.
3
Database Management Systems 3 Raghu Ramakrishnan Schema for Examples v Similar to old schema; rname added for variations. v Reserves: –Each tuple is 40 bytes long, 100 tuples per page, 1000 pages. v 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)
4
Database Management Systems 4 Raghu Ramakrishnan How to measure the cost? We only use I/O cost to quantify the performance: (a) Sizes of the relations (page numbers and tuples for each page); (b) Data indexing structure and sorting orders; (c) Size of available buffer pool; (d) Buffer replacement policy.
5
Database Management Systems 5 Raghu Ramakrishnan Selection Operator Select * From Reserves R Where R.rname = ‘Joe’ a. No index, Unsorted data Page 1 Page M Entity 0 Entity 99 Entity 0 Entity 99 I/O Cost: M I/Os
6
Database Management Systems 6 Raghu Ramakrishnan Selection Operator b. No index but sorted data Page 1Page M Entity 0 Entity 99 Entity 0 Entity 99 I/O Cost: O(logM) + O(T) I/Os Binary search is used! Select * From Reserves R Where R.rname = ‘Joe’ O(logM)
7
Database Management Systems 7 Raghu Ramakrishnan Selection Operator c. B+ tree index Page 1 Entity 0 Entity 99 Entity 0 Entity 99 I/O Cost: levels + m I/Os (Directs search) Index Data Entries Page M For clustered B+ tree!! Select * From Reserves R Where R.rname = ‘Joe’ m<<M
8
Database Management Systems 8 Raghu Ramakrishnan Selection Operator c. B+ tree index Page 1 Entity 0 Entity 99 Entity 0 Entity 99 I/O Cost: levels + m or levels + M I/Os (Directs search) Index Data Entries Page M For unclustered B+ tree!! Select * From Reserves R Where R.rname = ‘Joe’
9
Database Management Systems 9 Raghu Ramakrishnan Selection Operator d. Hash index Page 1 Entity 0 Entity 99 Entity 0 Entity 99 Page M Hash function Bucket 1Bucket n I/O Cost: buckets + m I/Os Select * From Reserves R Where R.rname = ‘Joe’ rname m<<M
10
Database Management Systems 10 Raghu Ramakrishnan Conjunctive Normal Form v Conjunct ( ) ^( ) ^( ) ^( ) v Disjunction ( V ) v Example: –Day<8/9/02 ^ rname=‘joe’
11
Database Management Systems 11 Raghu Ramakrishnan General Selection Operator (select from multiple relations with multiple conditions) Select * From Reserves S Where day<8/9/94^ bid=5 ^ sid =3 a. Without disjunction Retrieve by primary conjuncts. Apply all non-primary conjuncts. Retrieve sets by index. Sort by rid. Intersection. Apply all non-primary conjuncts. For several indexes
12
Database Management Systems 12 Raghu Ramakrishnan General Selection Operator I/O Cost: levels + m + p I/Os Hash function Bucket 1 Bucket n Data pages Search key is (bid, sid) (Directs search) Index Data Entries Search key is “day” Search from two indexes and intersection!
13
Database Management Systems 13 Raghu Ramakrishnan General Selection Operator b. With disjunction Select * From Reserves S Where day<8/9/94 V rname=‘joe’ File scan!
14
Database Management Systems 14 Raghu Ramakrishnan Intersection of Rids v Second approach (if we have 2 or more matching indexes): –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. If we have 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.
15
Database Management Systems 15 Raghu Ramakrishnan Projection Operation To implement projection, we have to do: (a) Remove unwanted attributes (i.e., those not on the selection). (b) Remove any duplicate tuples. Select DISTINCT R.sid, R.bid From Reserves R 1.Projection based on sorting (a) Scan R and produce a set of tuples that contain only the desired attributes; I/O cost is O(M) (b) Sort this set of tuples using the combination of all its attributes as the key for sorting; O(MlogM) © Scan the sorted result, compare adjacent tuples and remove duplicates. O(M) I/O Cost: O(MlogM) I/Os
16
Database Management Systems 16 Raghu Ramakrishnan 2. Projection based on hashing: (a) partitioning; (b) duplicate elimination. Projection Operation Select DISTINCT R.sid, R.bid From Reserves R B main memory buffers Disk Original Relation OUTPUT 2 INPUT 1 hash function h B-1 Partitions 1 2 B-1... When we have B buffer pages!
17
Database Management Systems 17 Raghu Ramakrishnan Projection Operation 2. Projection based on hashing (a) Partitioning I/O Cost: M + T (projected pages) (1) read relation R into buffer page, one page a time. (2) For each tuple on the input page, we project out the unwanted attributes and use a hash function to the combination of all remaining attributes. (3) After the projection, the tuple is written to the output buffer page. Select DISTINCT R.sid, R.bid From Reserves R I/O cost: M + 2*T
18
Database Management Systems 18 Raghu Ramakrishnan Projection Operation 2. Projection based on hashing (b) Duplicate Elimination (1) Two tuples that belong to different partitions are guaranteed not to be duplicates because they have different hashing value; (2) If two tuples are duplicates, they are in the same partition. We read in the B-1 partitions one at a time to remove duplicates. Select DISTINCT R.sid, R.bid From Reserves R I/O cost: T (selected)
19
Database Management Systems 19 Raghu Ramakrishnan The Projection Operation v An approach based on sorting: –Modify Pass 0 of external sort to eliminate unwanted fields. Thus, runs of about 2 * #buffer 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 SELECT DISTINCT R.sid, R.bid FROM Reserves R
20
Database Management Systems 20 Raghu Ramakrishnan Projection Based on Hashing v 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. v Duplicate elimination phase : For each partition, read it and build an in-memory hash table, using hash fn h2 (<> h1 ) on all fields, while discarding duplicates. – If partition does not fit in memory, can apply hash-based projection algorithm recursively to this partition.
21
Database Management Systems 21 Raghu Ramakrishnan Discussion of Projection v Sort-based approach is the standard; better handling of skew and result is sorted. v 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!) v 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.
22
Database Management Systems 22 Raghu Ramakrishnan Join Operation SELECT * FROM Reserves R1, Sailors S1 WHERE R1.sid=S1.sid v In algebra: R S Common! Must be carefully optimized. R S is large; so, R S followed by a selection is inefficient. v 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. v We will consider more complex join conditions later. v Cost metric : # of I/Os (read 1 page/IO). We will ignore output costs.
23
Database Management Systems 23 Raghu Ramakrishnan Simple Nested Loops Join v For each tuple in the outer relation R, we scan the entire inner relation S. –Cost of scanning R is number of page M I/Os –Scan S number tuples per page of R (p R ) * Number pages (M) with each scan costing number pages of S (N) –Cost: M + (p R * M )* N = 1000 + 100*1000*500 I/Os. (50,001,000 I/O - if 10ms per I/O 140 hours) –If smaller relation S is outer cost would be 500 + 80*500*1000 (40,000,500 somewhat smaller) For each tuple r in R do For each tuple s in S do if r i == s j then add to result
24
Database Management Systems 24 Raghu Ramakrishnan Simple Nested Loops Join v Page-oriented Nested Loops join: –Do the join page-at-a-time –Cost of scanning R is number of page M I/Os –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. –Cost: M + M*N = 1000 + 1000*500 = 501,000 1.4hours –If smaller relation (S) is outer, cost = 500 + 500*1000 = 500,500
25
Database Management Systems 25 Raghu Ramakrishnan Index Nested Loops Join v 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) v For each R tuple, cost of finding appropriate leaf is about 1-2 for hash index, 2-4 for B+ tree. Cost of then finding S tuples depends on clustering. –Clustered index: 1 I/O (typical), unclustered: upto 1 I/O per matching S tuple (could be much less, but no way to tell a-priori. foreach tuple r in R do foreach tuple s in S where r i == s j do add to result
26
Database Management Systems 26 Raghu Ramakrishnan Examples of Index Nested Loops v Hash-index (Alt. 2) on sid of Sailors (as inner): –Scan Reserves: 1000 pages thus 1000 I/Os –100 tuples/page*1000 pages = 100,000 tuples –For each Reserves tuple: 1.2 I/Os (on average) to get data entry in index of Sailors, plus 1 I/O to get (the exactly one since sid is primary key, otherwise there could be several) matching Sailors tuple. –1000 (from scan of reserves) +2.2 * 100,000 = 221,000 I/Os.
27
Database Management Systems 27 Raghu Ramakrishnan Examples of Index Nested Loops v Hash-index (Alt. 2) on sid of Reserves (as inner): –Scan Sailors: 500 pages thus 500 I/Os –80 tuples/page*500 pages = 40,000 tuples –For each Sailors tuple: 1.2 I/Os (on average) 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 either 1 or 2.5 I/Os depending on whether the index is clustered. –Clustered - 500 + (1.2 * 40000) + (1 * 40000) = 88,500 –Unclustered - 500 + (1.2 * 40000) + (2.5 * 40000) = 148,500
28
Database Management Systems 28 Raghu Ramakrishnan Block Nested Loops Join v Use one page of memory as an input buffer for scanning the inner S, one page as the output buffer, and use all remaining pages to hold ``block’’ of outer R –For each matching tuple r in R-block, s in S-page, add to result. Then read next R-block, scan S, etc.... R & S Hash table for block of R (k < B-2 pages) Input buffer for S Output buffer... Join Result Disk B Main Memory Buffers Disk I/O cost: M+N*[M/(B-2)]
29
Database Management Systems 29 Raghu Ramakrishnan Examples of Block Nested Loops v Cost: Scan of outer + #outer blocks * scan of inner –#outer blocks = v With Reserves (R) as outer, and 100 memory pages of R (blocksize): –Cost of scanning R is 1000 I/Os; a total of 10 blocks (1000/100). –Per block of R, we scan Sailors (S): 10*500 I/Os. –Total cost = 1000 + (10 * 500) = 6000 I/Os –If space for just 90 pages of R, we would scan S 12 times (1000/90) –Total cost = 1000 + (12 * 500) = 7000 I/Os #/ofpagesofouterblocksize
30
Database Management Systems 30 Raghu Ramakrishnan Examples of Block Nested Loops v With Sailors as outer and 100 memory pages of S (blocksize) –Cost of scanning S is 500 I/Os; a total of 5 blocks (500 / 100) –Per block of S, we scan Reserves (R): 5*1000 I/Os. –Total cost - 500 + (5*1000) = 5500 I/Os –If space for just 90 pages of S, we would scan R 6 times (500/90) –Total cost = 500 + (6 * 1000) = 6500 I/Os v With sequential reads considered, analysis changes: may be best to divide buffers evenly between R and S.
31
Database Management Systems 31 Raghu Ramakrishnan Sort-Merge Join (R S) v Sort R and S on the join column, then scan them to do a ``merge’’ (on join col.), and output result tuples. –Start scans at first tuple of each relation –Advance the scan of R until current R-tuple < current S tuple, then advance scan of S until current S-tuple < current R tuple; –Alternate between scan of R & S until R tuple(I) = S tuple(j) –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. –Then resume scanning R and S. i=j
32
Database Management Systems 32 Raghu Ramakrishnan Sort-Merge Join (R S) v R is scanned once; each S group is scanned once per matching R tuple. (Multiple scans of an S group are likely to find needed pages in buffer.) i=j
33
Database Management Systems 33 Raghu Ramakrishnan Example of Sort-Merge Join v Cost: M log M + N log N + (M+N) v With 100 buffer pages, both Reserves and Sailors can be sorted in 2 passes (see page 307 for # runs calculation) –First pass produces 10 runs of 100 pages each, second pass merges 10 runs to produce sorted result –Read/Write Reserves in each pass, thus 2 * 2 * 1000 = 4000 I/Os, Read/Write Sailors - 2 * 2 * 500 = 2000 I/Os v Second phase of sort-merge join - additional scan of each relation - total cost: 4000 + 2000 + 1000 + 500 = 7500 I/Os v Block Nested Loop cost: 2500 to 15000 I/Os Sorting R Sorting SMerging
34
Database Management Systems 34 Raghu Ramakrishnan Hash-Join v Partition both relations using hash fn h : R tuples in partition i will only match S tuples in partition i. v Read in a partition of R, hash it using h2 (<> h!). Scan matching partition of S, search for matches. Partitions of R & S Input buffer for Si Hash table for partition Ri (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...
35
Database Management Systems 35 Raghu Ramakrishnan Cost of Hash-Join v In partitioning phase, read+write both relns; 2(M+N). In matching phase, read both relns; M+N I/Os. v In our running example, this is a total of 4500 I/Os. v Sort-Merge Join vs. Hash Join: –Given a minimum amount of memory 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.
36
Database Management Systems 36 Raghu Ramakrishnan General Join Conditions v Equalities over several attributes (e.g., R.sid=S.sid AND R.rname=S.sname ): –For Index Nested Loop, 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. v Inequality conditions (e.g., R.rname < S.sname ): –For Index NL, need B+ tree index. u Range probes on inner; # matches likely to be much higher than for equality joins. –Hash Join, Sort Merge Join not applicable.
37
Database Management Systems 37 Raghu Ramakrishnan Set Operations v Intersection and cross-product special cases of join. v Union and Except similar; we’ll do union. v Sorting based approach to union: –Sort both relations (on combination of all attributes). –Scan sorted relations and merge them. v 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.
38
Database Management Systems 38 Raghu Ramakrishnan Aggregate Operations ( AVG, MIN, etc.) v Without grouping: –In general, requires scanning the relation. –Given index whose search key includes all attributes in the SELECT and WHERE clauses, can do index-only scan. v With grouping: –Sort on group-by attributes, then scan relation and compute aggregate for each group. (Can improve upon this by combining sorting and aggregate computation.) –Similar approach based on hashing on group-by attributes. –Given tree index whose search key includes all attributes in SELECT, WHERE and GROUP BY clauses, can do index-only scan.
39
Database Management Systems 39 Raghu Ramakrishnan Summary v 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!). v Many alternative implementation techniques for each operator; no universally superior technique for most operators. v 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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.