the query compiler 16.6 choosing an order for joins by: nitin mathur id: 110 cs: 257 sec-1

13
THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Post on 20-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

THE QUERY COMPILER16.6 CHOOSING AN ORDER FOR JOINS

By: Nitin MathurId: 110

CS: 257 Sec-1

Page 2: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Introduction

• This section focuses on critical problem in cost-based optimization:– Selecting order for natural join of three or more

relations

• Compared to other binary operations, joins take more time and therefore need effective optimization techniques

Page 3: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Significance of Arguments & How it executes

• The argument relations in joins determine the cost of the join

• The left argument of the join is – Called the build relation– Assumed to be smaller– Stored in main-memory

Page 4: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Significance of Arguments & How it executes

• The right argument of the join is– Called the probe relation – Read a block at a time– Its tuples are matched with those of build relation

• The join algorithms which distinguish between the arguments are:– One-pass join– Nested-loop join– Index join

Page 5: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Join Trees• Order of arguments is important for joining two relations• Left argument, since stored in main-memory, should be smaller• With two relations only two choices of join tree• With more than two relations, there are n! ways to order the

arguments and therefore n! join trees, where n is the no. of relations

Page 6: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Join Trees Types• Consider 4 relations. Different ways to join them are as follows

Citation: Database System The Complete BookChapter 16 Figure: 16.27

Page 7: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Join Trees Types

• In fig (a) all the right children are leaves. This is a left-deep tree• In fig (c) all the left children are leaves. This is a right-deep tree• Fig (b) is a bushy tree• Considering left-deep trees is advantageous for deciding join

orders

Page 8: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Join order

• Join order selection– A1 A2 A3 .. An– Left deep join trees

– Dynamic programming• Best plan computed for each subset of

relations– Best plan (A1, .., An) = min cost plan of(

Best plan(A2, .., An) A1 Best plan(A1, A3, .., An) A2 …. Best plan(A1, .., An-1)) An

Page 9: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Dynamic Programming to Select a Join Order and Grouping

• To pick an order for the join of many relations there are three choices• Consider them all• Consider a subset• Use a heuristic to pick one

• Use Dynamic Programming to enumerate trees

Page 10: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Dynamic Programming to Select a Join Order and Grouping

• Dynamic programming is used either to consider all or a subset

• Construct a table of costs based on relation size• Remember only the minimum entry which will

required to proceed

Page 11: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Dynamic Programming with More Detailed Cost Functions

• Disadvantage of dynamic programming is that it does not involve the actual costs of the joins in the calculations

• Can be improved by considering• Use disk’s I/O for evaluating cost• When computing cost of R1 join R2, since we sum

cost of R1 and R2, we must also compute estimates for there sizes

Page 12: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

A Greedy Algorithm for Selecting a Join Order

• It is expensive to use an exhaustive method like dynamic programming

• Better approach is to use a join-order heuristic for the query optimization

• Greedy algorithm is an example of that• Make one decision at a time and never backtrack on

the decisions once made

Page 13: THE QUERY COMPILER 16.6 CHOOSING AN ORDER FOR JOINS By: Nitin Mathur Id: 110 CS: 257 Sec-1

Thank you4

Constructive listening