DOC PREVIEW
UW CSE 444 - Query Optimization

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Query OptimizationQuery Optimization IssuesQuery Rewriting: Predicate PushdownQuery Rewrites: Predicate Pushdown (more complicated)Query Rewrite: Predicate MovearoundSlide 6Slide 7Query Rewrite SummaryEnumeration of Alternative PlansQueries Over Multiple RelationsEnumeration of Left-Deep PlansEnumeration of Plans (Contd.)ExampleNested QueriesCost EstimationStatistics and CatalogsSize Estimation and Reduction FactorsQuery OptimizationReservesSailorssid=sidbid=100 rating > 5sname(Simple Nested Loops)Imperative query execution plan:SELECT S.snameFROM Reserves R, Sailors SWHERE R.sid=S.sid AND R.bid=100 AND S.rating>5Declarative SQL queryPlan: Tree of R.A. ops, with choice of alg for each op.Ideally: Want to find best plan. Practically: Avoid worst plans!Goal:Query Optimization Issues•Query rewriting: –transformations from one SQL query to another one using semantic properties. •Selecting query execution plan:–done on single query blocks (I.e., S-P-J blocks)–main step: join enumeration•Cost estimation:–to compare between plans we need to estimate their cost using statistics on the database.Query Rewriting: Predicate PushdownReservesSailorssid=sidbid=100 rating > 5snameReservesSailorssid=sidbid=100 snamerating > 5(Scan;write to temp T1)(Scan;write totemp T2)The earlier we process selections, less tuples we need to manipulatehigher up in the tree (but may cause us to loose an important orderingof the tuples.Query Rewrites: Predicate Pushdown (more complicated)Select bid, Max(age)From Reserves R, Sailors SWhere R.sid=S.sid GroupBy bidHaving Max(age) > 40Select bid, Max(age)From Reserves R, Sailors SWhere R.sid=S.sid and S.age > 40GroupBy bidHaving Max(age) > 40• Advantage: the size of the join will be smaller.• Requires transformation rules specific to the grouping/aggregation operators.• Won’t work if we replace Max by Min.Query Rewrite: Predicate MovearoundCreate View V1 ASSelect rating, Min(age)From Sailors SWhere S.age < 20GroupBy bidCreate View V2 ASSelect sid, rating, age, dateFrom Sailors S, Reserves RWhere R.sid=S.sidSelect sid, dateFrom V1, V2Where V1.rating = V2.rating and V1.age = V2.ageSailing wizz dates: when did the youngest of each sailor level rent boats?Query Rewrite: Predicate MovearoundCreate View V1 ASSelect rating, Min(age)From Sailors SWhere S.age < 20GroupBy bidCreate View V2 ASSelect sid, rating, age, dateFrom Sailors S, Reserves RWhere R.sid=S.sidSelect sid, dateFrom V1, V2Where V1.rating = V2.rating and V1.age = V2.age, age < 20Sailing wizz dates: when did the youngest of each sailor level rent boats?First, move predicates up the tree.Query Rewrite: Predicate MovearoundCreate View V1 ASSelect rating, Min(age)From Sailors SWhere S.age < 20GroupBy bidCreate View V2 ASSelect sid, rating, age, dateFrom Sailors S, Reserves RWhere R.sid=S.sid, and S.age < 20.Select sid, dateFrom V1, V2Where V1.rating = V2.rating and V1.age = V2.age, and age < 20Sailing wizz dates: when did the youngest of each sailor level rent boats?First, move predicates up the tree.Then, move themdown.Query Rewrite Summary•The optimizer can use any semantically correct rule to transform one query to another.•Rules try to:–move constraints between blocks (because each will be optimized separately)–Unnest blocks•Especially important in decision support applications where queries are very complex.Enumeration of Alternative Plans•Task: create a query execution plan for a single Select-project-join block (well, and aggregates).•Main principle: some sort of search through the set of plans.–Assume some cost estimation model; more later.•Single-relation block case (only select, project, aggregation): –Each available access path is considered, and the one with the least estimated cost is chosen.–The different operations are essentially carried out together (e.g., if an index is used for a selection, projection is done for each retrieved tuple, and the resulting tuples are pipelined into the aggregate computation).Queries Over Multiple Relations•In principle, we need to consider all possible join orderings:•As the number of joins increases, the number of alternative plans grows rapidly; we need to restrict the search space.•System-R: consider only left-deep join trees.–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).BACDBACDCDBAEnumeration of Left-Deep Plans•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.Enumeration of Plans (Contd.)•ORDER BY, GROUP BY, aggregates etc. handled as a final step, using either an `interestingly ordered’ plan or an additional 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.•If we want to consider all (bushy) trees, we need only a slight modification to the algorithm.Example•Pass 1:–Sailors: B+ tree matches rating>5, and is probably cheapest. However, if this selection is expected to retrieve a lot of tuples, and index is unclustered, file scan may be cheaper.•Still, B+ tree plan kept (tuples are in rating order).–Reserves: B+ tree on bid matches bid=100; cheapest.•Pass 2: We consider each plan retained from Pass 1 as the outer, and consider how to join it with the (only) other relation. e.g., Reserves as outer: Hash index can be used to get Sailors tuples that satisfy sid = outer tuple’s sid value.Sailors: B+ tree on rating Hash on sidReserves: B+ tree on bidReservesSailorssid=sidbid=100 rating > 5snameNested Queries•Nested block is optimized independently, with the outer tuple considered as providing a selection condition.•Outer block is


View Full Document

UW CSE 444 - Query Optimization

Documents in this Course
XML

XML

48 pages

SQL

SQL

25 pages

SQL

SQL

42 pages

Recovery

Recovery

30 pages

SQL

SQL

36 pages

Indexes

Indexes

35 pages

Security

Security

36 pages

Wrap-up

Wrap-up

6 pages

SQL

SQL

37 pages

More SQL

More SQL

48 pages

SQL

SQL

35 pages

XML

XML

46 pages

Triggers

Triggers

26 pages

Load more
Download Query Optimization
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Query Optimization and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Query Optimization 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?