Query Processing and Optimization
Query Processing and Optimization
Query Processing and Optimization
Optimization
Introduction
• In this chapter we shall discuss the techniques
used by a DBMS to process, optimize and execute
high-level queries.
• The techniques used to split complex queries into
multiple simple operations and methods of
implementing these low-level operations.
• The query optimization techniques are used to
chose an efficient execution plan that will
minimize the runtime as well as many other types
of resources such as number of disk I/O, CPU
time and so on.
Query Processing
• Query Processing is a procedure of transforming a high-level
query (such as SQL) into a correct and efficient execution plan
expressed in low-level language.
• When a database system receives a query for update or
retrieval of information, it goes through a series of
compilation steps, called execution plan.
• Query processing goes through various phases:
• first phase is called syntax checking phase, the system parses
the query and checks that it follows the syntax rules or not.
• It then matches the objects in the query syntax with the view
tables and columns listed in the system table.
• In second phase the SQL query is translated in to an algebraic
expression using various rules.
• So that the process of transforming a high-level SQL query into a
relational algebraic form is called Query Decomposition.
• The relational algebraic expression now passes to the query
optimizer.
• In third phase optimization is performed by substituting equivalent
expression depends on the factors such that the existence of
certain database structures, whether or not a given file is stored,
the presence of different indexes & so on.
• Query optimization module work in tandem with the join manager
module to improve the order in which joins are performed.
• At this stage the cost model and several other
estimation formulas are used to rewrite the query.
• The modified query is written to utilize system
resources so as to bring the optimal performance.
• The query optimizer then generates an action plan also
called a execution plan.
• This action plans are converted into a query codes that
are finally executed by a run time database processor.
• The run time database processor estimate the cost of
each action plan and chose the optimal one for the
execution.
Query Analyzer
• The syntax analyzer takes the query from the
users, parses it into tokens and analyses the
tokens and their order to make sure they
follow the rules of the language grammar.
• Is an error is found in the query submitted by
the user, it is rejected and an error code
together with an explanation of why the query
was rejected is return to the user.
Query Decomposition
• In query decomposition the query processing aims are to transfer
the high-level query into a relational algebra query and to check
whether that query is syntactically and semantically correct.
• Thus the query decomposition is start with a high-level query and
transform into query graph of low-level operations, which satisfy
the query.
• The SQL query is decomposed into query blocks (low-level
operations), which form the basic unit.
• Hence nested queries within a query are identified as separate
query blocks.
• The query decomposer goes through five stages of processing for
decomposition into low-level operation and translation into
algebraic expressions.
Query Analysis
• During the query analysis phase, the query is
syntactically analyzed using the programming
language compiler (parser).
• A syntactically legal query is then validated,
using the system catalog, to ensure that all
data objects (relations and attributes) referred
to by the query are defined in the database.
• The type specification of the query qualifiers
and result is also checked at this stage.
• Example:
SELECT emp_nm FROM EMPLOYEE WHERE
emp_desg>100
• Commutativity of σ :-
• σ C1 (σ C2 (R)) = σ C2 (σ C1 (R))
Cascade of Л :-
• Л List1 (Л List2 (…(Л List n (R))…)) = Л List1 (R)
• Commuting σ with Л :-
• Л A1,A2,A3…An (σ C (R) ) = σ C (Л A1,A2,A3…An (R))
General Transformation Rules
Commutativity of ⋈ AND x :-
R⋈cS=S⋈cR
RxS=SxR
• Commuting σ with ⋈ or x :-
If all attributes in selection condition c involved only attributes of one of the relation
schemas (R).
σ c (R ⋈ S) = (σ c (R) ) ⋈ S
Alternatively, selection condition c can be written as (c1 AND c2) where condition c1
involves only attributes of R and condition c2 involves only attributes of S then :
σ c (R ⋈ S) = (σ c1 (R) ) ⋈ (σ c2 (S) )
• Commuting Л with ⋈ or x :-
The projection list L = {A1,A2,..An,B1,B2,…Bm}.
A1…An attributes of R and B1…Bm attributes of S.
Join condition C involves only attributes in L then :
ЛL ( R ⋈ c S ) = ( ЛA1,…An (R) ) ⋈c ( ЛB1,…Bm(S) )
General Transformation Rules
• Commutative of SET Operation :-
R⋃S=S⋃R
R⋂S=S⋂R
Minus (R-S) is not commutative.
• Associatively of ⋈, x, ⋂, and ⋃ :-
If ∅ stands for any one of these operation throughout the expression then :
(R ∅ S) ∅ T = R ∅ (S ∅ T)
• Commutativity of σ with SET Operation :-
If ∅ stands for any one of three operations (⋃,⋂,and-) then :
σ c (R ∅ S) = (σ c (R)) ⋃ (σ c (S))
Л c (R ∅ S) = (Л c (R)) ⋃ (Лc (S))
• The Л operation comute with ⋃ :-
Л L (R ⋃ S) = (Л L(R)) ⋃ (Л L(S))
• Converting a (σ,x) sequence with ⋃
(σ c (R x S)) = (R ⋈ c S)
THANK YOU