Lecture 23: Query Execution

25
1 Lecture 23: Query Execution Wednesday, November 23, 2005

description

Lecture 23: Query Execution . Wednesday, November 23, 2005. Outline. Query execution: 15.1 – 15.5. Architecture of a Database Engine. SQL query. Parse Query. Logical plan. Select Logical Plan. Query optimization. Select Physical Plan. Physical plan. Query Execution. - PowerPoint PPT Presentation

Transcript of Lecture 23: Query Execution

Page 1: Lecture 23: Query Execution

1

Lecture 23:Query Execution

Wednesday, November 23, 2005

Page 2: Lecture 23: Query Execution

2

Outline

• Query execution: 15.1 – 15.5

Page 3: Lecture 23: Query Execution

3

Architecture of a Database Engine

Parse Query

Select Logical Plan

Select Physical Plan

Query Execution

SQL query

Queryoptimization

Logicalplan

Physicalplan

Page 4: Lecture 23: Query Execution

4

Logical Algebra Operators

• Union, intersection, difference• Selection • Projection • Join |x| • Duplicate elimination • Grouping • Sorting

Page 5: Lecture 23: Query Execution

5

Logical Query Plan

SELECT city, count(*)FROM salesGROUP BY cityHAVING sum(price) > 100

sales(product, city, price)

city, sum(price)→p, count(*) → c

p > 100

city, c

T1(city,p,c)

T2(city,p,c)

T3(city, c)

T1, T2, T3 = temporary tables

Page 6: Lecture 23: Query Execution

6

Logical Query PlanSELECT S.buyerFROM Purchase P, Person QWHERE P.buyer=Q.name AND Q.city=‘seattle’ AND Q.phone > ‘5430000’

Purchase Person

Buyer=name

City=‘seattle’ phone>’5430000’

buyer

Page 7: Lecture 23: Query Execution

7

Physical Query PlanSELECT S.buyerFROM Purchase P, Person QWHERE P.buyer=Q.name AND Q.city=‘seattle’ AND Q.phone > ‘5430000’

Query Plan:• logical tree• implementation choice at every node• scheduling of operations.

Purchase Person

Buyer=name

City=‘seattle’ phone>’5430000’

buyer

(Simple Nested Loops)

(Table scan) (Index scan)

Some operators are from relationalalgebra, and others (e.g., scan)are not.

Page 8: Lecture 23: Query Execution

8

Question in Class

Logical operator: Product(pname, cname) || Company(cname, city)

Propose three physical operators for the join, assuming the tables are in main memory:

1. 2. 3.

Page 9: Lecture 23: Query Execution

9

Question in ClassProduct(pname, cname) |x| Company(cname, city)

• 1000000 products• 1000 companies

How much time do the following physical operators take if the data is in main memory ?

• Nested loop join time = • Sort and merge = merge-join time =• Hash join time =

Page 10: Lecture 23: Query Execution

10

Cost Parameters

In database systems the data is on disks, not in main memory

The cost of an operation = total number of I/OsCost parameters:

• B(R) = number of blocks for relation R• T(R) = number of tuples in relation R• V(R, a) = number of distinct values of attribute a

Page 11: Lecture 23: Query Execution

11

Cost Parameters

• Clustered table R:– Blocks consists only of records from this table– B(R) T(R) / blockSize

• Unclustered table R:– Its records are placed on blocks with other tables– When R is unclustered: B(R) T(R)

• When a is a key, V(R,a) = T(R)• When a is not a key, V(R,a)

Page 12: Lecture 23: Query Execution

12

Cost

Cost of an operation =number of disk I/Os needed to:– read the operands– compute the result

Cost of writing the result to disk is not included on the following slides

Page 13: Lecture 23: Query Execution

13

Scanning a Table

• Clustered relation:– Table scan:

• Result may be unsorted: B(R)• Result needs to be sorted: 3B(R)

– Index scan• Unsorted: B(R)• Sorted: B(R) or 3B(R)

• Unclustered relation– Unsorted: T(R)– Sorted: T(R) + 2B(R)

Page 14: Lecture 23: Query Execution

14

One-Pass Algorithms

Selection (R), projection (R)• Both are tuple-at-a-time algorithms• Cost: B(R)

Input buffer Output bufferUnaryoperator

Page 15: Lecture 23: Query Execution

15

One-pass Algorithms

Hash join: R |x| S• Scan S, build buckets in main memory• Then scan R and join

• Cost: B(R) + B(S)• Assumption: B(S) <= M

Page 16: Lecture 23: Query Execution

16

One-pass Algorithms

Duplicate elimination (R)• Need to keep tuples in memory• When new tuple arrives, need to compare it

with previously seen tuples• Balanced search tree, or hash table• Cost: B(R)• Assumption: B((R)) <= M

Page 17: Lecture 23: Query Execution

17

Question in Class

Grouping:Product(name, department, quantity)

department, sum(quantity) (Product) Answer(department, sum)

Question: how do you compute it in main memory ?

Answer:

Page 18: Lecture 23: Query Execution

18

One-pass Algorithms

Grouping: department, sum(quantity) (R)• Need to store all departments in memory• Also store the sum(quantity) for each

department• Balanced search tree or hash table• Cost: B(R)• Assumption: number of departments fits in

memory

Page 19: Lecture 23: Query Execution

19

One-pass Algorithms

Binary operations: R ∩ S, R ∪ S, R – S• Assumption: min(B(R), B(S)) <= M• Scan one table first, then the next, eliminate

duplicates• Cost: B(R)+B(S)

Page 20: Lecture 23: Query Execution

20

Question in ClassH emptyHashTable

/* scan R */For each x in R do insert(H, x )

/* scan S */For each y in S do _____________________

/* collect result */for each z in H do output(z)

What do wedo in each ofthese cases:R ∩ S, R ∪ S, R – S

Page 21: Lecture 23: Query Execution

21

Nested Loop Joins

• Tuple-based nested loop R ⋈ S

• Cost: T(R) B(S) when S is clustered• Cost: T(R) T(S) when S is unclustered

for each tuple r in R do for each tuple s in S do if r and s join then output (r,s)

Page 22: Lecture 23: Query Execution

22

Nested Loop Joins

• We can be much more clever

• Question: how would you compute the join in the following cases ? What is the cost ?

– B(R) = 1000, B(S) = 2, M = 4

– B(R) = 1000, B(S) = 3, M = 4

– B(R) = 1000, B(S) = 6, M = 4

Page 23: Lecture 23: Query Execution

23

Nested Loop Joins

• Block-based Nested Loop Join

for each (M-2) blocks bs of S do for each block br of R do for each tuple s in bs for each tuple r in br do if “r and s join” then output(r,s)

Page 24: Lecture 23: Query Execution

24

Nested Loop Joins

. . .. . .

R & SHash table for block of S

(M-2 pages)

Input buffer for R Output buffer

. . .

Join Result

Page 25: Lecture 23: Query Execution

25

Nested Loop Joins

• Block-based Nested Loop Join• Cost:

– Read S once: cost B(S)– Outer loop runs B(S)/(M-2) times, and each

time need to read R: costs B(S)B(R)/(M-2)– Total cost: B(S) + B(S)B(R)/(M-2)

• Notice: it is better to iterate over the smaller relation first

• R |x| S: R=outer relation, S=inner relation