Chapter 15: Query Processing
Chapter 15: Query Processing
Chapter 15: Query Processing
These slides are a modified version of the slides provided with the book:
Database System Concepts - 7th Edition 15.3 ©Silberschatz, Korth and Sudarshan
Basic Steps in Query Processing (cont.)
Parser and translator
Translate the (SQL) query into relational algebra
Parser checks syntax (e.g., correct relation and operator names)
Evaluation engine
The query-execution engine takes a query-evaluation plan, executes
that plan, and returns the answers to the query
Database System Concepts - 7th Edition 15.4 ©Silberschatz, Korth and Sudarshan
Basic Steps: Optimization
Input of optimization: a query in the form of an algebra expression
Database System Concepts - 7th Edition 15.5 ©Silberschatz, Korth and Sudarshan
Basic Steps: Optimization (Cont.)
Different query evaluation plans have different costs
User is not expected to specify least-cost plans
⋆
Silberschatz, Korth, and Sudarshan, Database System Concepts, 7° ed.
Database System Concepts - 7th Edition 15.6 ©Silberschatz, Korth and Sudarshan
How to measure query costs
(cost model)
These slides are a modified version of the slides provided with the book:
Thus
1. cost models (like ours) focus on resource consumption rather than response time
(optimizers minimize resource consumption rather than response time)
2. different optimizers may make different assumptions (parameters): every theoretical
analysis must be recast with the actual parameters used by the concrete system
(optimizer) to which the analysis is going to be applied
Database System Concepts - 7th Edition 15.8 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost (Cont.)
Query cost (total elapsed time for answering a query) is measured in terms of
different resources
disk access (I/O operation on disk)
CPU usage
(network communication for distributed DBMS – later in this course)
Typically disk access is the predominant cost, and is also relatively easy to
estimate. Measured by taking into account
Number of seeks (number of random I/O accesses)
Number of blocks read
Number of blocks written
It is generally assumed cost for writing to be twice as the cost for reading
(data is read back after being written to ensure the write was successful)
VERY IMPORTANT!!!
- “disk” refers to permanent drive for file storage, hard-disk, secondary memory, permanent memory
- “memory” refers to volatile drive for data storage, RAM, main memory, buffer
These are all used as synonims
This is a so far accepted choice for measuring query costs (cost model).
New technologies: faster hard-disks (solid-state drives – SSD) and cheaper (thus bigger) RAM
might direct towards different cost models (e.g., based also on CPU usage or RAM I/O operations)
Database System Concepts - 7th Edition 15.9 ©Silberschatz, Korth and Sudarshan
Measures of Query Cost (Cont.)
We ignore difference between writing and reading: we just consider
tS – time for one seek
tT – time to transfer one block
Example: cost for b block transfers plus S seeks
b * tT + S * t S
Values of tT and tS must be calibrated for the specific disk system
Typical values (2018): tS = 4 ms, tT = 0.1 ms
Some DBMS performs, during installation, seeks and block transfers to
estimate average values
We ignore CPU costs for simplicity
Real systems usually do take CPU cost into account
We do not include cost to writing output to disk in our cost formulae
Database System Concepts - 7th Edition 15.10 ©Silberschatz, Korth and Sudarshan
Algorithms for evaluating relational
algebra operations
These slides are a modified version of the slides provided with the book:
At the physical level, records are stored (on permanent disks) in files
(managed and organized by the filesystem)
We assume files are organized according to sequential file
organization
i.e., a file is stored in contiguous blocks, with records ordered according
to some attribute(s) – not necessarily ordered by primary key
Database System Concepts - 7th Edition 15.12 ©Silberschatz, Korth and Sudarshan
Selection Operation
File scan (relation scan without indices)
PROs: can be applied to any file, regardless of its ordering, availability of indices,
nature of selection operation, etc.
CONs: it is slow
Algorithm A1 (linear search). Retrieve and scan each file block and
test all records to see whether they satisfy the selection condition
br denotes number of blocks containing records from relation r
Cost estimate??? (selection on a generic, non-key attribute)
cost = br block transfers + 1 seek = tS + br * tT
We assume blocks are stored contiguously so 1 seek operation is enough (disk head
does not need to move to seek next block)
Database System Concepts - 7th Edition 15.13 ©Silberschatz, Korth and Sudarshan
Selections Using Indices
⋆
Silberschatz, Korth, and Sudarshan, Database System Concepts, 6° ed.
Database System Concepts - 7th Edition 15.14 ©Silberschatz, Korth and Sudarshan
Selections Using Indices
Database System Concepts - 7th Edition 15.15 ©Silberschatz, Korth and Sudarshan
Selections Involving Comparisons
Database System Concepts - 7th Edition 15.16 ©Silberschatz, Korth and Sudarshan
Selections Involving Comparisons
Database System Concepts - 7th Edition 15.17 ©Silberschatz, Korth and Sudarshan
Summary of costs for selections
Database System Concepts - 7th Edition 15.18 ©Silberschatz, Korth and Sudarshan
Complex Selections
conjunctions, disjunctions, and negation of simple conditions
A7 (conjunctive selection using 1 index)
θ1 AND θ2 AND … AND θn
If there is at least 1 index useful for 1 simple condition θi, then
use the right algorithm among A2-A6 to retrieve tuple satisfying θi
and in the meantime check for the other simple conditions on records selected .
Cost?
Cost is given by the cost of chosen algorithm for the chosen condition
cost depend on the choice of the condition (and the choice of the algorithm)
example: id=x AND dept=y (teacher)
primary index over id and secondary index over dept
id is primary key
it is convenient to choose id=x (with algorithm A2)
A8 (conjunctive selection using composite index)
use a composite index over attributes involved in all or some of the simple conditions
(it only works with equivalences), if any
example: name=x AND dept=y (teacher)
use composite index over pair (name,dept) with algorithm A4 (secondary index, equality on non-key)
Database System Concepts - 7th Edition 15.19 ©Silberschatz, Korth and Sudarshan
Complex Selections (cont’d)
A9 (conjunctive selection by intersection of identifiers)
Recall that indices we consider have pointers to records (rather than actual records)
Scan indices but do not access records, just collect sets of pointers (one per index)
Compute the intersection, and then access records (select only the tuples that
match also the conditions for which there is no index). Cost?
Cost: cost of scanning indices plus cost of accessing blocks with matching records
(some block can be accessed more than once if pointers are not sorted)
Optimization: order records in the intersection and then access them in sorted order.
Advantages:
less seeks and transfers: no block is accessed twice (2 records in the same block are retrieved together)
also, some additional seek time is saved as blocks are transferred in sorted order (disk-arm is minimized)
A10 (disjunctive selection by union of identifiers)
If ALL conditions can be checked through some index, then similar to A9
Scan indices but do not access records, just collect sets of pointers (one per index)
Compute the union, and then access records (in sorted order)
Cost: cost of scanning all indices plus cost of accessing records
If even only 1 condition has no associate index, then A1 (linear scan)
Database System Concepts - 7th Edition 15.20 ©Silberschatz, Korth and Sudarshan
2 more things on selections
2. A4, A6, A9, A10 are very inefficient (possibly worse than linear scan) due to some
blocks possibly accessed more than once
few records to be retrieved/no block is accessed more than once: better to use index scan,
a lot of records to be retrieved/several blocks accessed more than once: better to use liner scan
Solution: collect and sort pointers before accessing records (see optimization for A9)
Improved solution, based on bitmap structure: bitmap is a vector of bits (as many as number of blocks
used by the relation)
visit index without accessing records: in the bitmap set to 1 the bits corresponding to blocks to be accessed
linear scan guided by bitmap (sorted order access thanks to bitmap without actually performing a sorting)
hybrid solution (mix between linear scan and index access)
no block accessed more than once : slightly worse than index access,
all blocks containing at least one matching condition: slightly worse than liner scan
thus, the cost is slightly worse than the optimal plan (linear or index scan): good compromise
Database System Concepts - 7th Edition 15.21 ©Silberschatz, Korth and Sudarshan
Sorting
Reasons for sorting
Explicitly requested by SQL query
SELECT …
FROM …
SORT BY …
Needed to efficient executions of join operations
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, standard sorting techniques
like quick-sort can be used. For relations that don’t fit in
memory, external sort-merge algorithm is a good choice
Database System Concepts - 7th Edition 15.22 ©Silberschatz, Korth and Sudarshan
External Sort-Merge
Let M denote number of blocks that can fit in memory.
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 Ri
(d) Increment i
Let the final value of i be N (number of runs)
Database System Concepts - 7th Edition 15.23 ©Silberschatz, Korth and Sudarshan
External Sort-Merge (Cont.)
2. Merge the runs (N-way merge). We assume (for now) that N < M.
Database System Concepts - 7th Edition 15.24 ©Silberschatz, Korth and Sudarshan
External Sort-Merge (Cont.)
Database System Concepts - 7th Edition 15.25 ©Silberschatz, Korth and Sudarshan
Example: External Sorting Using Sort-Merge
M=3
Database System Concepts - 7th Edition 15.26 ©Silberschatz, Korth and Sudarshan
Join Operation
Several different algorithms to implement joins
Nested-loop join
Block nested-loop join
Indexed nested-loop join
Merge-join
Hash-join
Choice based on cost estimate
Running example : students takes
where
Number of records of student: 5,000
Number of blocks of student: 100
Number of records of takes: 10,000
Number of blocks of takes: 400
Database System Concepts - 7th Edition 15.27 ©Silberschatz, Korth and Sudarshan
Nested-Loop Join
r is called the outer relation and s the inner relation of the join
Requires no indices and can be used with any kind of join
condition
Expensive since it examines every pair of tuples in the two
relations
Database System Concepts - 7th Edition 15.28 ©Silberschatz, Korth and Sudarshan
Nested-Loop Join (Cont.)
If the smaller relation fits entirely in memory, use that as the inner relation
br + bs block transfers and 2 seeks
(same cost in the best case scenario, when both relations fit in memory)
Worst case (there is enough memory forn only one block for each relation)
# of block transfer:
nr bs + br (br transfers to read relation r + nrbs transfers to read s for each tuple in r)
# of seeks:
nr + b r (br seeks to read relation r + nr seeks to read s for each tuple in r)
Running example (join between students and takes – assuming worst case memory availability)
with student as outer relation:
5,000 400 + 100 = 2,000,100 block transfers
5,000 + 100 = 5,100 seeks
assuming t = 40 * t we have a total of: 2,204,100 * t
S T, T
with takes as the outer relation
10,000 100 + 400 = 1,000,400 block transfers
10,000 + 400 = 10,400 seeks
assuming t = 40 * t we have a total of: 1,416,400 * t
S T, T
if smaller relation (student) fits entirely in memory, the cost estimate will be 500 block transfers and 2 seeks
Database System Concepts - 7th Edition 15.29 ©Silberschatz, Korth and Sudarshan
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 Br of r do
for each block Bs of s do
for each tuple tr in Br do
for each tuple ts in Bs do
check if (tr,ts) satisfy the join condition
if it does, add tr • ts to the result
end
end
end
end
Database System Concepts - 7th Edition 15.30 ©Silberschatz, Korth and Sudarshan
Indexed Nested-Loop Join
If an index is available for one of the relations on the attribute of the join condition
then use such a relation as inner relation in a nested-loop join
Instead of doing a linear scan as inner loop, do an index scan
Database System Concepts - 7th Edition 15.31 ©Silberschatz, Korth and Sudarshan
How to combine algorithms for
individual operations in order to
evaluate a complex expression
These slides are a modified version of the slides provided with the book:
Database System Concepts - 7th Edition 15.33 ©Silberschatz, Korth and Sudarshan
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., name
( building "Watson " (department) instructor)
Database System Concepts - 7th Edition 15.34 ©Silberschatz, Korth and Sudarshan
Pipelining
Pipelined evaluation: evaluate several operations simultaneously, passing
(partial) results of one operation on to the next as they are generated (es.,
single records), without writing them on disk
E.g., in previous expression tree, don’t store result of
Database System Concepts - 7th Edition 15.35 ©Silberschatz, Korth and Sudarshan
End of Chapter
These slides are a modified version of the slides provided with the book: