Database Management Systems: Query Optimization in Relational DBMS, Slides of Database Management Systems (DBMS)

An excerpt from the book 'database management systems' by r. Ramakrishnan and j. Gehrke. It discusses the importance of query optimization in a relational dbms, the two parts of optimizing a query, and the process of considering alternative plans and estimating costs for single and multiple relation queries.

Typology: Slides

2011/2012

Uploaded on 02/15/2012

arien
arien 🇺🇸

4.8

(24)

309 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 1
RelationalQueryOptimization
Chapter15
DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 2
HighlightsofSystemROptimizer
Impact:
Mostwidelyusedcurrently;wo rks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plansallow outputofeachoperatortobepipelined into
thenextoperatorwitho utstoringitinatempora ryrelation.
Cartesianproductsavoided.
DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 3
OverviewofQueryOptimization
Plan:TreeofR.A.ops,withchoiceof alg foreachop.
Eachoperatortypicallyimplementedusinga`pull’
interface:whenanoperatoris`pulled’forthenextoutput
tuples,it`pulls’onitsinputsandcomputesthem.
Twomainissues:
Foragivenquery,whatplansareconsidered?
Algorithmtosearchplan spaceforcheapest(estimated)plan.
Howisthecostofaplanestimated?
Ideally:Wanttofindbestplan.Practically:Avoid
worstplans!
WewillstudytheSystemRapproach.
pf3
pf4
pf5

Partial preview of the text

Download Database Management Systems: Query Optimization in Relational DBMS and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Relational Query Optimization

Chapter 15

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Highlights of System R Optimizer

Impact:

 Most widely used currently; 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 plans allow output of each operator to be pipelined into the next operator without storing it in a temporary relation.  Cartesian products avoided.

Overview of Query Optimization

Plan : Tree of R.A. ops, with choice of alg for each op.

 Each operator typically implemented using a pull’ interface: when an operator ispulled’ for the next output tuples, it `pulls’ on its inputs and computes them.

Two main issues:

 For a given query, what plans are considered?

  • Algorithm to search plan space for cheapest (estimated) plan.  How is the cost of a plan estimated?

Ideally: Want to find best plan. Practically: Avoid

worst plans!

We will study the System R approach.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Schema for Examples

Similar to old schema; rname added for variations.

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)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5

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.

Nested blocks are usually treated as calls to a subroutine, made once per outer tuple. (This is an over- simplification, but serves for now.)

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

For each block, the plans considered are:

  • All available access methods, for each reln in FROM clause.
  • All left-deep join trees (i.e., all ways to join the relations one- at-a-time, with the inner reln in the FROM clause, considering all reln permutations and join methods.)

Relational Algebra Equivalences

Allow us to choose different join orders and to `push’

selections and projections ahead of joins.

Selections : σ c 1 ∧ ...∧ cn ( R ) ≡σ c 1 (.. .σ cn ( R )) ( Cascade )

σ c 1 ( σ c 2 ( R ) ) ≡ σ c 2 ( σ c 1 ( R )) ( Commute )

Projections:^ π a 1 (^ R )^^ ≡^ π^ a 1 (...^ (^ π an (^ R ))) (Cascade)

Joins: R  (S  T) ≡ (R  S)  T (Associative)

(R  S) ≡ (S   R) (Commute)

+ Show that: R  (S  T) ≡ (T  R)  S

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

Cost Estimates for Single-Relation Plans

Index I on primary key matches selection:

Cost is Height(I)+1 for a B+ tree, about 1.2 for hash index.

Clustered index I matching one or more selects:

(NPages(I)+NPages(R)) * product of RF’s of matching selects.

Non-clustered index I matching one or more selects:

(NPages(I)+NTuples(R)) * product of RF’s of matching selects.

Sequential scan of file:

NPages(R).

+ Note: Typically, no duplicate elimination on projections!

(Exception: Done on answers if user says DISTINCT.)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11

Example

If we have an index on rating :

(1/NKeys(I)) * NTuples(R) = (1/10) * 40000 tuples retrieved. Clustered index: (1/NKeys(I)) * (NPages(I)+NPages(R)) = (1/10) * (50+500) pages are retrieved. (This is the cost .) Unclustered index: (1/NKeys(I)) * (NPages(I)+NTuples(R)) = (1/10) * (50+40000) pages are retrieved.

If we have an index on sid :

Would have to retrieve all tuples/pages. With a clustered index, the cost is 50+500, with unclustered index, 50+40000.

Doing a file scan:

We retrieve all file pages (500).

SELECT S.sid FROM Sailors S WHERE S.rating=

Queries Over Multiple Relations

Fundamental decision in System R: only left-deep join

trees are considered.

As the number of joins increases, the number of alternative plans grows rapidly; we need to restrict the search space. Left-deep trees allow us to generate all fully pipelined plans.

  • Intermediate results not written to temporary files.
  • Not all left-deep trees are fully pipelined (e.g., SM join).

A B

C

D

A B

C

D

A B C D

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13

Enumeration of Left-Deep Plans

Left-deep plans differ only in the order of relations,

the access method for each relation, and the join

method for each join.

Enumerated using N passes (if N relations joined):

Pass 1: Find best 1-relation plan for each relation. Pass 2: Find best way to join result of each 1-relation plan (as outer) to another relation. (All 2-relation plans.) Pass N: Find best way to join result of a (N-1)-relation plan (as outer) to the N’th relation. (All N-relation plans.)

For each subset of relations, retain only:

Cheapest plan overall, plus Cheapest plan for each interesting order of the tuples.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

Enumeration of Plans (Contd.)

ORDER BY, GROUP BY, aggregates etc. handled as a

final step, using either an `interestingly ordered’

plan or an addional sorting operator.

An N-1 way plan is not combined with an

additional relation unless there is a join condition

between them, unless all predicates in WHERE have

been used up.

i.e., avoid Cartesian products if possible.

In spite of pruning plan space, this approach is still

exponential in the # of tables.

Cost Estimation for Multirelation Plans

Consider a query block:

Maximum # tuples in result is the product of the

cardinalities of relations in the FROM clause.

Reduction factor (RF) associated with each term reflects

the impact of the term in reducing result size. Result

cardinality = Max # tuples * product of all RF’s.

Multirelation plans are built up by joining one new

relation at a time.

 Cost of join method, plus estimation of join cardinality gives us both cost estimate and result size estimate

SELECT attribute list FROM relation list WHERE term1 AND ... AND termk

Summary (Contd.)

Single-relation queries:

All access paths considered, cheapest is chosen. Issues : Selections that match index, whether index key has all needed fields and/or provides tuples in a desired order.

Multiple-relation queries:

All single-relation plans are first enumerated.

  • Selections/projections considered as early as possible. Next, for each 1-relation plan, all ways of joining another relation (as inner) are considered. Next, for each 2-relation plan that is retained’, all ways of joining another relation (as inner) are considered, etc. At each level, for each subset of relations, only best plan for each interesting order of tuples isretained’.