You are on page 1of 66

Query Processing + Optimization: Outline

• Operator Evaluation Strategies


 Query processing in general
 Selection
 Join
• Query Optimization
 Heuristic query optimization
 Cost-based query optimization
• Query Tuning

1
Query Processing + Optimization
• Operator Evaluation Strategies
 Selection
 Join
• Query Optimization
• Query Tuning
Architectural Context
Application Programmers
DBA Staff Casual Application
Users Parametri
Programs
c Users
DDL Privileged Interactive
Precompiler
Statements Commands Query
Host Language
Compiler
Query Canned
Compiler Transactions
Query Execution DDL
DDL Plan Statements
Compiler

Run-time
Evaluator

Transaction and
DBMS Data Manager

Data Dictionary
Data Files
Evaluation of SQL Statement
• The query is evaluated in a different order.
 The tables in the from clause are combined using
Cartesian products.
 The where predicate is then applied.
 The resulting tuples are grouped according to the group
by clause.
 The having predicate is applied to each group,
possibly eliminating some groups.
 The aggregates are applied to each remaining
group. The select clause is performed last.
Overview of Query Processing
Query in high-level language
1. Parsing and translation
Scanning, Parsing, and
2. Optimization
Semantic Analysis
3. Evaluation
Intermediate form of query
4. Execution
Query Optimization
Execution Plan

Query Code Generator

Code to execute the query


Runtime Database
Processor
Result of query
Selection Queries
• Primary key, point
FilmID = 2 (Film)
• Point
Title = ‘Terminator’ (Film)
• Range
1 < RentalPrice < 4 (Film)
• Conjunction
Type = ‘M’  Distributor = ‘MGM’ (Film)
• Disjunction
PubDate < 2004  Distributor = ‘MGM’ (Film)
Selection Strategies
• Linear search
 Expensive, but always applicable.
• Binary search
 Applicable only when the file is appropriately ordered.
• Hash index search
 Single record retrieval; does not work for range queries.
 Retrieval of multiple records.
• Clustering index search
 Multiple records for each index item.
 Implemented with single pointer to block with
first associated record.
• Secondary index search
 Implemented with dense pointers, each to a single record.
Selection Strategies for Conjunctive Queries
• Use any available indices for attributes involved
in simple conditions.
 If several are available, use the most selective index. Then
check each record with respect to the remaining
conditions.
• Attempt to use composite indices.
 This can be very efficient.
• Do intersection of record pointers.
 If several indices with record pointers are applicable to the
selection predicate, retrieve and intersect the pointers.
Then retrieve (and check) the qualifying records.

• Disjunctive queries provide little opportunity for


smart processing.
Joins
• Join Strategies
 Nested loop join
 Index-based join
 Sort-merge join
 Hash join

• Strategies work on a per block (not per record) basis.


 Need to estimate #I/Os (block retrievals)

• Relation sizes and join selectivities impact join cost.


 Query selectivity = #tuples in result / #candidates
 ‘More selective’ means smaller ‘selectivity value’
 For join, #candidates is the size of Cartesian product
Nested Loop Join and Index-Based Join
• Nested loop join
 Exhaustive comparison (i.e., brute force approach)
 The ordering (outer/inner) of files and allocation of
buffer space is important.

• Index-based join
 Requires (at least) one index on a join attribute.
 At times, a temporary index is created for the purpose of
a join.
 The ordering (outer/inner) of files is important.
Nested Loop
• Basically, for each block of the outer table (r), scan
the entire inner table (s).
 Requires quadratic time, O(n2)
 Improved when buffer is used.
Outer Main Memory
Buffer
r

spill when full Output


Scan Inner

s
Example of Nested-Loop Join
Customer nC.CustomerID = CO.EmpId CheckedOut
• Parameters
rCheckedOut = 40.000 rCustomer = 200
bCheckedOut = 2.000 bCustomer =
10 n = 6 (size of main
B
memory buffer)
• Algorithm:
repeat: read (nB - 2) blocks from outer relation
repeat: read 1 block from inner relation
compare tuples
• Cost: bouter + ( ⎡bouter/ (nB -2)⎤ )  binner
• CheckedOut as outer: 2.000 + ⎡2.000/4⎤  10 = 7.000
• Customer as outer: 10 + ⎡10/4⎤  2.000 = 6.010
Index-based Join
• Requires (at least) one index on a join attribute
 A temporary index can be created

