Query Processing and Optimization
Query Processing and Optimization
(DBMS)
GTU # 3130703
Unit-5
Query
Processing
and
Optimization
Prof. Firoz A Sherasiya
Computer Engineering
Department
Darshan Institute of Engineering & Technology, Rajkot
firoz.sherasiya@darshan.ac.in
9879879861
Looping
Outline
• Steps in query processing
• Measures of query cost
• Selection operation
• Evaluation of expressions
• Query optimization
• Transformation of relational
expressions
• Sorting and join
Steps in query
processing
Section – 1
Steps in Query Processing
Parser checks the Translator
syntax of query and translates the
verifies attribute query into its
name and relation internal form
name Parser (relational algebra)
Relational
Quer and
algebra
y translat
expression
or
Choose best execution
plan Optimiz
Execute the query-
er
evaluation plan and
returns output
Evaluati
Query Execution
on
output plan
engine
Database
Data Catalog
Statistics about
#3130703 (DBMS) Data
Unit 5 – Query Processing
Prof. Firoz A Sherasiya 4
Measures of query cost
Section – 2
Measures of Query Cost
Cost is generally measured as the total time required to execute a
statement/query.
Factors contribute to time cost
Disk accesses (time to process a data request and retrieve the required data from
the storage device)
CPU time to execute a query
Network communication cost
Disk access is the predominant (major) cost, since disk access is
slow as compared to in-memory operation.
Cost to write a block is greater than cost to read a block because
data is read back after being written to ensure that the write was
successful.
Bottom to top
(customer) )
Execution
To evaluate such expression we
need to evaluate each operations σBalance<25 (custom
one by one in appropriate order. er)
00
Two methods for evaluating an
expression carrying multiple
(accou
operations are: nt)
Materialization
Pipelining
4 4
records records
Custo
mer
CI AN Na Balan
D O me ce σANO<3 Λ Balance<2000 Output
C0 1 Raj 3000 (Customer) CI AN Na Balan
OUTP
1 D O me ce
UT
C0 2 Meet 1000 C0 2 Meet 1000
2
σANO<3 (σBalance<2000 2
C0 3 Jay 2000
(Customer))
3
C0
4
4 Ram 4000 σθ1Λθ2 (E) = σθ1 (σθ2
(E))
Custo
mer
CI AN
D O
Na
me
Balan
ce σANO<3 (σBalance<2000 Output
C0 1 Raj 3000 (Customer)) CI AN Na Balan
OUTP
1 D O me ce
UT
C0 2 Meet 1000 C0 2 Meet 1000
2
σBalance<2000 (σANO<3 2
C0 3 Jay 2000
(Customer))
3
C0
4
4 Ram σθ1 (σθ2 (E))
4000 = σθ2 (σθ1
(E))
(Customer)) Raj
C0 1 Raj 3000 OUTP
1 Meet
UT
C0 2 Meet 1000 Jay
2
ΠName (Customer) Ram
C0 3 Jay 2000
3
C0
4
4 Ram ΠL1 (ΠL2 (…(Π Ln (E))…))
4000 = ΠL1
(E)
Account) CI AN Na Balan
C0 1 Raj 1 3000 OUTP D O me ce
1 UT
2 1000 C0 1 Raj 3000
C0 2 Meet 1
3 2000
2
(Customer) σANO<3 C0 2 Meet 1000
C0 3 Jay 4 4000 (Account) 2
3
C0
4
4 Ram σθ (E1 E2)) = E1 θE2
σθ1 (E1 θ2 E2)) = E1
θ1Λ θ2 E2
#3130703 (DBMS) Unit 5 – Query Processing
Prof. Firoz A Sherasiya 24
Transformation of relational expressions
Theta operations are commutative.
Example:
Custo Accoun
mer tAN Balan
CI AN
D O
Na
me O ce (Account) σANO<3 Output
(Customer) CI AN Na Balan
C0 1 Raj 1 3000 OUTP D O me ce
1 UT
2 1000 C0 1 Raj 3000
C0 2 Meet 1
3 2000
2
(Customer) σANO<3 C0 2 Meet 1000
C0 3 Jay 4 4000 (Account) 2
3
C0
4
4 Ram E1 σθ E2 = E2 σθ E1
σ θ1Λθ2
only the attributes of E2.
( θ1(E1)= θ σ
When the selection condition θ1 involves only the attributes of E1 and θ2 involves
(E1 θ E2)
(σθ2 (E2)))
Thank
You