In-Class Query Optimization Exercise Reserves Sailors Boats

advertisement
In-Class Query Optimization Exercise
Reserves
sid
22
58
Sailors
bid
101
103
day
10/10/96
11/12/96
sid
22
31
58
Each tuple 40 bytes long
100 tuples per page,
M = 1000 pages total.
Boats
sname
dustin
lubber
rusty
rating
7
8
10
bid bname
color
101 Interlake Blue
102 Interlake Red
103 Clipper
Green
104 Marine
Red
Each tuple 45 bytes long
90 tuples per page,
P = 500 pages total.
age
45.0
55.5
35.0
Each tuple 50 bytes long
80 tuples per page,
N = 500 pages total.
1) How many left-deep query trees can you find for R
indexes.
S
B? Assume no
There are 4 left-deep trees:
B
B
S
S
p sname
B.
R
S
S
R
B
R
R
s
B
rating = 5
sid = sid
File Scan
File Scan
Reserves
Salesperson
A.
p sname
p sname
C.
Block Nested Loop Join
Sort-merge Join
sid = sid
s
rating=5
Use Clustered
B-Tree Index
Sailors
sid = sid
s
rating =5
File Scan
File
Scan
Use Clustered
B-Tree Index
Reserves
Reserves
Sailors
p sname
B.
2) What is the lowest cost query plan for the query:
s
SELECT S.sname FROM Sailors S, Reserves R
WHERE S.sid = R.sid AND S.rating = “5”?
(You have 10 buffers of memory)
rating = 5
sid = sid
File Scan
File Scan
Reserves
Salesperson
A.
p sname
p sname
C.
Block Nested Loop Join
Sort-merge Join
sid = sid
sid = sid
s
rating=5
Use Clustered
B-Tree Index
s
rating =5
File Scan
File
Scan
Use Clustered
B-Tree Index
Reserves
Sailors
Reserves
Sailors
Solution:
A.
Steps:
•
B-tree Index for rating =5, cost 2-3 IOs, + access all matching tuples, which are 10% of Sailor’s 500
pages, which is 50.
•
Sort-merge join, cost about 3*(M+N) = 3*(50 + 1000) = 3150
•
Project sname done on the fly, no additional IO cost
B. Steps
•
No join algorithm specified, lets use sort-merge, for 3*(500 + 1000) = 4500
•
Selection, projection done on the fly, no additional IO
C. Steps
•
Select rating = 5, cost same as in plan A, 53 IOs
•
Blocked nested loops join is M + ceiling(M/B-1)*N = 50 + ceiling(50/9)*1000 = 50 + 6*1000 = 6050
•
Projection done on the fly, no additional IO cost
Download