for each record of s, index


queryon r
in index
r
Scan Outer
if joins
s
Output
spill
when full

Main Memory
Example of Index-Based Join
Customer nC.CustomerID = CO.EmpId CheckedOut
• Cost: bouter + router  cost use of index
• Assume that the video store has 10 employees.
 There are 10 distinct EmpIDs in CheckedOut.
• Assume 1-level index on CustomerID of Customer.
• Iterate through all 40.000 tuples in CheckedOut
(outer rel.)
 2.000 disk reads (bCheckedOut) to scan CheckedOut
 For each CheckedOut tuple, search for matching Customer
tuples using index.
 0 disk reads for index (in main memory) + 1 disk read for actual
data block
• Cost: 2.000 + 40.000  (0 + 1) = 42.000
Sort-Merge Join
• Sort each relation using multiway merge-sort
• Perform merge-join
Scan
r1
sorted •••
Main Memory
r
unsorted r2 sorted •••
r ••• r •••
rn ••• Match
Scan

sorted
s
Output
External or Disk-based Sorting
• Relation on disk often too large to fit into memory
• Sort in pieces, called runs
Main Memory r1
Buffer (N blocks)
Scan Output r2
when sorted
r •••
rM div N

Unsorted relation (M rlast


blocks)
Runs
(M div N runs each of
size N blocks, and maybe one
last run of < N leftover blocks)
External or Disk-based Sorting, Cont.
• Runs are now repeatedly merged
• One memory buffer used to collect output
Main Memory Main Memory
r1 m1
Buffer (N blocks) Buffer (N blocks)
r Output
m2
2
when merged
••• •••
rN - 1 output mlast - 1

