Beruflich Dokumente
Kultur Dokumente
CPSC 421, 2009 1
Steps
• Query op5miza5on steps:
– Translate SQL Query to Rela5onal Algebra Query
– Create a query tree
– Create leY‐deep alterna5ve trees
– Create query plans for our trees
– Es5mate costs of plans
– Pick best one
CPSC 421, 2009 2
1
12/13/09
Step 1
• Translate SQL Query to Rela5onal Algebra Query
CPSC 421, 2009 3
Step 2
• Create a query tree πsid,sname,age
σage<30
σcolor=“Red”
σbid=bid
×
Boats σsid=sid
Note our query only involves joins
(as opposed to plain old cross-
products) … !
×
Lets draw the trees with joins! Reserves Sailors
CPSC 421, 2009 4
2
12/13/09
Step 3
• Create leY‐deep alterna5ve trees
– We replace cross products if they are really joins
– How?
πsid,sname,age
• Using RA equivalences!
σage<30
– This is a leY‐deep plan with joins
σcolor=“Red”
⋈bid=bid
⋈sid=sid Boats
Reserves Sailors
CPSC 421, 2009 5
CPSC 421, 2009 6
3
12/13/09
Boats Reserves
CPSC 421, 2009 7
πsid,sname,age (On-the-fly)
Sort(M) + Sort(R) + M + N =
Sailors
0 + 4,000 + 160 + 1,000 =
5,160
⋈bid=bid (Sort Merge)
Boats sorted on bid, Reserves isn’t
Assume buffers B = 50 * We are assuming merge (in Sort
Cost of sorting reserves: Boats Reserves Merge) takes M + N … this may
• 2*1,000 + 2*1*1,000 = 4,000 I/Os not always be the case. Why?
CPSC 421, 2009 8
4
12/13/09
CPSC 421, 2009 9
πsid,sname,age (On-the-fly)
5
12/13/09
Sailors
M + (M/(B-1))*N = 160+4*1,000 = 4,160 ⋈bid=bid (Block NL)
(this is the cost of reading, not writing)
Boats Reserves
CPSC 421, 2009 11
Reserves Sailors
CPSC 421, 2009 12
6
12/13/09
• No5ce that by adding an index on color to Boats …
– We reduced our best query 5me in half!
– This is one reason we talk about query op5miza5on
– It drives physical database design
– Improving performance (by adding indexes, e.g.) should be
driving on how query op5miza5on works!
CPSC 421, 2009 14
7