Chapter 13: Query Processing: Database System Concepts, 6 Ed
Chapter 13: Query Processing: Database System Concepts, 6 Ed
Chapter 13: Query Processing: Database System Concepts, 6 Ed
Join Operation
Other Operations Evaluation of Expressions
12.2
12.3
translate the query into its internal form. This is then translated into relational algebra.
Evaluation
12.4
expressions
called an evaluation-plan.
E.g., can use an index on salary to find instructors with salary < 75000, or can perform complete relation scan and discard instructors with salary 75000
12.5 Silberschatz, Korth and Sudarshan
How to measure query costs Algorithms for evaluating relational algebra operations
How to combine algorithms for individual operations in order to evaluate a complete expression
We study how to optimize queries, that is, how to find an evaluation plan with lowest estimated cost
Next lecture
12.6
query
* average-seek-cost * average-block-read-cost
12.7
12.8
space
Amount of real memory available to buffer depends on other concurrent queries and OS processes, known only during execution
We
often use worst case estimates, assuming only the minimum amount of memory needed for the operation is available
12.9
Selection Operation
File scan
Algorithm A1 (linear search). Scan each file block and test all
records to see whether they satisfy the selection condition. Cost estimate = br block transfers + 1 seek
br
If selection is on a key attribute, can stop on finding record cost = (br /2) block transfers + 1 seek Linear search can be applied regardless of
selection condition or
ordering of records in the file, or availability of indices Note: binary search generally does not make sense since data is not
stored consecutively
except when there is an index available, and binary search requires more seeks than index search
12.10 Silberschatz, Korth and Sudarshan
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. perform projection on each tuple followed by duplicate elimination.
Projection:
12.11
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. When combining partial aggregate for count, add up the aggregates
For
avg, keep sum and count, and divide sum by count at the end
12.12
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 ri. 2. Process si as follows r s: 1. Add tuples in si to the hash index if they are not already in it. 2. At end of si add the tuples in the hash index to the result.
12.13
output tuples in si to the result if they are already there in the hash index
for each tuple in si, if it is there in the hash index, delete it from the index. At end of si add remaining tuples in the hash index to the result.
12.14 Silberschatz, Korth and Sudarshan
r s:
1.
2.
Evaluation 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
12.15
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
12.16
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
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
12.17
Pipelining
Pipelined evaluation : evaluate several operations
simultaneously, passing the results of one operation on to the next. E.g., in previous expression tree, dont store result of
12.18
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
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
Pipelining (Cont.)
Implementation of demand-driven pipelining
Each operation is implemented as an iterator implementing the following operations open() E.g. file scan: initialize file scan state: pointer to beginning of file E.g.merge join: sort relations; state: pointers to beginning of sorted relations next() 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. close()
12.20 Silberschatz, Korth and Sudarshan
input tuples
E.g. hybrid hash join generates output tuples even as probe relation tuples in the in-memory partition (partition 0) are read in Double-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 r0 tuple is found, match it with existing s0 tuples, output matches, and save it in r0
Symmetrically for s0 tuples
12.21 Silberschatz, Korth and Sudarshan