Unit 1 (Query Optimization)
Unit 1 (Query Optimization)
Unit 1 (QueryOptimization) 1
Query optimization
One aspect of optimization occurs at the relational-algebra level,
where the system attempts to find an expression that is
equivalent to the given expression, but more efficient to
execute.
Another aspect is selecting a detailed strategy for processing
the query, such as choosing the algorithm to use for executing
an operation, choosing the specific indices to use, and so on.
Unit 1 (QueryOptimization) 2
Query optimization
The difference in cost (in terms of evaluation time) between a
good strategy and a bad strategy is often substantial, and may
be several orders of magnitude.
Hence, it is worthwhile for the system to spend a substantial
amount of time on the selection of a good strategy for
processing a query, even if the query is executed only once.
Unit 1 (QueryOptimization) 3
Query optimization
Example: Find the names of all instructors in the Music
department together with the course title of all the courses that the
instructors teach.
Unit 1 (QueryOptimization) 4
Query optimization
An evaluation plan defines exactly what algorithm should be used
for each operation, and how the execution of the operations
should be coordinated.
Given a relational-algebra expression, it is the job of the query
optimizer to come up with a query-evaluation plan that computes
the same result as the given expression, and is the least-costly
way of generating the result.
To find the least-costly query-evaluation plan, the optimizer
needs to generate alternative plans that produce the same result
as the given expression, and to choose the least-costly one.
Unit 1 (QueryOptimization) 5
Query optimization
Generation of query-evaluation plans involves three steps:
1. Generating expressions that are logically equivalent to the given
expression,
2. Annotating the resultant expressions in alternative ways to
generate alternative query-evaluation plans, and
3. Estimating the cost of each evaluation plan, and choosing the
one whose estimated cost is the least.
Steps (1), (2), and (3) are interleaved in the query optimizer—
some expressions are generated, and annotated to generate
evaluation plans, then further expressions are generated and
annotated, and so on.
Unit 1 (QueryOptimization) 6
Query optimization
As evaluation plans are generated, their costs are estimated by
using statistical information about the relations, such as relation
sizes and index depths.
To implement the first step, the query optimizer must generate
expressions equivalent to a given expression.
It does so by means of equivalence rules that specify how to
transform an expression into a logically equivalent one.
Unit 1 (QueryOptimization) 7
Equivalence Rules
An equivalence rule says that expressions of two forms are
equivalent.
We can replace an expression of the first form by an expression of
the second form, or vice-Versa.
The optimizer uses equivalence rules to transform expressions into
other logically equivalent expressions.
There are number of general equivalence rules on relational-
algebra expressions.
Unit 1 (QueryOptimization) 8
Equivalence Rules
We use , 1, 2, and so on to denote predicates, L1, L2, L3, and so
on to denote lists of attributes, and E, E1, E2, and so on to denote
relational-algebra expressions.
A relation name r is simply a special case of a relational-algebra
expression, and can be used wherever E appears.
Unit 1 (QueryOptimization) 9
Equivalence Rules
1. Conjunctive selection operations can be deconstructed into a
sequence of individual selections. This transformation is referred to
as a cascade of .
Unit 1 (QueryOptimization) 10
Equivalence Rules
3. Only the final operations in a sequence of projection operations are
needed; the others can be omitted. This transformation can also be
referred to as a cascade of ∏.
Unit 1 (QueryOptimization) 11
Equivalence Rules
5. Theta-join operations are commutative.
Unit 1 (QueryOptimization) 12
Pictorial Depiction Equivalence Rules
Unit 1 (QueryOptimization) 13
Equivalence Rules
7. The selection operation distributes over the theta join
operation under the following two conditions:
Unit 1 (QueryOptimization) 14
Equivalence Rules
9. The set operations union and intersection are
commutative
E1 E2 = E2 E1
E1 E2 = E2 E1
(set difference is not commutative).
Unit 1 (QueryOptimization) 15
Equivalence Rules
11.The selection operation distributes over , and –.
(E1 – E2) = (E1) – (E2)
and similarly for and in place of –
Also: (E1 – E2) = (E1) – E2
and similarly for in place of –, but
not for
Unit 1 (QueryOptimization) 16
Examples of Transformation
instructor(ID, name, dept name, salary)
teaches(ID, course id, sec id, semester, year)
course(course id, title, dept name, credits)
Is transformed to
Unit 1 (QueryOptimization) 18
Examples of Transformation
Unit 1 (QueryOptimization) 19
Examples of Transformation
Unit 1 (QueryOptimization) 20
Thank You !
Unit 1 (QueryOptimization)
21