0% found this document useful (0 votes)
3 views

Unit 1 (Query Optimization)

Query optimization is the process of selecting the most efficient query-evaluation plan for complex queries, minimizing evaluation costs. It involves generating equivalent expressions, annotating them to create alternative plans, and estimating their costs to choose the least costly option. Equivalence rules play a crucial role in transforming expressions to improve execution efficiency.

Uploaded by

ptav341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 1 (Query Optimization)

Query optimization is the process of selecting the most efficient query-evaluation plan for complex queries, minimizing evaluation costs. It involves generating equivalent expressions, annotating them to create alternative plans, and estimating their costs to choose the least costly option. Equivalence rules play a crucial role in transforming expressions to improve execution efficiency.

Uploaded by

ptav341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Query optimization

 It is the process of selecting the most efficient query-evaluation


plan from among the many strategies if the query is complex.
 We do not expect users to write their queries so that they can
be processed efficiently.
 Rather, we expect the system to construct a query-evaluation
plan that minimizes the cost of query evaluation.

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 .

2. Selection operations are commutative.

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 ∏.

4.Selections can be combined with Cartesian products and


theta joins.

Unit 1 (QueryOptimization) 11
Equivalence Rules
5. Theta-join operations are commutative.

6. a. Natural-join operations are associative.

b. Theta joins are associative in the following manner:

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:

(a) When all the attributes in 0 involve only the


attributes of one of the expressions (E1) being joined.

(b) When 1 involves only the attributes of E1 and 2


involves only the attributes of E2.

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).

10.Set union and intersection are associative.

(E1  E2)  E3 = E1  (E2  E3)


(E1  E2)  E3 = E1  (E2  E3)

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 

12. The projection operation distributes over union

L(E1  E2) = (L(E1))  (L(E2))

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

Note: Performing the selection as early as possible reduces the


size of the relation to be joined.
Unit 1 (QueryOptimization) 17
Examples of Transformation
If we modify our original query to restrict attention to instructors
who have taught a course in 2009. The new relational-algebra
query is:

We cannot apply the selection predicate directly to the instructor


relation, since the predicate involves attributes of both the
instructor and teaches relations. However, we can first apply rule
6.a (associativity of natural join) to transform the join

Unit 1 (QueryOptimization) 18
Examples of Transformation

Then, using rule 7.a, we can rewrite our query as:

Unit 1 (QueryOptimization) 19
Examples of Transformation

Unit 1 (QueryOptimization) 20
Thank You !

Unit 1 (QueryOptimization)
21

You might also like