Advanced Database Systems

Similar documents
Chapter 12: Query Processing

Chapter 13: Query Processing

Chapter 12: Query Processing. Chapter 12: Query Processing

! A relational algebra expression may have many equivalent. ! Cost is generally measured as total elapsed time for

Chapter 13: Query Processing Basic Steps in Query Processing

Query Processing. Debapriyo Majumdar Indian Sta4s4cal Ins4tute Kolkata DBMS PGDBA 2016

Database System Concepts

Query Processing & Optimization

Chapter 12: Query Processing

Shuigeng Zhou. May 18, 2016 School of Computer Science Fudan University

CMSC424: Database Design. Instructor: Amol Deshpande

CSIT5300: Advanced Database Systems

DBMS Y3/S5. 1. OVERVIEW The steps involved in processing a query are: 1. Parsing and translation. 2. Optimization. 3. Evaluation.

Query processing and optimization

Review. Support for data retrieval at the physical level:

Advanced Databases. Lecture 1- Query Processing. Masood Niazi Torshiz Islamic Azad university- Mashhad Branch

Ch 5 : Query Processing & Optimization

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

Something to think about. Problems. Purpose. Vocabulary. Query Evaluation Techniques for large DB. Part 1. Fact:

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

Query Processing. Introduction to Databases CompSci 316 Fall 2017

Query Optimization. Query Optimization. Optimization considerations. Example. Interaction of algorithm choice and tree arrangement.

Database Applications (15-415)

Faloutsos 1. Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Outline

Chapter 12: Indexing and Hashing. Basic Concepts

University of Waterloo Midterm Examination Sample Solution

Query Processing. Solutions to Practice Exercises Query:

Chapter 12: Indexing and Hashing

Hash-Based Indexing 165

Evaluation of relational operations

Introduction Alternative ways of evaluating a given query using

Lecture 19 Query Processing Part 1

Chapter 3. Algorithms for Query Processing and Optimization

Evaluation of Relational Operations

CS122 Lecture 8 Winter Term,

Implementation of Relational Operations: Other Operations

Intro to DB CHAPTER 12 INDEXING & HASHING

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #10: Query Processing

CS122 Lecture 15 Winter Term,

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Chapter 12: Query Processing

Storage hierarchy. Textbook: chapters 11, 12, and 13

Fundamentals of Database Systems

Query Processing & Optimization. CS 377: Database Systems

Database Applications (15-415)

Evaluation of Relational Operations: Other Techniques. Chapter 14 Sayyed Nezhadi

Chapter 11: Indexing and Hashing

What happens. 376a. Database Design. Execution strategy. Query conversion. Next. Two types of techniques

Advances in Data Management Query Processing and Query Optimisation A.Poulovassilis

Chapter 11: Indexing and Hashing

Query Optimization. Shuigeng Zhou. December 9, 2009 School of Computer Science Fudan University

Chapter 11: Indexing and Hashing

! Parallel machines are becoming quite common and affordable. ! Databases are growing increasingly large

Chapter 20: Parallel Databases

Chapter 20: Parallel Databases. Introduction

Chapter 17: Parallel Databases

Indexing. Week 14, Spring Edited by M. Naci Akkøk, , Contains slides from 8-9. April 2002 by Hector Garcia-Molina, Vera Goebel

CAS CS 460/660 Introduction to Database Systems. Query Evaluation II 1.1

Announcements. Reading Material. Recap. Today 9/17/17. Storage (contd. from Lecture 6)

Evaluation of Relational Operations: Other Techniques

Chapter 18: Parallel Databases

Chapter 18: Parallel Databases. Chapter 18: Parallel Databases. Parallelism in Databases. Introduction

Overview of Implementing Relational Operators and Query Evaluation

CMSC424: Database Design. Instructor: Amol Deshpande

Database System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Relational Database Systems 2 5. Query Processing

Chapter 11: Indexing and Hashing" Chapter 11: Indexing and Hashing"

Outline. Query Processing Overview Algorithms for basic operations. Query optimization. Sorting Selection Join Projection

Database Applications (15-415)

Implementation of Relational Operations. Introduction. CS 186, Fall 2002, Lecture 19 R&G - Chapter 12

