Query Processing and Optimization PDF
Query Processing and Optimization PDF
Query Processing and Optimization PDF
optimization
Objectives:
Reduce processing time
Reduce buffer memory
Reduce communication cost between sites
Low resource usage
Query transformation
Query optimization
Query preprocessor:
Scanning: key words, attribute names,
www.ptit.edu.vn
relations,…
Parsing: syntax checking, parse tree
representation
Validating: semantic checking (relations,
attributes, data types)
Query optimizer:
Select suitable strategy for query processing
www.ptit.edu.vn
GROUP BY-HAVING
Integrated queries: separate into query blocks
FROM Staff
WHERE Salary > (SELECT AVG(Salary)
FROM Staff
WHERE Gender = “Male”)
Global
Query decomposition Schema
Algebraic query on global relations
Control Fragment
www.ptit.edu.vn
Local
Global Optimization Schema
Distributed query execution plan
Else
{ORQ1, ..., ORQm, MRQ’} MRQ
For i1 to m
Output’ run(ORQi)
Output output output’
Endfor
R CHOOSE_ RELATION(MRQ’)
For each tuple t R
MRQ” substitute values for t in MRQ’
Output’ INGRES-QOA(MRQ”)
Output output output’
Endfor
Endif
End.
Lecture 3: {INGRES-QOA}
Query processing and optimization 24 Hoa Dinh Nguyen, PhD.
Department of Information Technology
Query Decomposition
Normalization: to transform the query to a
normalized form to facilitate further processing.
www.ptit.edu.vn
unnecessary.
A query is type incorrect: if any of its attribute or
relation names are not defined in the global schema,
or if operations are being applied to attributes of the
wrong type.
A query is semantically incorrect if its components
do not contribute in any way to the generation of the
result.
Hoa Dinh Nguyen, PhD.
Lecture 3: Query processing and optimization 28 Department of Information Technology
Query Decomposition
Example: This query is type incorrect
SELECT E#
www.ptit.edu.vn
FROM EMP
WHERE ENAME > 200;
For 2 reasons:
Attribute E# is not declared in the schema
the operation “>200” is incompatible with the type
string of ENAME.
queries.
It is possible to do so based on the representation of
the query as a graph, called a query graph or
connection graph
E.ENO=G.ENO G.PNO=J.PNO
www.ptit.edu.vn
G
G.ENO=G.ENO G.PNO=J.PNO
E J
(b) Joint graph
FROM E, G, J
WHERE E.ENO = G.ENO
AND PNAME = "CAD/CAM"
AND DUR > 36
AND TITLE = "Programmer”;
DUR 36
E.ENO=G.ENO
Query graph
5. p true p 6. p p true
7. p false p 8. p1 (p1 p2) p1
9. p false false 10.p1 (p1 p2) p1
FROM E
WHERE (NOT (TITLE = "Programmer")
AND (TITLE = "Programmer" OR TITLE = "Elect. Eng.")
AND NOT (TITLE = "Elect. Eng."))
OR ENAME = “J. Doe”;
SELECT TITLE
FROM E
WHERE ENAME = "J. Doe";
SELECT ENAME
FROM PROJ, ASG, EMP
WHERE ASG.ENO = EMP.ENO
AND ASG.PNO = PROJ.PNO
AND ENAME != "J. Doe"
AND PROJ.PNAME = "CAD/CAM"
AND (DUR = 12 OR DUR = 24) ;
Hoa Dinh Nguyen, PhD.
Lecture 3: Query processing and optimization 44 Department of Information Technology
www.ptit.edu.vn Query Decomposition
R⋈SS⋈R
2. Associativity of binary operators
R × (S × T) (R × S) × T
R ⋈ (S ⋈ T) (R ⋈ S) ⋈ T
3. Idempotence of unary operators
A’(A’’(R)) A’(R) , where A’, A’’ R and A’ A’’
p ( A ) ( p
1 1 2 ( A2 )
( R)) p1 ( A1 ) p2 ( A2 ) ( R)
Hoa Dinh Nguyen, PhD.
Lecture 3: Query processing and optimization 46 Department of Information Technology
Query Decomposition
4. Commuting selection with projection
A1 ,...,An ( p ( Ap ) ( R)) A1 ,...,An ( p ( Ap ) ( A1 ,...,An , Ap ( R)))
www.ptit.edu.vn
p ( A ) ( R S ) p ( A ) ( R) S
www.ptit.edu.vn
p p
p( A ) (R
i p ( Ai , Bk )
S ) p ( Ai ) ( R ) p ( Ai , Bk ) S
p ( A ) ( R T ) p ( A ) ( R) p ( A ) (T )
i i i
defined, we have
C ( R S ) A' ( R) B ' (S )
C ( R S ) A' ( R) B ' (S )
FROM EMP
WHERE ENO = “E5” ;
FROM EMP
Example:
1. EMP1 = 𝜎𝐸𝑁𝑂≤"𝐸4" ∏𝐸𝑁𝑂,𝐸𝑁𝐴𝑀𝐸 𝐸𝑀𝑃
www.ptit.edu.vn
WHERE ENO="E5“