••• output
mlast
rM div N
sorted runs (N blocks each)
rlast
M div N-1) runs each of size N*N-1
e blocks, and maybe one last run of <
r N*N-1 leftover blocks)
g
e
d
r
u
n
s
(
(
M

d
i
v

N
)
External Sorting (Multiway Merge Sort)
• Buffer size is nB = 4 (N)
Orginal rel.
32 5 1712 1 14 8 3 3130232629 2 2511 6 7 151628 4 9 221821102013272419

Initial runs
5 121732 1 3 8 14 23263031 2 112529 6 7 1516 4 9 2228 10182021 13192427

First merge
1 3 5 8 1214172326303132 2 4 6 7 9 11151622252829 1013181920202427

Second merge
1 2 3 4 5 6 7 8 9 1011121314151617181920212223242526272829303132

• Cost: 2  brelation + 2  brelation  ⎡lognB - 1 (brelation/nB)⎤


• 2  32 + 2  32  log3(32/4) = 192
Example of Sort-Merge Join
• Cost to sort CheckedOut (bCheckedOut= 10)
 CostSort ChecedOut = 2  2.000 + 2  2.000  ⎡log5(2.000/6)⎤ = 20.000
• Cost to sort Customer relation (bCustomer= 10)
 CostSort Customer= 2  10 + 2  10  ⎡log5(10/6)⎤ = 40
• Cost for merge join
 Cost to scan sorted Customer + cost to scan sorted CheckedOut
 Costmerge join= 10 + 2.000 = 2.010

• Costsort-merge join = CostSort Customer + CostSort ChecedOut + Costmerge join

• Costsort-merge join= 20.000 + 40 + 2.010 = 22.050


Hash Join
• Hash each relation on the join attributes
• Join corresponding buckets from each relation
Buckets from r Buckets from s

r1
r1 n s1 r1 n s1
r2 n s2
r2
r2 n s2
Relation
r •••
••• •••
rn
rn n sn rn n sn
Hash r (same for s)
Join corresponding r and s buckets
Partitioning Phase
• Partitioning phase: r divided into nh partitions. The
number of buffer blocks is nB. One block used for
reading r. (nh = nB -1)
 Similar with relation s
 I/O cost: 2  (br + bs)
OUTPUT Partitions
1

1
Relation INPUT 2
r or s hash 2
function
... h
nB -1

nB -1
Disk nB main memory buffers Disk
Joining Phase
• Joining (or probing) phase: nh iterations where ri n si.
 Load ri into memory and build an in-memory hash index on it using the
join attribute. (h2 needed, ri called build input)
 Load si, for each tuple in it, join it with ri using h2. (si called probe input)
 I/O cost: br + bs + 4  nh (each partition may have a partially filled block)
 One write and one read for each partially filled block
In-memory hash table for Join Result
Hash partition ri (< nB -1 blocks)
func.
h2
Partitions
of r & s h2

Input buffer for si Output


buffer

Disk
nB main Disk
memory
buffers
Hash Join Cost
• CostTotal = Cost Partitioning + CostJoining
= 3  (br + bs) + 2  nh
• Cost = 3  (2000 + 10) + 2  5 = 6040

• Any problem not considered?


 What if nh > nB -1? I.e., more partitions than available buffer blocks!
 How to solve it?
OUTPUT
1

Relation INPUT 2
r or s hash
function
... h
n-1
n B

Disk
nB main memory buffers
Recursive Partitioning
• Required if number of partitions nh is greater than
number of available buffer blocks nB -1.
 instead of partitioning nh ways, use nB – 1 partitions for s
 Further partition the nB – 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.

• Costhjrp = 2  (br + bs)  ⎡lognB - 1 (bs) -1⎤ + br + bs


• Costhjrp = 2  (2000 + 10)  ⎡log5 (10) -1⎤ + 2000 + 10
= 6030
Cost and Applicability of Join Strategies
• Nested-loop join
 Brute-force
 Can handle all types of joins (=, <, >)
• Index-based join
 Requires minimum one index on join attributes
• Sort-merge join
 Requires that the files are sorted on the join attributes.
 Sorting can be done for the purpose of the join.
 A variation is also applicable when secondary indices
are available instead.
• Hash join
 Requires good hashing functions to be available.
 Performance best if smallest relation fits in memory. ?
Query Processing + Optimization
• Operator Evaluation Strategies
• Query Optimization
 Heuristic Query Optimization
 Cost-based Query Optimization
• Query Tuning
Query Optimization
• Aim: Transform query into faster, equivalent query

equivalent query 1
equivalent query 2
query ... faster
equivalent query n quer
y

• Heuristic (logical) optimization


 Query tree (relational algebra) optimization
 Query graph optimization
• Cost-based (physical) optimization
Query Tree Optimization Example
• What are the names of customers living on Elm
Street who have checked out “Terminator”?
• SQL query:
SELECT Name
FROM Customer CU, CheckedOut CH, Film F
WHERE Title = ’Terminator’ AND F.FilmId = CH.FilmID
AND CU.CustomerID = CH.CustomerID AND CU.Street = ‘Elm’
• Canonical query tree Name
Title = ‘Terminator’  F.FilmId = CH.FilmID  CU.CustomerID = CH.CustomerID  CU.Street = ‘Elm’

 Note the use of
F
Cartesian product!
CU CH
Apply Selections Early
Name

 F.FilmId = CH.FilmID

CU.CustomerID = CH.CustomerID Title = ‘Terminator’

F

= ‘Elm’ CH
Street

CU
Apply More Restrictive Selections Early
Name

 CU.CustomerID = CH.CustomerID

 F.FilmId = CH.FilmID Street = ‘Elm’

CU

Title = ‘Terminator’ CH
F
Form Joins
Name

n CU.CustomerID = CH.CustomerID

n F.FilmId = CH.FilmID Street = ‘Elm’


CU
Title = ‘Terminator’ CH

F
Apply Projections Early
Name

n CU.CustomerID = CH.CustomerID

n F.FilmId = Name, CustomerID


CH.FilmID

FilmID FilmID, CustomerID Street


CU= ‘Elm’

Title = ‘Terminator’ CH

F
Some Transformation Rules
• Cascade of : c  c  ...  c (R) = c (c (...(c (R))...))
1 2 n 1 2 n
• Commutativity of : c (c (R)) = c (c (R))
1 2 2 1
• Commuting  with : L(c (R)) = c(L(R))
 Only if c involves solely attributes in L.
• Commuting  with n : c n S) = c (R) n S
(R
 Only if c involves solely attributes in R.
• Commuting  with set operations: c(R  S) = c(R)  c(S)
 Where  is one of , , or -.
• Commutativity of , , and n: R  S = S  R
 Where  is one of ,  and n.
• Associativity of n, , : (R  S)  T = R  (S  T)
Transformation Algorithm Outline
• Transform a query represented in relational algebra
to an equivalent one (generates the same result.)

• Step 1: Decompose  operations.


• Step 2: Move  as far down the query tree as possible.
• Step 3: Rearrange leaf nodes to apply the
most restrictive  operations first.
• Step 4: Form joins from  and subsequent  operations.
• Step 5: Decompose  and move down the query tree
as far as possible.
• Step 6: Identify candidates for combined operations.
Heuristic Query Optimization Summary
• Heuristic optimization transforms the query-tree by
using a set of rules (Heuristics) that typically (but
not in all cases) improve execution performance.
 Perform selection early (reduces the number of tuples)
 Perform projection early (reduces the number of attributes)
 Perform most restrictive selection and join operations
(i.e. with smallest result size) before other similar
operations.

• Generate initial query tree from SQL statement.


• Transform query tree into more efficient query tree,
via a series of tree modifications, each of which
hopefully reduces the execution time.
• A single query tree is involved.
Cost-Based Optimization
• Use transformations to generate multiple
candidate query trees from the canonical query
tree.
• Statistics on the inputs to each operator are needed.
 Statistics on leaf relations are stored in the system catalog.
 Statistics on intermediate relations must be estimated;
most important is the relations' cardinalities.
• Cost formulas estimate the cost of executing
each operation in each candidate query tree.
 Parameterized by statistics of the input relations.
 Also dependent on the specific algorithm used by the operator.
 Cost can be CPU time, I/O time, communication time,
main memory usage, or a combination.
• The candidate query tree with the least total cost
is selected for execution.
Relevant Statistics
• Per relation
 Tuple size
 Number of tuples (records): r
 Load factor (fill factor), percentage of space used in
each block
 Blocking factor (number of records per block): bfr
 Relation size in blocks: b
 Relation organization
 Number of overflow blocks
Relevant Statistics, cont.
• Per attribute
 Attribute size and type
 Number of distinct values for attribute A: dA
 Probability distribution over the values
 Representation, e.g., compressed
 Selection cardinality specifies the average size of
A = aR) for an arbitrary value a. (sA)
 Could be maintained for the “average” attribute value, or on a
per- value basis, as a histogram.
Relevant Statistics, cont.
• Per Index
 Base relation
 Indexed attribute(s)
 Organization, e.g., B+-Tree, Hash, ISAM
 Clustering index?
 On key attribute(s)?
 Sparse or dense?
 Number of levels (if appropriate)
 Number of first-level index blocks: b1
• General
 Available main memory blocks: N
Cost Estimation Example
Name
4
⋈CU.CustomerID = R.CustomerID

2 ⋈F.FilmId = R.FilmID 3
FilmID, CustomerID, Name

FilmID FilmID, CustomerID


1 Street = ‘Elm’

Title = ‘Terminator’ R CU

F
Operation 1:  followed by a 
• Statistics
 Relation statistics: rFilm= 5,000 bFilm= 50
 Attribute statistics: sTitle= 1
 Index statistics: Secondary Hash Index on Title.

• Result relation size: 1 tuple.

• Operation: Use index with ‘Terminator’, then


project on FilmID. Leave result in main memory (1
block).
• Cost (in disk accesses): C1 = 1 +1 = 2
Operation 2: n followed by a 
• Statistics
 Relation statistics: rCheckedOut= 40,000 bCheckedOut= 2,000
 Attribute statistics: sFilmID= 8
 Index statistics: Secondary B+-Tree Index for CheckedOut on
FilmID with 2 levels.

• Result relation size: 8 tuples.

• Operation: Index join using B+-Tree, then project on


CustomerID. Leave result in main memory (one block).

• Cost: C2 = 1 +1 + 8 = 10
Operation 3:  followed by a 
• Statistics
 Relation statistics: rCustomer= 200 bCustomer= 10
 Attribute statistics:sStreet= 10

