Docsity

Relational Query Optimization: CMPSCI 445 Fall 2008, Study notes of Management Information Systems

Partial preview of the text

Download Relational Query Optimization: CMPSCI 445 Fall 2008 and more Study notes Management Information Systems in PDF only on Docsity!

1

Relational Query Optimization

CMPSCI 445

Fall 2008

Slide Content Courtesy of R. Ramakrishnan, J. Gehrke, and J. Hellerstein

Schema for Examples

 Reserves:

 (^) Each tuple is 40 bytes long, 100 tuples per page, 1000 pages.

 Sailors:

 (^) 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)

Basics of Query Optimization

 (^) 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 };

System Catalog

 System information: buffer pool size and page size.

 For each relation:

 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…

 For each index:

 index name and structure (B+ tree)  search key attributes

 For each view:

 view name and definition

Relational Algebra Tree

 (^) 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

 (^) 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)

Alternative Plan 1 (No Indexes)

 (^) 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).

Alternative Plan 1 (No Indexes)

 (^) 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).

Alternative Plan 1 (No Indexes)

 (^) 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.

Alternative Plan 1 (No Indexes)

 (^) 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:

Alternative Plan 2 (With Indexes)

 (^) 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.

Pipelined Evaluation

 Materialization : Output of an op is saved in a

temporary relation for processing by the next op.

 Pipelining : Doesn’t create a temporary relation,

saves the cost of writing it out and reading it back.

Can occur in two cases:

 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.

Highlights of System R Optimizer

 Impact: Most widely used; works well for < 10 joins.

 Cost estimation: Approximate art at best.

 (^) Statistics, maintained in system catalogs, used to estimate cost of operations and result sizes.  (^) Considers combination of CPU and I/O costs.

 Plan Space: Too large, must be pruned.

 (^) Only the space of left-deep plans is considered.

  • Left-deep plan: a tree of joins in which the inner is a base relation.
  • Left-deep plans naturally support pipelining.  (^) Cartesian products avoided.

 Plan Search: Dynamic programming (prunes useless

subtrees).

Query Blocks: Units of Optimization

 An SQL query is parsed

into a collection of query

blocks , and these are

optimized one block at a

time.

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