20 likes | 100 Vues
In-Class Query Optimization Exercise. p sname. p sname. Reserves. Sailors. Boats. Each tuple 40 bytes long 100 tuples per page, M = 1000 pages total. Each tuple 50 bytes long 80 tuples per page, N = 500 pages total. Each tuple 45 bytes long 90 tuples per page,
E N D
In-Class Query Optimization Exercise psname p sname Reserves Sailors Boats Each tuple 40 bytes long 100 tuples per page, M = 1000 pages total. Each tuple 50 bytes long 80 tuples per page, N = 500 pages total. Each tuple 45 bytes long 90 tuples per page, P = 500 pages total. 1) How many left-deep query trees can you find for R S B? Assume no indexes. There are 4 left-deep trees: B B S S B. s rating = 5 R S S R B R R B sid = sid File Scan File Scan Reserves Salesperson A. p sname C. Block Nested Loop Join Sort-merge Join sid = sid sid = sid s rating =5 File Scan s rating=5 File Scan Use Clustered B-Tree Index Use Clustered B-Tree Index Reserves Reserves Sailors Sailors
psname p sname B. s rating = 5 sid = sid File Scan File Scan Reserves Salesperson 2) What is the lowest cost query plan for the query: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND S.rating = “5”? (You have 10 buffers of memory) A. p sname C. Block Nested Loop Join Sort-merge Join sid = sid sid = sid s rating =5 File Scan s rating=5 File Scan Use Clustered B-Tree Index Use Clustered B-Tree Index Reserves Reserves Sailors Sailors • Solution: • 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