2 Algorithms For Query Processing Optimization
2 Algorithms For Query Processing Optimization
and Optimization
Chapter 2
Heuristic Rules
Rules for ordering the operations in query
optimization.
Systematical estimation
It estimates cost of different execution
strategies and chooses the execution plan with
lowest execution cost
QUERY DATA STRUCTURE
• Before optimizing the query it is represented in an internal or
intermediate form.
Two internal representations of a query:
It is created using two data structures
• DDL compiler
• DML compiler
• Runtime
database
processor
• System catalog
• SQL query:
Q2: SELECT P.NUMBER,P.DNUM,E.LNAME,
E.ADDRESS, E.BDATE
FROM PROJECT AS P,DEPARTMENT
AS D, EMPLOYEE AS E
WHERE P.DNUM=D.DNUMBER AND
D.MGRSSN=E.SSN AND
P.PLOCATION=‘STAFFORD’;
Using Heuristics in Query Optimization (4)
Using Heuristics in Query Optimization (5)
Using Heuristics in Query Optimization (6)
• Heuristic Optimization of Query Trees:
• The same query could correspond to many different relational
algebra expressions — and hence many different query trees.
• The task of heuristic optimization of query trees is to find a final
query tree that is efficient to execute.
• Example:
Q: SELECT LNAME
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE PNAME = ‘AQUARIUS’ AND
PNMUBER=PNO AND
ESSN=SSN AND BDATE > ‘1957-
12-31’;
Using Heuristics in Query Optimization (7)
Using Heuristics in Query Optimization (8)
Using Heuristics in Query Optimization (9)
• General Transformation Rules for Relational Algebra Operations:
1. Cascade of s: A conjunctive selection condition can be broken up into a
cascade (sequence) of individual s operations:
• s c1 AND c2 AND ... AND cn(R) = sc1 (sc2 (...(scn(R))...) )
2. Commutativity of s: The s operation is commutative:
• sc1 (sc2(R)) = sc2 (sc1(R))
3. Cascade of p: In a cascade (sequence) of p operations, all but the last one
can be ignored:
• pList1 (pList2 (...(pListn(R))...) ) = pList1(R)
4. Commuting s with p: If the selection condition c involves only the
attributes A1, ..., An in the projection list, the two operations can be
commuted:
• pA1, A2, ..., An (sc (R)) = sc (pA1, A2, ..., An (R))
Using Heuristics in Query Optimization (10)
• General Transformation Rules for Relational Algebra Operations (contd.):
5. Commutativity of ( and x ): The operation is commutative as is the x
operation:
• R C S = S C R; R x S = S x R
6. Commuting s with (or x ): If all the attributes in the selection condition c
involve only the attributes of one of the relations being joined—say, R—
the two operations can be commuted as follows:
• sc ( R S ) = (sc (R)) S
• Alternatively, if the selection condition c can be written as (c1 and c2),
where condition c1 involves only the attributes of R and condition c2
involves only the attributes of S, the operations commute as follows:
• sc ( R S ) = (sc1 (R)) (sc2 (S))
Using Heuristics in Query Optimization (11)
• General Transformation Rules for Relational Algebra
Operations (contd.):
7. Commuting p with (or x): Suppose that the projection list is
L = {A1, ..., An, B1, ..., Bm}, where A1, ..., An are attributes
of R and B1, ..., Bm are attributes of S. If the join condition c
involves only attributes in L, the two operations can be
commuted as follows:
• pL ( R C S ) = (pA1, ..., An (R)) C (p B1, ..., Bm (S))
• If the join condition C contains additional attributes not in L,
these must be added to the projection list, and a final p
operation is needed.
Using Heuristics in Query Optimization (12)
• 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 q stands for any one of
these four operations (throughout the expression), we have
• (RqS)qT = Rq(SqT)
10. Commuting s with set operations: The s operation commutes
with υ , ∩ , and –. If q stands for any one of these three
operations, we have
• sc ( R q S ) = (sc (R)) q (sc (S))
Using Heuristics in Query Optimization (13)
• General Transformation Rules for Relational Algebra
Operations (contd.):
• The p operation commutes with υ.
pL ( R υ S ) = (pL (R)) υ (pL (S))
• Other transformations
Using Heuristics in Query Optimization (14)
• Outline of a Heuristic Algebraic Optimization Algorithm:
1. Using rule 1, break up any select operations with conjunctive conditions into a
cascade of select operations.
2. Using rules 2, 4, 6, and 10 concerning the commutativity of select with other
operations, move each select operation as far down the query tree as is permitted
by the attributes involved in the select condition.
3. Using rule 9 concerning associativity of binary operations, rearrange the leaf nodes
of the tree so that the leaf node relations with the most restrictive select operations
are executed first in the query tree representation.
4. Using Rule 12, combine a Cartesian product operation with a subsequent select
operation in the tree into a join operation.
5. Using rules 3, 4, 7, and 11 concerning the cascading of project and the commuting
of project with other operations, break down and move lists of projection attributes
down the tree as far as possible by creating new project operations as needed.
6. Identify subtrees that represent groups of operations that can be executed by a
single algorithm.
Using Heuristics in Query Optimization (15)
• Summary of Heuristics for Algebraic Optimization:
1. The main heuristic is to apply first the operations that reduce
the size of intermediate results.
2. Perform select operations as early as possible to reduce the
number of tuples and perform project operations as early as
possible to reduce the number of attributes. (This is done by
moving select and project operations as far down the tree as
possible.)
3. The select and join operations that are most restrictive should
be executed before other similar operations. (This is done by
reordering the leaf nodes of the tree among themselves and
adjusting the rest of the tree appropriately.)
Using Heuristics in Query Optimization (16)
• Query Execution Plans
• An execution plan for a relational algebra query consists of a
combination of the relational algebra query tree and information
about the access methods to be used for each relation as well as
the methods to be used in computing the relational operators
stored in the tree.
• Materialized evaluation: the result of an operation is stored as a
temporary relation.
• Pipelined evaluation: as the result of an operator is produced, it
is forwarded to the next operator in sequence.
8. Using Selectivity and Cost Estimates in
Query Optimization (1)
• Cost-based query optimization:
• Estimate and compare the costs of executing a query using
different execution strategies and choose the strategy with
the lowest cost estimate.
• (Compare to heuristic query optimization)
• Issues
• Cost function
• Number of execution strategies to be considered
Using Selectivity and Cost Estimates in Query
Optimization (2)
• Cost Components for Query Execution
1. Access cost to secondary storage
2. Storage cost
3. Computation cost
4. Memory usage cost
5. Communication cost