Lecture 18
Lecture 18
Lecture 18
CSE 444
Lecture 18: Query Processing Overview
Logical
Select Logical Plan
Query plan
optimization
Select Physical Plan
Physical
plan
Query Execution
Disk 4
Example Database Schema
Supplier(sno,sname,scity,sstate)
Part(pno,pname,psize,pcolor)
Supply(sno,pno,price)
Rewritten query:
SELECT S.sname
FROM Supplier S, Supplies U
WHERE S.scity='Seattle' AND S.sstate='WA’
AND S.sno = U.sno
AND U.pno = 2;
CSE 444 - Spring 2009 8
Continue with Query Evaluation
• Step 3: Query optimization
– Find an efficient query plan for executing the query
– We will spend a whole lecture on this topic
• A query plan is
– Logical query plan: an extended relational algebra tree
– Physical query plan: with additional annotations at each
node
• Access method to use for each relation
• Implementation to use for each relational operator
sno = sno
Suppliers Supplies
π fields
σ selection condition
SELECT-PROJECT-JOIN
join condition
Query
join condition …
R S
CSE 444 - Spring 2009 13
Typical Plan For Block (2/2)
havingcondition
γ fields, sum/count/min/max(fields)
π fields
σ selection condition
join condition
… …
CSE 444 - Spring 2009 14
How about Subqueries?
SELECT Q.name
FROM Person Q
WHERE Q.age > 25
and not exists
SELECT *
FROM Purchase P
WHERE P.buyer = Q.name
and P.price > 100
SELECT *
FROM Purchase P σ
WHERE P.buyer = Q.name Price > 100
(Nested loop)
sno = sno
Suppliers Supplies
(File scan) (File scan)
CSE 444 - Spring 2009 18
Final Step in Query Processing
• Step 4: Query execution
– How to synchronize operators?
– How to pass data between operators?
• Approach:
– Iterator interface with
– Pipelined execution or
– Intermediate result materialization
(Nested loop)
sno = sno
Suppliers Supplies
(File scan) (File scan)
CSE 444 - Spring 2009 22
Intermediate Tuple
Materialization
• Writes the results of an operator to an
intermediate table on disk
(Sort-merge join)
sno = sno
Suppliers Supplies
(File scan) (File scan)
CSE 444 - Spring 2009 24
Next Time
• Algorithms for physical op. implementations