


















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!



















Query Decomposition
Data Localization
Acknowledgements:
I am indebted to Arturas Mazeika for providing me his slides of this course.
J. Gamper
Query
decomposition:
Mapping
of
calcu-
project, join, rename)lus query (SQL) to algebra operations (select,
data.lations, without knowledge of the distribution ofBoth input and output queries refer to global re-
goodThe output query is semantically correct and
in
the
sense
that
redundant
work
is
avoided.
Normalization
: Transform query to a normalized form
Analysis
: Detect and reject ”incorrect” queries; possible only for a subset of relational
calculus
Elimination of redundancy
: Eliminate redundant predicates
Rewriting
: Transform query to RA and optimize query
J. Gamper
Example:
Consider the following query:
Find the names of employees who have been
working on project P1 for 12 or 24 months?
The query in SQL:
SELECT
ENAME
FROM
EMP, ASG
WHERE
EMP.ENO =
ASG.ENO
AND
ASG.PNO =
‘‘P1’’
AND
DUR =
12
OR
DUR =
24
The qualification in conjunctive normal form: EM P.EN O
=
ASG.EN O
∧
ASG.P N O
= ”
P (^) 1”
∧
( DU R
= 12
∨
DU R
= 24)
The qualification in disjunctive normal form:
( EM P.EN O
=
ASG.EN O
∧
ASG.P N O
= ”
P
(^) 1”
∧
DU R
= 12)
∨
( EM P.EN O
=
ASG.EN O
∧
ASG.P N O
= ”
P
(^) 1”
∧
DU R
= 24)
J. Gamper
Analysis:
Identify and reject type incorrect or semantically incorrect queries
Type incorrect
schemaChecks whether the attributes and relation names of a query are defined in the global
attributes, e.g., a comparisonChecks whether the operations on attributes do not conflict with the types of the
operation with an attribute of type string
Semantically incorrect
Checks whether the components contribute in any way to the generation of the result
that do not contain disjunction and negationOnly a subset of relational calculus queries can be tested for correctness, i.e., those
∗ Typical data structures used to detect the semantically incorrect queries are:
Connection graph (query graph)
Join graph
J. Gamper
Example:
Consider the following query and its query graph:
SELECT
ENAME,RESP
FROM
EMP, ASG, PROJ
WHERE
EMP.ENO =
ASG.ENO
AND
PNAME =
"CAD/CAM"
AND
DUR
≥
36
AND
TITLE =
"Programmer"
Since the graph
is not connected
, the query is semantically incorrect.
3 possible solutions:
Reject the query
Assume an implicit Cartesian Product between ASG and PROJ
Infer from the schema the missing join predicate ASG.PNO = PROJ.PNO
J. Gamper
Elimination of redundancy:
Simplify the query by eliminate redundancies, e.g.,
redundant predicates
languageRedundancies are often due to semantic integrity constraints expressed in the query
integrity and security constraintse.g., queries on views are expanded into queries on relations that satiesfy certain
Transformation rules are used, e.g.,
p
∧
p
p
p
∨
p
p
p
∧
true
p
p
∨
f alse
p
p
∧
f alse
f alse
p
∨
true
true
p
∧ ¬
p
f alse
p
∨ ¬
p
true
p 1
p 1
J. Gamper
Rewriting:
Convert relational calculus query to relational algebra query and find an
efficient
expression.
Example:
Find the names of employees other
project for either 1 or 2 years.than J. Doe who worked on the CAD/CAM
SELECT
ENAME
FROM
EMP,
ASG, PROJ
WHERE
EMP.ENO
= ASG.ENO
AND
ASG.PNO
= PROJ.PNO
AND
ENAME
"J.
Doe"
AND
PNAME
= "CAD/CAM"
AND
(DUR =
12
OR
DUR =
A
query tree
represents the RA-expression
Relations are leaves (FROM clause)
Result attributes are root (SELECT clause)
from the leaves to the rootIntermediate leaves should give a result
J. Gamper
By applying
transformation rules
, many different trees/expressions may be found that
are
equivalent
to the original tree/expression, but might be more efficient.
In the following we assume relations
1 ,... , A
n ) , S ( B 1
n ) , and
which is
union-compatible to
.
Commutativity
of binary operations
Associativity
of binary operations
Idempotence
of unary operations
A
(Π
A
( R
)) = Π
A
( R
)
σ p 1(
A
( σ p 2(
A
( R
)) =
σ p 1(
A
∧ p 2(
A
( R
)
J. Gamper
Example:
Two equivalent query trees for the previous example
ASG(ENO, PNO, RESP, DUR)PROJ(PNO, PNAME, BUDGET)Recall the schemas: EMP(ENO, ENAME, TITLE)
J. Gamper
Example (contd.):
Another equivalent query tree, which allows a more efficient query
evaluation, since the most selective operations are applied first.
J. Gamper
Example:
∗ into EMP1, EMP2, EMP3 as follows:Assume EMP is horizontally fragmented
EM P
(^) 1 =
σ EN O
≤ ” E 3”
( EM P
(^) )
∗
EM P
(^) 2 =
σ ” E 3”
” E 6”
( EM P
(^) )
∗ follows:ASG fragmented into ASG1 and ASG2 as
σ EN O
≤ ” E
3”
σ EN O>
” E
3”
Simple approach: Replace in all queries
EMP by (EMP
EMP
EMP3)
ASG by (ASG
ASG2)
Result is also called
generic query
In general, the
generic query is inefficient
since important restructurings and
simplifications can be done.
J. Gamper
Example (contd.)
: Parallelsim in the evaluation is often possible
followed by the union of the intermediate results.Depending on the horizontal fragmentation, the fragments can be joined in parallel
J. Gamper
Various more advanced
reduction techniques
are possible to generate simpler and
optimized queries.
Reduction of horizontal fragmentation (HF)
Reduction with selection
Reduction with join
Reduction of vertical fragmentation (VF)
Find empty relations
J. Gamper
Reduction with selection for HF
Consider relation
with horizontal fragmentation
1 , R
2 ,... , R
k } , where
R i = σ p i
Selections on fragments,
σ p j (^) ( R
i ) , that have a qualification contradicting the
qualification of the fragmentation generate empty relations, i.e.,
σ p j (^) ( R
i ) =
x ∈ R ( p i ( x ) ∧ p j
x ) =
f alse
predicate.Can be applied if fragmentation predicate is inconsistent with the query selection
Example:
Consider the query:
SELECT
FROM
EMP
WHERE
ENO=”E5”
selectionit is easy to detect that thetion with the union operation, After commuting the selec-
predicate
contra-
dicts the predicates of EMP
1
and EMP
3 .
J. Gamper