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

28-Query Processing-30-09-2024

fvzvzsf

Uploaded by

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

28-Query Processing-30-09-2024

fvzvzsf

Uploaded by

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

Query Optimization

Dr. Anand Bihari


SJT Annex- 203N
[email protected]
Introduction
 Query: A query is a request for information from a database.
 Query Plans: A query plan (or query execution plan) is an
ordered set of steps used to access data in a SQL relational
database management system.
 Query Optimization: A single query can be executed through
different algorithms or re-written in different forms and
structures.
Hence, the question of query optimization comes into the
picture – Which of these forms or pathways is the most
optimal?
The query optimizer attempts to determine the most efficient
way to execute a given query by considering the possible
query plans.
Importance of Query
Optimization
The goal of query optimization is to reduce the system
resources required to fulfill a query, and ultimately provide
the user with the correct result set faster.
 First, it provides the user with faster results, which makes
the application seem faster to the user.
 Secondly, it allows the system to service more queries in
the same amount of time, because each request takes less
time than un-optimized queries.
 Thirdly, query optimization ultimately reduces the amount
of wear on the hardware (e.g. disk drives), and allows the
server to run more efficiently (e.g. lower power
consumption, less memory usage).
Way to Optimize the Query
Analyze and transform equivalent relational
expressions: Try to minimize the tuple and column
counts of the intermediate and final query processes.

Using different algorithms for each operation: These


underlying algorithms determine how tuples are accessed
from the data structures they are stored in, indexing,
hashing, data retrieval and hence influence the number of
disk and block accesses (discussed in query processing).
Translating SQL Queries into Relational
Algebra
SELECT Lname, Fname FROM EMPLOYEE
WHERE Salary > ( SELECT MAX (Salary)
FROM EMPLOYEE WHERE Dno=5 );
First Decomposition has been performed on
this query.
1. ( SELECT MAX (Salary) FROM EMPLOYEE
WHERE
Dno=5 )
2. SELECT Lname, Fname FROM EMPLOYEE
WHERE Salary > c
ℑ (σDno=5(EMPLOYEE))
MAX Salary

Π Lname,Fname (σSalary>c(EMPLOYEE))
Equivalence Rules
1. Conjunctive selection operations can be
deconstructed into a sequence of individual
selections.

2. Selection operations are commutative.

3. Only the last in a sequence of projection operations


is needed,the(others
( (can( Ebe omitted.
)) ))  ( E )
L1 L2 Ln L1

4. Selections can be combined with Cartesian products


and theta joins.
a) (E1 X E2) = E1  E2
b) 1(E1 2 E2) = E1 1 2 E2
Equivalence Rules
5. Theta-join operations (and natural joins) are
commutative.
E1  E2 = E2  E1

6. (a) Natural join operations are associative:


(E1 E2) E3 = E1 (E2 E3)

(b) Theta joins are associative in the following


manner:

(E1 1 E2) 2 3 E3 = E1 1 3 (E2 2 E3)

where 2 involves attributes from only E2 and E3.


Pictorial Depiction of Equivalence
Rules
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.

0E1  E2) = (0(E1))  E2

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


2 involves only the attributes of E2.
1 E1  E2) = (1(E1))  ( (E2))
Equivalence Rules
8. The projection operation distributes over the theta join
operation as follows:
(a) if  involves only attributes from L1  L2:
ÕL1 ÈL2 ( E1 q E2 ) = (Õ L1 ( E1 )) q (Õ L2 ( E2 ))

(b) Consider a join E1  E2.


 Let L1 and L2 be sets of attributes from E1 and E2, respectively.
 Let L3 be attributes of E1 that are involved in join condition , but
are not in L1  L2, and
 let L4 be attributes of E2 that are involved in join condition , but are
not in L1  L2.
Õ L ÈL ( E1
1 2 q E2 ) = Õ L ÈL ((Õ L ÈL ( E1 ))
1 2 1 3 q (Õ L ÈL ( E2 )))
2 4
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)
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
There are two methods of
query optimization
Cost based Optimization (Physical)
Heuristic Optimization (Logical)
Cost based Optimization (Physical)
 It is based on the cost of the query.

 The query can use different paths based on indexes, constraints,


sorting methods etc.

 This method mainly uses the statistics like record size, number of
records, number of records per block, number of blocks, table
size, whether whole table fits in a block, organization of tables,
uniqueness of column values, size of columns etc.
 Cost difference between evaluation plans for a query can be
enormous
 Steps in cost-based query optimization
Generate logically equivalent expressions using equivalence
rules
Annotate resultant expressions to get alternative query plans
Choose the cheapest plan based on estimated cost
 Estimation of plan cost based on:
Statistical information about relations. Examples:
number of tuples, number of distinct values for an attribute
Statistics estimation for intermediate results
to compute cost of complex expressions
 Cost formulae for algorithms, computed using statistics

You might also like