






































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!







































Basic Concepts
Distributed Cost Model
Database Statistics
Joins and Semijoins
Query Optimization Algorithms
Acknowledgements:
I am indebted to Arturas Mazeika for providing me his slides of this course.
J. Gamper
Query optimization:
Process of producing an op-
represents an execution strategy for the querytimal (close to optimal) query execution plan which
-
sider different orderings of the operationsThe main task in query optimization is to con-
Centralized query optimization:
-
space of equivalent query treesFind (the best) query execution plan in the
-
Minimize an objective cost function
-
Gather statistics about relations
Distributed query optimization brings additional issues
-
Linear query trees are not necessarily a good choice
-
Bushy query trees are not necessarily a bad choice
-
What and where to ship the relations
-
How to ship relations (ship as a whole, ship as needed)
-
When to use semi-joins instead of joins
J. Gamper
Reduction
of the search space
-
∗ Restrict by means of heuristics
Perform unary operations before binary operations, etc
-
∗ Restrict the shape of the join tree
Consider the type of trees (linear trees, vs. bushy ones)
Linear Join Tree
Bushy Join Tree
J. Gamper
There are two main strategies to
scan the search space
-
Deterministic
-
Randomized
Deterministic scan
of the search space
-
Start from base relations and build plans by adding one relation at each step
-
(dynamic programming approach)Breadth-first strategy: build all possible plans before choosing the “best” plan
-
Depth-first strategy: build only one plan (greedy approach)
J. Gamper
Two different types of
cost functions
can be used
-
Reduce
total time
cost component as possibleReduce each cost component (in terms of time) individually, i.e., do as little for each
Optimize the utilization of the resources (i.e., increase system throughput)
-
Reduce
response time
Do as many things in parallel as possible
May increase total time because of increased total activity
J. Gamper
Total time
: Sum of the time of all individual components
-
Local processing time: CPU time + I/O time
-
Communication time: fixed time to initiate a message + time to transmit the data
Total
time
CPU
instructions
I (^) / O
Os
MSG
messages
TR
bytes
The individual components of the total cost have different weights:
-
∗ Wide area network
Message initiation and transmission costs are high
Local processing cost is low (fast mainframes or minicomputers)
Ratio of communication to I/O costs is 20:
-
∗ Local area networks
Communication and local processing costs are more or less equal
Ratio of communication to I/O costs is 1:1.6 (10MB/s network)
J. Gamper
Example:
Query at site 3 with data from sites 1 and 2.
-
Assume that only the communication cost is considered
-
Total
time
MSG
TR
∗ ( x + y )
-
Response
time
= max
MSG
TR
x, T
MSG
TR
y }
J. Gamper
The
primary cost factor
is the
size of intermediate relations
-
that are produced during the execution and
-
different sitemust be transmitted over the network, if a subsequent operation is located on a
It is costly to compute the size of the intermediate relations precisely.
Instead
global statistics of relations and fragments
are computed and used to
provide approximations
J. Gamper
Selectivity factor
of an operation: the proportion of tuples of an operand relation that
participate in the result of that operation
Assumption: independent attributes and uniform distribution of attribute values
Selectivity factor of selection
σ ( A
value
card
A
( R
))
σ ( A > value
max(
value
max(
min(
σ ( A < value
value
min(
max(
min(
SF Properties of the selectivity factor of the selection
σ ( p ( A i ) ∧ p ( A j
σ ( p ( A i
σ ( p ( A j
σ ( p ( A i ) ∨ p ( A j
σ ( p ( A i
σ ( p ( A j
σ ( p ( A i
σ ( p ( A j
σ ( A
values
σ ( A
value
card
values
J. Gamper
Page 13
Cardinality
of intermediate results
-
Selection
card
σ P (^) ( R
)) =
σ ( P
(^) )
∗
card
-
∗ Projection
More difficult: duplicates, correlations between projected attributes are unknown
Simple if the projected attribute is a key
card
A
( R
)) =
card
-
Cartesian Product
card
card
card
-
∗ Union
upper bound:
card
card
card
lower bound:
card
max
card
, card
-
∗ Set Difference
upper bound:
card
card
lower bound:
J. Gamper
Selectivity factor
for semijoins: fraction of R-tuples that join with S-tuples
-
An approximation is the selectivity of
in
⊲ <
( S.A
card
A
( S
))
card
dom
Cardinality
of semijoin (general case):
card
⊲ <
( S.A
card
Example:
is a foreign key in
( S.A
is a primary key)
Then
and the result size corresponds to the size of
J. Gamper
Join ordering
is an important aspect in centralized DBMS, and it
is even more
important in a DDBMS
since joins between fragments that are stored at different sites
may increase the communication time.
Two approaches exist:
-
∗ Optimize the ordering of joins directly
INGRES and distributed INGRES
System
and System
∗
-
∗ costsReplace joins by combinations of semijoins in order to minimize the communication
Hill Climbing and SDD-
J. Gamper
Direct join ordering
of queries involving more than two relations is substantially more
complex
Example:
Consider the following query and the respective join graph, where we make
also assumptions about the locations of the three relations/fragments
P N O
EN O
J. Gamper
Example (contd.):
The query can be evaluated in at least 5 different ways.
-
Plan 1:
EMP
→
Site 2
Site 2: EMP’=EMP
⋊⋉
ASG
EMP’
→
Site 3
Site 3: EMP’
⋊⋉
PROJ
-
Plan 2:
ASG
→
Site 1
Site 1: EMP’=EMP
⋊⋉
ASG
EMP’
→
Site 3
Site 3: EMP’
⋊⋉
PROJ
-
Plan 3:
ASG
→
Site 3
Site 3: ASG’=ASG
⋊⋉
PROJ
ASG’
→
Site 1
Site 1: ASG’
⋊⋉
EMP
-
Plan 4:
PROJ
→
Site 2
Site 2: PROJ’=PROJ
⋊⋉
ASG
PROJ’
→
Site 1
Site 1: PROJ’
⋊⋉
EMP
-
Plan 5:
EMP
→
Site 2
PROJ
→
Site 2
Site 2: EMP
⋊⋉
PROJ
⋊⋉
ASG
To select a plan, a lot of information is needed, including
-
size
( EM P
(^) ) , size
( ASG
) , size
( P ROJ
) , size
( EM P
⋊⋉
ASG
) ,
size
( ASG
⋊⋉
P ROJ
)
-
Possibilities of parallel execution if response time is used
J. Gamper