• Result relation size: 10 tuples.

• Operation: Linear search of Customer. Leave result


in main memory (one block).

• Cost: C3 = 10
Operation 4: n followed by a 
• Operation: Main memory join on relations in
main memory.

• Cost: C4 = 0

• Total cost: C  2 1010 0  22


4

C i
i1
Comparison
• Heuristic query optimization
 Sequence of single query plans
 Each plan is (presumably) more efficient than the previous.
 Search is linear.
• Cost-based query optimization
 Many query plans generated.
 The cost of each is estimated, with the most efficient chosen.
 Search is multi-dimensional, usually using
dynamic programming. Still can be very expensive.
• Hybrid way
 Systems may use heuristics to reduce the number of
choices that must be made in a cost-based fashion.
Query Processing + Optimization
• Operator Evaluation Strategies
• Query Optimization
• Query Tuning
Query Tuning
• Query optimization is a very complex task.
 Combinatorial explosion.
 The task is to find one good query evaluation plan, not
the best one.
• No optimizer optimizes all queries adequately.
• There is a need for query tuning.
 All optimizers differ in their ability to optimize
queries, making it difficult to prescribe principles.
• Having to tune queries is a fact of life.
 Query tuning has a localized effect and is thus
relatively attractive.
 It is a time-consuming and specialized task.
 It makes the queries harder to understand.
 However, it is often a necessity.
 This is not likely to change any time soon.
Query Tuning Issues
• Need too many disk accesses (eg. Scan for a
point query)?
• Need unnecessary computation?
 Redundant DISTINT
SELECT DISTINCT cpr#
FROM Employee
WHERE dept = ‘computer’
• Relevant indexes are not used? (Next slide)
• Unnecessary nested subqueries?
• ……
Join on Clustering Index, and Integer
SELECT Employee.cpr#
FROM Employee, Student
WHERE Employee.name = Student.name
-->
SELECT Employee.cpr#
FROM Employee, Student
WHERE Employee.cpr# = Student.cpr#
Nested Queries
• Nested block is optimized independently, with the outer
tuple considered as providing a selection condition.
• Outer block is optimized with the cost of ‘calling’ nested
block computation taken into account.
• Implicit ordering of these blocks means that some good
strategies are not considered. The non-nested version of
the query is typically optimized better.
Nested block to optimize:
SELECT S.sname SELECT *
FROM Sailors S FROM Reserves R
WHERE EXISTS WHERE R.bid=103
(SELECT * AND S.sid= outer value
FROM Reserves R
Equivalent non-nested query:
WHERE R.bid=103 SELECT S.sname
AND R.sid=S.sid) FROM Sailors S, Reserves R
WHERE S.sid=R.sid
AND R.bid=103 50
Unnesting Nested Queries
• Uncorrelated sub-queries with aggregates.
 Most systems would compute the average only once.
SELECT ssn
FROM emp
WHERE salary > (SELECT AVG(salary) FROM emp)

• Uncorrelated sub-queries without aggregates.


SELECT ssn When is
FROM emp
WHERE dept IN (SELECT dept FROM this
techdept) acceptable
?
 Some systems may not use emp's index on dept, so
a transformation is desirable.
SELECT ssn
FROM emp, techdept
WHERE emp.dept = techdept.dept
51
Unnesting Nested Queries, cont.
• Watch out for duplicates! Consider a query and
its rewritten counterpart.
SELECT
AVG(salary) FROM
emp
WHERE manager IN (SELECT manager FROM techdept)
 Unnested version, with problems: (what’s the problem?)
SELECT AVG(salary)
FROM emp, techdept
WHERE emp.manager = techdept.manager
 This query may yield wrong results! A solution:
SELECT DISTINCT (manager) INTO temp
FROM techdept

SELECT
AVG(salary) FROM
emp, temp
WHERE emp. manager = temp. manager
Summary
• Query processing & optimization is the heart of
a relational DBMS.
• Heuristic optimization is more efficient to generate,
but may not yield the optimal query evaluation plan.
• Cost-based optimization relies on statistics gathered
on the relations (the default in most DBMSs).
• Until query optimization is perfected, query tuning
will be a fact of life.

You might also like