5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator

Advanced Databases. Lecture 4 - Query Optimization. Masood Niazi Torshiz Islamic Azad university- Mashhad Branch

Chapter 12: Indexing and Hashing

Introduction to Indexing 2. Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana

Evaluation of Relational Operations: Other Techniques

SQL QUERY EVALUATION. CS121: Relational Databases Fall 2017 Lecture 12

Query Processing Strategies and Optimization

Kathleen Durant PhD Northeastern University CS Indexes

DBMS Query evaluation

15-415/615 Faloutsos 1

Data on External Storage

Administriva. CS 133: Databases. General Themes. Goals for Today. Fall 2018 Lec 11 10/11 Query Evaluation Prof. Beth Trushkowsky

Relational Database Systems 2 5. Query Processing

Statistics. Duplicate Elimination

Principles of Data Management. Lecture #9 (Query Processing Overview)

Physical Disk Structure. Physical Data Organization and Indexing. Pages and Blocks. Access Path. I/O Time to Access a Page. Disks.

CMPUT 391 Database Management Systems. Query Processing: The Basics. Textbook: Chapter 10. (first edition: Chapter 13) University of Alberta 1

Implementing Relational Operators: Selection, Projection, Join. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Evaluation of Relational Operations

Chapter 14: Query Optimization

The physical database. Contents - physical database design DATABASE DESIGN I - 1DL300. Introduction to Physical Database Design

Database Systems. Announcement. December 13/14, 2006 Lecture #10. Assignment #4 is due next week.

TotalCost = 3 (1, , 000) = 6, 000

CSE 530A. B+ Trees. Washington University Fall 2013

CS122 Lecture 4 Winter Term,

Database System Concepts

QUERY EXECUTION: How to Implement Relational Operations?

Transcription:

Lecture IV Query Processing Kyumars Sheykh Esmaili

Basic Steps in Query Processing 2

Query Optimization Many equivalent execution plans Choosing the best one Based on Heuristics, Cost Will be discussed separately Today: Measure query costs Algorithms for evaluating (main) relational algebra operations Combine algorithms for individual operations in order to evaluate a complete expression 3

Measures of Query Cost Cost is generally measured as total elapsed time for answering query Many factors contribute to time cost disk accesses, CPU, or even network communication Typically disk access is the predominant cost, and is also relatively easy to estimate. Measured by taking into account Number of seeks Number of blocks read The cost of a seek is usually much higher than that of a block transfer read or write (one order of magnitude) 4

Measures of Query Cost (Cont.) For simplicity we just use the number of block transfers from disk and the number of seeks as the cost measures t T time to transfer one block t S time for one seek Cost for b block transfers plus S seeks b * t T + S * t S We do not include cost to writing output to disk in the cost formulae We ignore CPU costs for simplicity, as they tend to be much lower Real systems do take CPU cost into account, but they are clearly less significant 5

Outline Selection Operation Sorting Join Operation Other Operations Evaluation of Expressions 6

Selection Operator

Algorithms for 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 = b r * block transfers + 1 seek b r denotes number of blocks containing records from relation r If selection is on a key attribute, can stop on finding record cost = (b r /2) * block transfers + 1 seek This linear search can be always applied, regardless of: selection condition or ordering of records in the file, or availability of indices 8

Algorithms for Selection (Cont.) A2 (binary search). Applicable only if the selection is an equality comparison on the attribute on which file is ordered. Assumes that the blocks of a relation are stored contiguously Cost estimate (number of disk blocks to be scanned): cost of locating the first tuple by a binary search on the blocks o log 2 (b r ) * (t T + t S ) If there are multiple records satisfying selection o Add transfer cost of the number of blocks containing records that satisfy selection condition If b r is not too big, then most likely binary search doesn t pay off 9

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 = (h i + 1) * (t T + t S ) where h i denotes the height of the index Recall that the height of a B+-tree index is log n/2 (K), where n is the number of index entries per node and K is the number of search keys. E.g. for a relation r with 1.000.000 different search key, and with 100 index entries per node, h i = 4 Unless the relation is really small, this algorithms always pays off when indexes are available 10

