Κεφάλαιο 13 Επεξεργασία και Εκτέλεση Ερωτήσεων σε Σχεσιακές Βάσεις εδοµένων Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.1
Επεξεργασία και Βελτιστοποίηση Επερωτήσεων Αρχιτεκτονική του ΕΠΕΞΕΡΓΑΣΤΗ ΕΠΕΡΩΤΗΣΕΩΝ σε ένα DBMS ΕπεξεργασίαΕπερωτήσεων (Query Processing) ΥλοποίησηΣχεσιακώνΠράξεων (συνένωση, sεπιλογή, προβολή, συναθροίσεις, κλπ.) Χρήση Ευρετηρίων Ενδιάµεση Μνήµη Βελτιστοποίηση Επερωτήσεων (Query Optimization) Σχέδια Εκτέλεσης (plans) Εµφωλιασµός Επερωτήσεων Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.2
Αρχιτεκτονική του QUERY PROCESSOR TERMINAL MONITOR Query Language QUERY LANGUAGE COMPILER Internal Form (e.g., Relational Algebra) QUERY OPTIMIZER Internal Form (e.g., Relational Algebra) CODE GENERATOR / INTERPRETER Record-at-a-time calls embedded in a program ACCESS METHOD Page-at-a-time access to files FILE SYSTEM Select e.name from empl e, dept d where e.dno = d.dno and d.floor=3 σ floor=3 >< Π Name ( (e d) Π Name ( e >< ( σ floor=3 d) (plans) manipulate records manipulate pages Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.3
Βασικά Στάδια στο Query Processing Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.4
Query Processing σεένα DBMS Ο Query Language Parser / Compilerµεταφράζειτηνεπερώτηση σε µια εσωτερική µορφή, συχνά σε µια έκφραση σχεσιακής άλγεβρας ή σε µορφή δένδρου / γράφου (αφού κάνει λεκτική και συντακτική ανάλυση καθώς και έλεγχο εγκυρότητας) Ο Query Optimizer εξετάζει όλες τις ισοδύναµες αλγεβρικές παραστάσεις (σχέδια εκτέλεσης - plans) και επιλέγει το καλύτερο / λιγότερο δαπανηρό Χρησιµοποιεί στοιχεία όπως το µέγεθος, τη χρήση και τα περιεχόµενα των σχέσεων (που αποθηκεύονται σε καταλόγους και ανανεώνονται δυναµικά) Χρησιµοποιεί τη γνώση για το Κόστος Εκτέλεσης των σχεσιακών πράξεων (π.χ., πως υλοποιούνται οι συνενώσεις, κλπ.) Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.5
Query Processing σεένα DBMS -- (β) Ο Code Generator (Γεννήτορας Κώδικα) υλοποιεί το σχέδιο εκτέλεσης που παράγεται από το Βελτιστοποιητή. Τουποσύστηµαεκτέλεσηςεπερωτήσεων (Access Methods) υποστηρίζει εντολές για πρόσβαση σε ένα αρχείο. Λειτουργίες των access method είναι:» Πως τοποθετούνται οι εγγραφές σε µια σελίδα» Πως τοποθετούνται τα πεδία σε µια εγγραφή» Data Compression» Ευρετήρια εγγραφών µέσω πεδίων Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.6
Query Processing σεένα DBMS -- (γ) Το File System υποστηρίζει την πρόσβαση σε σελίδες Λειτουργίες του File System είναι:» Πως το αρχείο διαµοιράζεται στο ίσκο» ιαχείριση ελεύθερου χώρου στο ίσκο» Εκτέλεση Hardware I/O εντολών» ιαχείριση Ενδιάµεσης Μνήµης Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.7
Μετρήσεις Κόστους Επερωτήσεων Γενικά, το ΚΟΣΤΟΣ µετριέται ως ο συνολικός χρόνος για την απάντηση µιας επερώτησης Παράγοντες που επηρεάζουν το χρονικό κόστος» Προσβάσεις στο δίσκο, CPU, επικοινωνία δικτύου Στην τυπική περίπτωση, οι ΠΡΟΣΒΑΣΕΙΣ ΣΤΟ ΙΣΚΟ είναι ο σηµαντικότερος παράγων, και είναι σχετικά εύκολο να υπολογιστούν. Λαµβάνονται υπόψη: Αριθµός Προσβάσεων * average-seek-cost Αριθµός των Μπλόκ που ιαβάζονται * average-block-read-cost Αριθµός των Μπλόκ που Γράφονται * average-block-write-cost» Το κόστος γραψίµατος ενός Μπλόκ είναι µεγαλύτερο από το κόστος διαβάσµατος ενός Μπλόκ ΓΙΑ ΑΠΛΟΥΣΤΕΥΣΗ χρησιµοποιούµε τον ΑΡΙΘΜΟ ΠΡΟΣΒΑΣΕΩΝ για τη µέτρηση Κόστους. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.8
Relational Operations -- Implementation Θα περιγράψουµε πως υλοποιούνται: Selection ( ) Επιλογή υποσυνόλου πλειάδων από σχέση. Projection ( ) ιαγραφή στηλών που δεν είναι επιθυµητές. >< Join ( ) Μας επιτρέπει να συνδυάζουµε δύο σχέσεις. Set-difference ( ) Πλειάδες στη Σχέση 1, αλλά όχι στην 2. U σ π Union ( ) ΠλειάδεςστηΣχέση 1 καιστησχέση 2. Aggregation (SUM, MIN, κλπ.) και GROUP BY Καθώς κάθε πράξη έχει σαν αποτέλεσµα µια Σχέση, οι πράξεις µπορούν να συνδυαστούν! Αφού καλύψουµε τις απλές πράξεις, θα συζητήσουµε πως µπορεί να βελτιστοποιηθούν (optimize) επερωτήσεις που σχηµατίζονται µε συνδυασµό πράξεων. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.9
Βάση εδοµένων για Παραδείγµατα Sailors (sid: integer, sname: string, rating: integer, age: real) Reserves (sid: integer, bid: integer, day: dates, rname: string) Όπως αυτή που ξέραµε; Το rname προστίθεται µόνο. Reserves: Κάθε tupleείναι 40 bytes σεµήκος, 100 tuples ανάσελίδα, 1000 σελίδες. Sailors: Κάθε tuple είναι 50 bytes, 80 tuples ανάσελίδα, 500 σελίδες. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.10
Simple Selections σ R a ttr SELECT * FROM WHERE. ( R ) Reserves R R.rname < C% Of the form o p v a lu e Size of result approximated as size of R * reduction factor; we will consider how to estimate reduction factors later. With no index, unsorted: Must essentially scan the whole relation; cost is b r (#pages in R). With an index on selection attribute: Use index to find qualifying data entries, then retrieve corresponding data records. (Hash index useful only for equality selections.) Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.11
Selection Operation File scan search algorithms that locate and retrieve records that fulfill a selection condition. Algorithm A1 (linear search). Scan each file block and test all records to see whether they satisfy the selection condition. Cost estimate (number of disk blocks scanned) = b r» b r denotes number of blocks containing records from relation r If selection is on a key attribute, average cost = (b r /2)» stop on finding record Linear search can be applied regardless of» selection condition or» ordering of records in the file, or» availability of indices Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.12
Selection Operation (Cont.) A2 (binary search). Applicable if selection is an equality comparison on the attribute on which file is ordered. Assume that the blocks of a relation are stored contiguously Cost estimate (number of disk blocks to be scanned):» log 2 (b r ) cost of locating the first tuple by a binary search on the blocks» Plus number of blocks containing records that satisfy selection condition Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.13
Selections Using Indices Index scan search algorithms that use an index selection condition must be on search-key of index. A3 (primary index on candidate key, equality). Retrieve a single record that satisfies the corresponding equality condition Cost = HT i + 1 (ύψοςτουβ+ δέντρου + 1 ανάκληση) A4 (primary index on nonkey, equality) Retrieve multiple records. Records will be on consecutive blocks Cost = HT i + number of blocks containing retrieved records A5 (equality on search-key of secondary index). Retrieve a single record if the search-key is a candidate key» Cost = HT i + 1 Retrieve multiple records if search-key is not a candidate key» Cost = HT i + number of records retrieved Can be very expensive!» each record may be on a different block one block access for each retrieved record Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.14
Selections Involving Comparisons Can implement selections of the form σ A V (r) or σ A V (r) by using a linear file scan or binary search, or by using indices in the following ways: A6 (primary index, comparison). (Relation is sorted on A)» For σ A V (r) use index to find first tuple v and scan relation sequentially from there» For σ A V (r) just scan relation sequentially till first tuple > v; do not use index A7 (secondary index, comparison).» For σ A V (r) use index to find first index entry v and scan index sequentially from there, to find pointers to records.» For σ A V (r) just scan leaf pages of index finding pointers to records, till first entry > v» In either case, retrieve records that are pointed to requires an I/O for each record Linear file scan may be cheaper if many records are to be fetched! Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.15
Implementation of of Complex Selections Conjunction: σ θ 1 θ 2... θ n(r) A8 (conjunctive selection using one index). Select a combination of θ i and algorithms A1 through A7 that results in the least cost for σ θi (r). Test other conditions on tuple after fetching it into memory buffer. A9 (conjunctive selection using multiple-key index). Use appropriate composite (multiple-key) index if available. A10 (conjunctive selection by intersection of identifiers). Requires indices with record pointers. Use corresponding index for each condition, and take intersection of all the obtained sets of record pointers. Then fetch records from file If some conditions do not have appropriate indices, apply test in memory. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.16
Algorithms for Complex Selections Disjunction:σ θ 1 θ 2... θ n(r). A11 (disjunctive selection by union of identifiers). Applicable if all conditions have available indices.» Otherwise use linear scan. Use corresponding index for each condition, and take union of all the obtained sets of record pointers. Then fetch records from file Negation: σ θ (r) Use linear scan on file If very few records satisfy θ, and an index is applicable to θ» Find satisfying records using index and fetch from file Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.17
Sorting We may build an index on the relation, and then use the index to read the relation in sorted order. May lead to one disk block access for each tuple. For relations that fit in memory, techniques like quicksort can be used. For relations that don t fit in memory, external sort-merge is a good choice. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.18
External Sort-Merge Let M denote memory size (in pages). 1. Create sorted runs. Let i be 0 initially. Repeatedly do the following till the end of the relation: (a) Read M blocks of relation into memory (b) Sort the in-memory blocks (c) Write sorted data to run R i ; increment i. Let the final value of I be N 2. Merge the runs (N-way merge). We assume (for now) that N < M. 1. Use N blocks of memory to buffer input runs, and 1 block to buffer output. Read the first block of each run into its buffer page 2. repeat 1. Select the first record (in sort order) among all buffer pages 2. Write the record to the output buffer. If the output buffer is full write it to disk. 3. Delete the record from its input buffer page. If the buffer page becomes empty then read the next block (if any) of the run into the buffer. 3. until all input buffer pages are empty: Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.19
External Sort-Merge (Cont.) If i M, several merge passes are required. In each pass, contiguous groups of M - 1 runs are merged. A pass reduces the number of runs by a factor of M -1, and creates runs longer by the same factor.» E.g. If M=11, and there are 90 runs, one pass reduces the number of runs to 9, each 10 times the size of the initial runs Repeated passes are performed till all runs have been merged into one. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.20
Example: External Sorting Using Sort-Merge Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.21
External Merge Sort (Cont.) Cost analysis: Total number of merge passes required: log M 1 (b r /M). Disk accesses for initial run creation as well as in each pass is 2b r» for final pass, we don t count write cost we ignore final write cost for all operations since the output of an operation may be sent to the parent operation without being written to disk Thus total number of disk accesses for external sorting: b r ( 2 log M 1 (b r / M) + 1) Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.22
Equality Joins µε ένα γνώρισµα συνένωσης SELECT * FROM Reserves R1, Sailors S1 WHERE R1.sid=S1.sid >< Στην άλγεβρα: R S. Πολύ συχνό! Θέλει Προσοχή γιατί το R S είναιµεγάλο,άρα, R S ακολουθούµενοαπόµια επιλογή είναι πολύ αργό Έστω: M pages στην R, p R tuples per page, N pages στην S, p S tuples per page. Στοπαράδειγµα, το R είναι Reserves καιτο S είναι Sailors. Cost metric: # of I/Os. εν µας ενδιαφέρει το κόστος για παρουσίαση του αποτελέσµατος (output). Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.23
Simple Nested Loop Join foreach tuple r in R do foreach tuple s in S do if r i == s j then add <r, s> to result For each tuple in the outer relation R, we scan the entire inner relation S. Cost: M + p R * 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 S-page. Cost: M + M*N = 1000 + 1000*500 If smaller relation (S) is outer, cost = 500 + 500*1000 Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.24
Index Nested Loops Join foreach tuple r in R do foreach tuple s in S where r i == s j do add <r, s> to result Index lookups can replace file scans if join is an equi-join or natural join and an index is available on the inner relation s join attribute» Can construct an index just to compute a 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 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), un clustered: up to 1 I/O per matching S tuple. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.25
Examples of Index Nested Loops Hash-index 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: 220,000 I/Os. Hash-index 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. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.26
Block Nested Loops Join 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 of outer R. For each matching tuple r in R-block, s in S-page, add <r, s> to result. Then read next R-block, scan S, etc. R & S... Hash table for block of R (k < B-1 pages)... Join Result... Input buffer for S Output buffer Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.27
Examples of Block Nested Loops Cost: Scan of outer + #outer blocks * scan of inner #outer blocks = With Reserves (R) as outer, and 100 pages of R: 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 100-page block of Sailors as outer: # of pages of 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. blocksize With sequential reads considered, analysis changes: may be best to divide buffers evenly between R and S. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.28
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. Advance scan of R until current R-tuple >= current S tuple, then advance scan of S until current S-tuple >= current R tuple; do this 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. 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 are likely to find needed pages in buffer.) i=j Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.29
Example of Sort-Merge Join sid sname rating age 22 dustin 7 45.0 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 sid bid day rname 28 103 12/4/96 guppy 28 103 11/3/96 yuppy 31 101 10/10/96 dustin 31 102 10/12/96 lubber 31 101 10/11/96 lubber 58 103 11/12/96 dustin 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) Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.30
Refinement of Sort-Merge Join We can combine the merging phases in the sorting of R and S with the merging required for the join. L With B >, where L is the size of the larger relation, using the sorting refinement that produces runs of length 2B in Pass 0, #runs of each relation is < B/2. Allocate 1 page per run of each relation, and `merge while checking the join condition. Cost: read+write each relation in Pass 0 + read each relation in (only) merging pass (+ writing of result tuples). In example, cost goes down from 7500 to 4500 I/Os. In practice, cost of sort-merge join, like the cost of external sorting, is linear. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.31
Hash-Join Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.32
Hash-Join Partition both relations using hash fn h: R tuples in partition i will only match S tuples in partition i. Original Relation... OUTPUT 1 INPUT 2 hash function h B-1 Partitions 1 2 B-1 Disk B main memory buffers Disk Read in a partition of R, hash it using h2 (<> h!). Scan matching partition of S, search for matches. Partitions of R & S hash fn h2 Hash table for partition Ri (k < B-1 pages) h2 Join Result Disk Input buffer for Si Output buffer B main memory buffers Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.33 Disk
Observations on Hash-Join #partitions k < B-1, and B-2 > size of largest partition to be held in memory. Assuming uniformly sized partitions, and maximizing k, we get: k= B-1, and M/(B-1) < B-2, i.e., B must be > If we build an in-memory hash table to speed up the matching of tuples, a little more memory is needed. 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. M Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.34
Cost of Hash-Join In partitioning phase, read+write both relations; 2(M+N). In matching phase, read both relations; 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. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.35
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, Sort Merge Join not applicable. Block NL quite likely to be the best join method here. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.36
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 corresponding R-partition and add tuples to table while discarding duplicates. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.37
Aggregate Operations (AVG, MIN, etc.) Without grouping: In general, requires scanning the relation. Given index whose search key includes all attributes in the SELECT or WHERE clauses, can do index-only scan. 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.) Given tree index whose search key includes all attributes in SELECT, WHERE and GROUP BY clauses, can do index-only scan; if group-by attributes form prefix of search key, can retrieve data entries/tuples in group-by order. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.38
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, MRU is best, LRU is worst (sequential flooding). Does replacement policy matter for Block Nested Loops? What about Index Nested Loops? Sort-Merge Join? Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.39
Summary for Relational Operators Implementation 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. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.40
Evaluation of of Expressions So far: we have seen algorithms for individual operations Alternatives for evaluating an entire expression tree Materialization: generate results of an expression whose inputs are relations or are already computed, materialize (store) it on disk. Repeat. Pipelining: pass on tuples to parent operations even as an operation is being executed We study above alternatives in more detail Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.41
Materialization Materialized evaluation: evaluate one operation at a time, starting at the lowest-level. Use intermediate results materialized into temporary relations to evaluate next-level operations. E.g., in figure below, compute and store σ balance < 2500 ( account then compute the store its join with customer, and finally compute the projections on customer-name. ) Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.42
Materialization (Cont.) Materialized evaluation is always applicable Cost of writing results to disk and reading them back can be quite high Our cost formulas for operations ignore cost of writing results to disk, so» Overall cost = Sum of costs of individual operations + cost of writing intermediate results to disk Double buffering: use two output buffers for each operation, when one is full write it to disk while the other is getting filled Allows overlap of disk writes with computation and reduces execution time Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.43
Pipelining Pipelined evaluation : evaluate several operations simultaneously, passing the results of one operation on to the next. E.g., in previous expression tree, don t store result of σ balance <2500 ( account ) instead, pass tuples directly to the join.. Similarly, don t store result of join, pass tuples directly to projection. Much cheaper than materialization: no need to store a temporary relation to disk. Pipelining may not always be possible e.g., sort, hash-join. For pipelining to be effective, use evaluation algorithms that generate output tuples even as tuples are received for inputs to the operation. Pipelines can be executed in two ways: demand driven and producer driven Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.44
Pipelining (Cont.) In demand driven or lazy evaluation system repeatedly requests next tuple from top level operation Each operation requests next tuple from children operations as required, in order to output its next tuple In between calls, operation has to maintain state so it knows what to return next Each operation is implemented as an iterator implementing the following operations» open()» next()» close() E.g. file scan: initialize file scan, store pointer to beginning of file as state E.g.merge join: sort relations and store pointers to beginning of sorted relations as state E.g. for file scan: Output next tuple, and advance and store file pointer E.g. for merge join: continue with merge from earlier state till next output tuple is found. Save pointers as iterator state. Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.45
Pipelining (Cont.) In produce-driven or eager pipelining Operators produce tuples eagerly and pass them up to their parents» Buffer maintained between operators, child puts tuples in buffer, parent removes tuples from buffer» if buffer is full, child waits till there is space in the buffer, and then generates more tuples System schedules operations that have space in output buffer and can process more input tuples Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.46
Evaluation Algorithms for Pipelining Some algorithms are not able to output results even as they get input tuples E.g. merge join, or hash join These result in intermediate results being written to disk and then read back always Algorithm variants are possible to generate (at least some) results on the fly, as input tuples are read in E.g. hybrid hash join generates output tuples even as probe relation tuples in the in-memory partition (partition 0) are read in Pipelined join technique: Hybrid hash join, modified to buffer partition 0 tuples of both relations in-memory, reading them as they become available, and output results of any matches between partition 0 tuples» When a new r 0 tuple is found, match it with existing s 0 tuples, output matches, and save it in r 0» Symmetrically for s 0 tuples Ι.Β -- Εκτέλεση Ερωτήσεων Σελίδα 4.47