



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























1
Slide Content Courtesy of R. Ramakrishnan, J. Gehrke, and J. Hellerstein
(^) Each tuple is 40 bytes long, 100 tuples per page, 1000 pages.
(^) Each tuple is 50 bytes long, 80 tuples per page, 500 pages. Sailors ( sid : integer, sname : string, rating : integer, age : real) Reserves ( sid : integer, bid : integer, day : dates, rname : string)
(^) Selection conditions are first converted to conjunctive normal form (CNF): ( day<8/9/94 OR bid=5 OR sid=3 ) AND ( rname=‘Paul’ OR sid=3 ) (^) Query optimization interleaves FROM and WHERE into a plan tree. (^) GROUP BY, HAVING, DISTINCT and ORDER BY are applied at the end, pretty much in that order. SELECT {DISTINCT} FROM
{WHERE <list of "Boolean Factors" (predicates in CNF)>} {GROUP BY
{HAVING
}} {ORDER BY
};
relation name, file name, file structure (e.g., heap file) attribute name and type of each attribute index name of each index on the relation integrity constraints…
index name and structure (B+ tree) search key attributes
view name and definition
(^) The algebra expression partially specifies how to evaluate the query: Compute the natural join of Reserves and Sailors Perform the selections Project the sname field SELECT S.sname FROM Reserves R, Sailors S WHERE R.sid=S.sid AND R.bid=100 AND S.rating> Reserves Sailors sid=sid bid=100 rating > 5 sname RA Tree:
sname
bid=100∧rating> (Reserves sid=sid Sailors))
(^) Query evaluation plan is an extended RA tree, with additional annotations: access method for each relation; implementation method for each relational operator. (^) Cost: 500+500*1000 I/Os (^) Misses several opportunities: selections could have been `pushed’ earlier, no use is made of any available indexes, etc. Reserves Sailors sid=sid bid=100 rating > 5 sname (Simple Nested Loops) (On-the-fly) (On-the-fly) (File scan) (File scan)
(^) Main difference: push selections below the join. (^) Materialization : store a temporary relation T, if the subsequent join needs to scan T multiple times. (^) With 5 buffers, cost of plan: Reserves (^) Sailors sid=sid bid= sname (On-the-fly) rating > 5 (Scan; write to temp T1) (Scan; write to temp T2) (Sort-Merge Join) (File scan) (File scan) (^) Scan Reserves (1000) + write temp T1 (10 pages, if we have 100 boats, uniform distribution).
(^) Main difference: push selections below the join. (^) Materialization : store a temporary relation T, if the subsequent join needs to scan T multiple times. (^) With 5 buffers, cost of plan: Reserves (^) Sailors sid=sid bid= sname (On-the-fly) rating > 5 (Scan; write to temp T1) (Scan; write to temp T2) (Sort-Merge Join) (File scan) (File scan) (^) Scan Reserves (1000) + write temp T1 (10 pages, if we have 100 boats, uniform distribution). (^) Scan Sailors (500) + write temp T2 (250 pages, if we have 10 ratings).
(^) Main difference: push selections below the join. (^) Materialization : store a temporary relation T, if the subsequent join needs to scan T multiple times. (^) With 5 buffers, cost of plan: Reserves (^) Sailors sid=sid bid= sname (On-the-fly) rating > 5 (Scan; write to temp T1) (Scan; write to temp T2) (Sort-Merge Join) (File scan) (File scan) (^) Scan Reserves (1000) + write temp T1 (10 pages, if we have 100 boats, uniform distribution). (^) Scan Sailors (500) + write temp T2 (250 pages, if we have 10 ratings). (^) Sort-Merge join: (^) Sort T1 (2210), sort T2 (23250), merge (10+250), total = 3560 I/Os.
(^) Main difference: push selections below the join. (^) Materialization : store a temporary relation T, if the subsequent join needs to scan T multiple times. (^) With 5 buffers, cost of plan: Reserves (^) Sailors sid=sid bid= sname (On-the-fly) rating > 5 (Scan; write to temp T1) (Scan; write to temp T2) (Sort-Merge Join) (File scan) (File scan) (^) Scan Reserves (1000) + write temp T1 (10 pages, if we have 100 boats, uniform distribution). (^) Scan Sailors (500) + write temp T2 (250 pages, if we have 10 ratings). (^) Sort-Merge join: (^) Sort T1 (2210), sort T2 (23250), merge (10+250), total = 3560 I/Os. BNL join:
(^) Selection using index : with clustered index on bid of Reserves, we get 100,000/100 = 1000 tuples on 1000/ = 10 pages. (^) Indexed NLJ: pipelining the outer and indexed lookup on the inner. The outer: scanned only once in join, pipelining, no need to materialize. The inner: Join column sid is a key for Sailors. At most one matching tuple, unclustered index on sid OK. Reserves Sailors sid=sid bid= sname (On-the-fly) rating > 5 (Use hash index; do not write result to temp) (Index Nested Loops, with pipelining ) (On-the-fly) (Hash index scan on bid) (^) Decision not to push rating>5 before the join is based on availability of sid index on Sailors. (^) Cost: Selection of Reserves tuples (10 I/Os); for each, must get matching Sailors tuple (1000*1.2); total 1210 I/Os.
Unary operator: when the input is pipelined into it, say the operator is applied on-the-fly, e.g. selection on-the- fly, project on-the-fly. Binary operator: e.g., the outer relation in indexed nested loops join.
(^) Statistics, maintained in system catalogs, used to estimate cost of operations and result sizes. (^) Considers combination of CPU and I/O costs.
(^) Only the space of left-deep plans is considered.
SELECT S.sname FROM Sailors S WHERE S.age IN ( SELECT MAX (S2.age) FROM Sailors S GROUP BY S2.rating ) Outer block Nested block