Query Optimization
Query Optimization
Query Optimization
Heuristic Optimization
Lecturer: Dr Hui Ma
Engineering and Computer Science
Outline
Query Processing in DBMS
Query optimization techniques
Heuristic query optimization
Translating SQL into relational algebra
Reordering query operations
Transformation rules for relational algebra operations
SQL query:
SELECT StudId, Lname
FROM Student s, Grades g, Course c
WHERE s.StudId=g.StudId AND g.CrsId= c.CrsId
AND Grade = ‘A+’ AND Dept = ‘Comp’ AND Major = ‘Math’;
• Relational Algebra:
StudId, LName (σ s.StudId=g.StudId ˄ g.CrsId=c.CrsId ˄ Grade=‘A+’ ˄ Major = ‘Math’ ˄ Dept =
‘Comp’ (Course × (Student × Grades)))
R1 C R2 = R2 CR1; R1 x R2 = R2 x R1
6. Commuting with (or x ): If all the attributes in the selection
condition c involve only the attributes of one of the relations
being joined—say, R1—the two operations can be commuted
as follows:
(R R2 ) = c (R1) R2
c 1
Alternatively, if the selection condition c can be written as (c1
and c2), where condition c1 involves only the attributes of R1
and condition c2 involves only the attributes of R2, the
operations commute as follows:
(R R2) = c1(R1) c2 (R2)
c 1
SWEN 304 Lect10: Heuristic Query Optimization 16
Using Heuristics in Query Optimization (3)
General Transformation Rules for Relational Algebra
Operations (contd.):
7. Commuting with (or x): Suppose that the projection list
is AL = {A1, ..., An, B1, ..., Bm}, where A1, ..., An are
attributes of R1 and B1, ..., Bm are attributes of R2. If the join
condition c involves only attributes in AL, the two operations
can be commuted as follows:
AL (R1 C R2) = (A1, ..., An (R1)) C ( B1, ..., Bm (R2))
If the join condition C contains additional attributes not in
AL, these must be added to the projection list, and a final
operation is needed.
AL(R1 C R2) = AL(AL1(R1) (R2)),
C AL2
General Transformation Rules for Relational Algebra
Operations (contd.):
8. Commutativity of set operations: The set operations
and ∩ are commutative but “–” is not.
9. Associativity of , x, , and ∩ : These four operations
are individually associative; that is, if stands for any
one of these four operations (throughout the
expression), we have
(R1 R2) R3 = R1 (R2 R3)
General Transformation Rules for Relational Algebra
Operations (contd.):
11. The operation commutes with .
AL (R1 R2) = (AL (R1)) (AL (R2))
Query tree