Selections Using Indices (cont) A4 (primary index on nonkey, equality) Retrieve multiple records. Records will be on consecutive blocks Let b = number of blocks containing matching records Cost = h i * (t T + t S ) + t S + t T * b A5 (equality on search-key of secondary index). Retrieve a single record if the search-key is a candidate key Cost = (h i + 1) * (t T + t S ) Retrieve multiple records if search-key is not a candidate key each of n matching records may be on a different block Cost = (h i + n) * (t T + t S ) o Can be very expensive if n is big! Note that it multiplies the time for seeks by n. 11

Selections Involving Comparisons One can implement selections of the form σ A V (r) or σ A V (r) by using a linear file scan or binary search just as before, or by using indices in the following ways: A6 (primary index, comparison). 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; o Using the index would be useless, and would requires extra seeks on the index file 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 o requires an I/O for each record (a lot!) o Linear file scan may be much cheaper!!!! 12

Complex Selections Conjunction: σ θ1 θ2... θn (r) A8 (conjunctive selection using one index). Select a combination of θ i and algorithms A1-A7 that results in the least cost for σ θi (r). Test other conditions on tuple after fetching it into memory buffer. In this case the choice of the first condition is crucial! One must use estimates to know which one is better 13

Complex Selections Algorithms Disjunction:σ θ1 θ2... θn (r). A9 (disjunctive selection by union of identifiers). Applicable only 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 14

Sort Operator

Sorting Sorting algorithms are important in query processing at least for two reasons: The query itself may require sorting (order by clause) Some algorithms for other operations, like join, set operations and aggregation, require that relation are previously sorted To sort a relation: We may build an index on the relation, and then use the index to read the relation in sorted order. This only sorts the relation logically, not physically May lead to one disk block access for each tuple. For relations that fit in memory sorting algorithms that you ve studied before, like quicksort, can be used. For relations that don t fit in memory special algorithms are required, that take into account the measures in terms of disc transfers and seeks. 16

External Sort-Merge It is a sorting algorithm to be used when the whole relation does not fit in memory. 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 (with your favorite sorting algorithm) (c) Write sorted data to run R i ; increment i. Let the final value of i be N 2. Merge the runs (next slide).. 17

External Sort-Merge (Cont.) 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: 18

External Sort-Merge (Cont.) If N 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. 19

Example: External Sorting Using Sort-Merge 20

External Merge Sort Transfer Cost Cost analysis: Total number of merge passes required: log M 1 (b r /M). Block transfers for initial run creation as well as in each pass is 2b r for final pass, we don t count write cost o 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 block transfers for external sorting: b r ( 2 log M 1 (b r / M) + 1) 21

External Merge Sort Seek Cost Cost of seeks During run generation: one seek to read each run and one seek to write each run 2 b r / M During the merge phase Buffer size: b b (read/write b b blocks at a time) Need 2 b r / b b seeks for each merge pass o except the final one which does not require a write Total number of seeks: 2 b r / M + b r / b b (2 log M 1 (b r / M) -1) 22

Join Operator

Join Operation Several different algorithms to implement joins exist (not counting with the ones involving parallelism) Nested-loop join Block nested-loop join Indexed nested-loop join Merge-join Hash-join As for selection, the choice is based on cost estimate Examples in next slides use the following information Number of records of customer: 10.000 depositor: 5.000 Number of blocks of customer: 400 depositor: 100 24

Nested-Loop Join The simplest join algorithms, that can be used independently of everything (like the linear search for selection) To compute the theta join r θ s for each tuple t r in r do begin for each tuple t s in s do begin test pair (t r,t s ) to see if they satisfy the join condition θ if they do, add t r t s to the result. end end r is called the outer relation and s the inner relation of the join. Quite expensive in general, since it requires to examine every pair of tuples in the two relations. 25

Nested-Loop Join (Cont.) In the worst case, if there is enough memory only to hold one block of each relation, the estimated cost is n r b s + b r block transfers, plus n r + b r seeks If the smaller relation fits entirely in memory, use that as the inner relation. Reduces cost to b r + b s block transfers and 2 seeks In general, it is much better to have the smaller relation as the outer relation The number of block transfers is multiplied by the number of blocks of the inner relation However, if the smaller relation is small enough to fit in memory, one should use it as the inner relation! The choice of the inner and outer relation strongly depends on the estimate of the size of each relation 26

Nested-Loop Join Cost in Example Assuming worst case memory availability cost estimate is with depositor as outer relation: 5.000 400 + 100 = 2.000.100 block transfers, 5.000 + 100 = 5100 seeks with customer as the outer relation 10.000 100 + 400 = 1.000.400 block transfers, 10.400 seeks If smaller relation (depositor) fits entirely in memory, the cost estimate will be 500 block transfers and 2 seeks Instead of iterating over records, one could iterate over blocks. This way, instead of n r b s + b r we would have b r b s + b r block transfers This is the basis of the block nested-loops algorithm (next slide). 27

Block Nested-Loop Join Variant of nested-loop join in which every block of inner relation is paired with every block of outer relation. for each block B r of r do begin for each block B s of s do begin for each tuple t r in B r do begin for each tuple t s in B s do begin Check if (t r,t s ) satisfy the join condition if they do, add t r t s to the result. end end end end 28

Block Nested-Loop Join Cost Worst case estimate: b r b s + b r block transfers and 2 * b r seeks Each block in the inner relation s is read once for each block in the outer relation (instead of once for each tuple in the outer relation Best case (when smaller relation fits into memory): b r + b s block transfers plus 2 seeks. Some improvements to nested loop and block nested loop algorithms can be made: Scan inner loop forward and backward alternately, to make use of the blocks remaining in buffer (with LRU replacement) Use index on inner relation if available to faster get the tuples which match the tuple of the outer relation at hands 29

Indexed Nested-Loop Join 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 In some cases, it pays to construct an index just to compute a join. For each tuple t r in the outer relation r, use the index on s to look up tuples in s that satisfy the join condition with tuple t r. Worst case: buffer has space for only one page of r, and, for each tuple in r, we perform an index lookup on s. Cost of the join: b r (t T + t S ) + n r c Where c is the cost of traversing index and fetching all matching s tuples for one tuple or r c can be estimated as cost of a single selection on s using the join condition (usually quite low, when compared to the join) If indices are available on join attributes of both r and s, use the relation with fewer tuples as the outer relation. 30

Example of Nested-Loop Join Costs Compute depositor customer, with depositor as the outer relation. Let customer have a primary B + -tree index on the join attribute customer-name, which contains 20 entries in each index node. Since customer has 10.000 tuples, the height of the tree is 4, and one more access is needed to find the actual data depositor has 5.000 tuples For nested loop: 2.000.100 block transfers and 5.100 seeks Cost of block nested loops join 400*100 + 100 = 40.100 block transfers + 2 * 100 = 200 seeks Cost of indexed nested loops join 100 + 5.000 * 5 = 25.100 block transfers and seeks. CPU cost likely to be less than that for block nested loops join However in terms of time for transfers and seeks, in this case using the index doesn t pay (this is specially so because the relations are small) 31

Merge-Join 1. Sort both relations on their join attribute (if not already sorted on the join attributes). 2. Merge the sorted relations to join them 1. Join step is similar to the merge stage of the sort-merge algorithm. 2. Main difference is handling of duplicate values in join attribute every pair with same value on join attribute must be matched 32

Merge-Join (Cont.) Can be used only for equi-joins and natural joins Each block needs to be read only once (assuming that all tuples for any given value of the join attributes fit in memory) Thus the cost of merge join is (where b b is the number of blocks in allocated in memory): b r + b s block transfers + b r / b b + b s / b b seeks + the cost of sorting if relations are unsorted. hybrid merge-join: If one relation is sorted, and the other has a secondary B + -tree index on the join attribute Merge the sorted relation with the leaf entries of the B + -tree. Sort the result on the addresses of the unsorted relation s tuples Scan the unsorted relation in physical address order and merge with previous result, to replace addresses by the actual tuples Sequential scan more efficient than random lookup 33

Hash-Join Also only applicable for equi-joins and natural joins. A hash function h is used to partition tuples of both relations h maps JoinAttrs values to {0, 1,..., n}, where JoinAttrs denotes the common attributes of r and s used in the natural join. r 0, r 1,..., r n denote partitions of r tuples Each tuple t r r is put in partition r i where i = h(t r [JoinAttrs]). s 0, s 1..., s n denotes partitions of s tuples Each tuple t s s is put in partition s i, where i = h(t s [JoinAttrs]). General idea: Partition the relations according to this Then perform the join on each partition r i and s i There is no need to compute the join between different partitions since an r tuple and an s tuple that satisfy the join condition will have the same value for the join attributes. If that value is hashed to some value i, the r tuple has to be in r i and the s tuple in s i. 34

Hash-Join (Cont.) 35

Hash-Join Algorithm The hash-join of r and s is computed as follows. 1. Partition the relation s using hashing function h. When partitioning a relation, one block of memory is reserved as the output buffer for each partition. 2. Partition r similarly. 3. For each i: (a) Load s i into memory and build an in-memory hash Relation s is called the build input and r is called the probe input. index on it using the join attribute. This hash index uses a different hash function than the earlier one h. (b) Read the tuples in r i from the disk one by one. For each tuple t r locate each matching tuple t s in s i using the inmemory hash index. Output the concatenation of their attributes. 36

Hash-Join algorithm (Cont.) The number of partitions n for the hash function h is chosen such that each s i should fit in memory. Typically n is chosen as b s/m * f where f is a fudge factor, typically around 1.2, to avoid overflows The probe relation partitions r i need not fit in memory Recursive partitioning required if number of partitions n is greater than number of pages M of memory. instead of partitioning n ways, use M 1 partitions for s Further partition the M 1 partitions using a different hash function Use same partitioning method on r Rarely required: e.g., recursive partitioning not needed for relations of 1GB or less with memory size of 2MB, with block size of 4KB. So is not further considered here (see the book for details on the associated costs) 37

Cost of Hash-Join The cost of hash join is 3(b r + b s ) +4 n h block transfers + 2( b r / b b + b s / b b ) seeks If the entire build input can be kept in main memory no partitioning is required Cost estimate goes down to b r + b s. For the running example, assume that memory size is 20 blocks b depositor = 100 and b customer = 400. depositor is to be used as build input. Partition it into five partitions, each of size 20 blocks. This partitioning can be done in one pass. Similarly, partition customer into five partitions, each of size 80. This is also done in one pass. Therefore total cost, ignoring cost of writing partially filled blocks: 3(100 + 400) = 1.500 block transfers + 2( 100/3 + 400/3 ) = 336 seeks The best we had up to here was 40.100 block transfers plus 200 seeks (for block nested loop) or 25,100 block transfers and seeks (for index nested loop) 38

Other Operators

Other Operations Duplicate elimination can be implemented via hashing or sorting. On sorting duplicates will come adjacent to each other, and all but one set of duplicates can be deleted. Optimization: duplicates can be deleted during run generation as well as at intermediate merge steps in external sort-merge. Hashing is similar duplicates will come into the same bucket. Projection: perform projection on each tuple followed by duplicate elimination. 40

Other Operations : Aggregation Aggregation can be implemented in similarly to duplicate elimination. Sorting or hashing can be used to bring tuples in the same group together, and then the aggregate functions can be applied on each group. Optimization: combine tuples in the same group during run generation and intermediate merges, by computing partial aggregate values For count, min, max, sum: keep aggregate values on tuples found so far in the group. o When combining partial aggregate for count, add up the aggregates For avg, keep sum and count, and divide sum by count at the end 41

Other Operations : Set Operations Set operations (, and ): can either use variant of merge-join after sorting, or variant of hash-join. E.g., Set operations using hashing: 1. Partition both relations using the same hash function 2. Process each partition i as follows. 1. Using a different hashing function, build an in-memory hash index on r i. 2. Process s i as follows r s: 1. Add tuples in s i to the hash index if they are not already in it. 2. At end of s i add the tuples in the hash index to the result. r s: 1. output tuples in s i to the result if they are already there in the hash index r s: 1. for each tuple in s i, if it is there in the hash index, delete it from the index. 2. At end of s i add remaining tuples in the hash index to the result. 42

Evaluation of Expressions

Evaluation of Expressions So far we have seen algorithms for individual operations These have then to be combined to evaluate complex expressions, with several 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 44

Materialization Evaluate one operation at a time starting at the lowest-level Use intermediate results materialized into temporary relations to evaluate next-level operations 45

Materialization at Work Application scan scan scan R S T 46

Materialization at Work Application Results of Scanning R scan scan scan R S T 47

Materialization at Work Application Results of Scanning R Results of Scanning S scan scan scan R S T 48

Materialization at Work Application Results of Joining R and S Results of Scanning R Results of Scanning S scan scan scan R S T 49

Materialization at Work Application Results of Joining R and S Results of Scanning R Results of Scanning S scan scan scan Results of Scanning T R S T 50

Materialization at Work Application Final Results Results of Joining R and S Results of Scanning R Results of Scanning S scan scan scan Results of Scanning T R S T 51

Materialization Summary Advantages Materialized evaluation is always applicable Disadvantages It may require considerable storage space cost of writing results to disk and reading them back can be quite high 52

Pipelining Pipelined evaluation : evaluate several operations simultaneously, passing the results of one operation on to the next. It is much cheaper than materialization: there is no need to store a temporary relation to disk. Pipelining may not always be possible e.g., sort and hash-join where a preliminary phase is required over the whole relations Pipelines can be executed in two ways: demand driven and producer driven 53

Pipelining (Cont.) In producer-driven (or eager or push) 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 In demand driven (or lazy, or pull) 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 54

Pipelining (Cont.) Implementation of pull pipelining Each operation is implemented as an iterator implementing the following operations open() o E.g. file scan: initialize file scan state: pointer to beginning of file o E.g. merge join: sort relations; state: pointers to beginning of sorted relations next() o E.g. for file scan: Output next tuple, and advance and store file pointer o E.g. for merge join: continue with merge from earlier state till next output tuple is found. Save pointers as iterator state. close() 55

Iterator Model at Work Application scan scan scan R S T 56

Iterator Model at Work JDBC: execute() Application scan scan scan R S T 57

Iterator Model at Work JDBC: execute() Application open() scan scan scan R S T 58

Iterator Model at Work JDBC: execute() Application open() open() open() scan scan scan R S T 59

Iterator Model at Work JDBC: execute() Application open() open() scan open/close for scan scan each R tuple R S T 60

Iterator Model at Work JDBC: execute() Application open() scan scan open/close for scan each R,S tuple R S T 61

Iterator Model at Work JDBC: next() Application next() next() next() scan scan scan R S T 62

Iterator Model at Work JDBC: next() Application next() next() r1 scan scan scan R S T 63

Iterator Model at Work JDBC: next() Application next() next() r1 scan open() scan scan R S T 64

Iterator Model at Work JDBC: next() Application next() next() r1 scan next() scan scan R S T 65

Iterator Model at Work JDBC: next() Application next() next() r1 scan s1 scan scan R S T 66

Iterator Model at Work JDBC: next() Application next() next() r1 scan next() scan scan R S T 67

Iterator Model at Work JDBC: next() Application next() next() r1 scan s2 scan scan R S T 68

Iterator Model at Work JDBC: next() Application next() r1, s2 scan scan scan R S T 69

Iterator Model at Work JDBC: next() Application next() r1, s2 scan scan open() scan R S T 70

Iterator Model at Work JDBC: next() Application next() r1, s2 scan scan next() scan R S T 71

Iterator Model at Work JDBC: next() Application next() r1, s2 scan scan t1 scan R S T 72

Iterator Model at Work JDBC: next() Application r1, s2, t1 scan scan scan R S T r2, r3, s3, s4, t2, t3, 73 73

Iterator Model Summary Advantages generic interface for all operators: great information hiding easy to implement iterators (clear what to do in any phase) works well with JDBC and embedded SQL no overheads in terms of main memory Disadvantages Not applicable to all cases Blocking operators (e.g. Sort) high overhead of method calls poor instruction cache locality 74