Query Processing and Optimization
Query Processing and Optimization
Query Processing and Optimization
Query Processing
and Optimization
Outline
Looping
• 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 syntax of Translator translates the
query and verifies attribute query into its internal
name and relation name form (relational algebra)
Database Catalog
Data Statistics about Data
Notation: σ
condition (Relation)
Operation: Selects tuples from a relation that satisfy a given condition.
Operators: =, <>, <, >, <=, >=, Λ (AND), V (OR)
Example Display the detail of students belongs to “CE” Branch. Answer σBranch=‘CE’ (Student)
Student Output
RollNo Name Branch SPI RollNo Name Branch SPI
101 Raju CE 8 101 Raju CE 8
102 Mitesh ME 9 104 Meet CE 9
103 Nilesh CI 9
104 Meet CE 9
Bottom to top
Execution
To evaluate such expression we need to
evaluate each operations one by one in
appropriate order.
σBalance<2500 (customer)
Two methods for evaluating an expression
carrying multiple operations are:
Materialization
(account)
Pipelining
4 records 4 records
Customer
CID ANO Name Balance
σANO<3 Λ Balance<2000 (Customer) Output
C01 1 Raj 3000
CID ANO Name Balance
C02 2 Meet 1000 OUTPUT
C02 2 Meet 1000
C03 3 Jay 2000
C04 4 Ram 4000 σANO<3 (σBalance<2000 (Customer))
Customer
CID ANO Name Balance
σANO<3 (σBalance<2000 (Customer)) Output
C01 1 Raj 3000
CID ANO Name Balance
C02 2 Meet 1000 OUTPUT
C02 2 Meet 1000
C03 3 Jay 2000
C04 4 Ram 4000 σBalance<2000 (σANO<3 (Customer))
Customer Customer
CID ANO Name Balance Name
ΠName (ΠANO, Name (Customer))
C01 1 Raj 3000 Raj
C02 2 Meet 1000 OUTPUT Meet
C03 3 Jay 2000 Jay
C04 4 Ram 4000 ΠName (Customer) Ram
Customer Account
CID ANO Name ANO Balance
σANO<3 (Customer Account)
Output
C01 1 Raj 1 3000 CID ANO Name Balance
C02 2 Meet 2 1000 OUTPUT C01 1 Raj 3000
C03 3 Jay 3 2000 C02 2 Meet 1000
C04 4 Ram 4 4000 (Customer) σANO<3 (Account)
σθ (E1 E2)) = E1 θ E2
Customer Account
CID ANO Name ANO Balance
(Account) σANO<3 (Customer) Output
C01 1 Raj 1 3000 CID ANO Name Balance
C02 2 Meet 2 1000 OUTPUT C01 1 Raj 3000
C03 3 Jay 3 2000 C02 2 Meet 1000
C04 4 Ram 4 4000 (Customer) σANO<3 (Account)
E1 σθ E2 = E2 σθ E1
When the selection condition θ1 involves only the attributes of E1 and θ2 involves only the attributes of E2.
σθ1Λθ2 (E1 θ E2) = (σθ1(E1) θ (σθ2 (E2)))