CS186 Final Review Query Optimization
Overview Query Relational Algebra Tree What is a query plan? ? Implementation choices at each operator Order of operators What is a query plan? Tree of R.A. ops (and some others) with choice of algorithm for each op. Reserves Sailors sid=sid bid=100 rating > 5 sname ?
Join Joins (Chapter 14.4) (Page-oriented) Nested loops join Blocked nested loops: simple, exploits buffer pages Indexed nested loops: best if 1 relation small and one indexed Sort/Merge Join sort cost + merge cost (M+N M*N) Hash Join partition cost (2(M+N)) + match cost (M+N)
Query Tree Rating from 1 to 10 100 boats |S| = 500, |R|=1000 SELECT S.sname FROM Reserves R, Sailors S WHERE R.sid=S.sid AND R.bid=100 AND S.rating>5 Rating from 1 to 10 100 boats |S| = 500, |R|=1000 Sailors Reserves sid=sid bid=100 rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) Reserves Sailors sid=sid bid=100 sname (Page-Oriented Nested loops) (On-the-fly) rating>5 (Scan & Write to temp T2)
Query Tree Left-deep plans Relational Algebra Equivalences Fully pipelined plans: Intermediate results not written to temporary files. All left-deep trees are fully pipelined? (SM join?) Relational Algebra Equivalences Chapter 15.3 Cost estimation Reduction Factors (also called “selectivity”) values are uniformly distributed histogram
Choice of Algorithm for Operator Rating from 1 to 10, 100 boats |S| = 500, |R|=1000 Sailors Reserves sid=sid rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) bid=100 (Scan & Write to temp T2) Reserves Sailors sid=sid bid=100 sname (On-the-fly) rating > 5 (Scan; write to temp T1) temp T2) (Sort-Merge Join) 4250 IOs 3560 IOs
Choice of Algorithm for Operator What affect choices? # Buffer Pages Statistic of the relation Index? Clustered Unclustered: access each tuple 1 I/O
Conclusion What you should know? Given a SQL query, how to give the best (good) query plan? Best plan for accessing each relation Enumeration to get trees (left-deep join) Select algorithm for each operator Cost estimation # of Buffer? Index