2015-6-27prof. ghandeharizadeh1 query processing (chapter 12)

27
22/6/27 Prof. Ghandehari zadeh 1 QUERY PROCESSING (CHAPTER 12)

Post on 21-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 1

QUERY PROCESSING (CHAPTER 12)

Page 2: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 2

Software Architecture of a DBMS

File System

Buffer Pool Manager

Abstraction of records

Index structures

Query Interpretor

Query Optimizer

Relational Algebra operators: , , , , , , , ,

Query Parser

Page 3: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 3

TERM DEFINITION

• P(R): Number of pages that constitute R

• t(R): Number of tuples that constitute R

• ν(A,R): the number of unique values for attribute A of R

• min(A,R): the minimum value for attribute A of R

• max(A,R): the maximum value for attribute A of R

• P(IR,A): the number of pages that constitute the B+-tree index on attribute A of R

• d(IR,A): the depth of a B+-tree index on attribute A of R

• lp(IR,A): the number of leaf pages for a B+-tree index on attribute A of R

• B(IR,A): the number of buckets for a hash index on attribute A of R

Page 4: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 4

HEAP FILE ORGANIZATION

• Assume a student table: Student(name, age, gpa, major)

t(Student) = 16

P(Student) = 4

Bob, 21, 3.7, CS

Mary, 24, 3, ECE

Tom, 20, 3.2, EE

Kathy, 18, 3.8, LS

Kane, 19, 3.8, ME

Lam, 22, 2.8, ME

Chang, 18, 2.5, CS

Vera, 17, 3.9, EE

Louis, 32, 4, LS

Martha, 29, 3.8, CS

James, 24, 3.1, ME

Pat, 19, 2.8, EE

Chris, 22, 3.9, CS

Chad, 28, 2.3, LS

Leila, 20, 3.5, LS

Shideh, 16, 4, CS

Page 5: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 5

Non-Clustered Hash Index

• A non-clustered hash index on the age attribute with 4 buckets,

• h(age) = age % B

Bob, 21, 3.7, CS

Mary, 24, 3, ECE

Tom, 20, 3.2, EE

Kathy, 18, 3.8, LS

Kane, 19, 3.8, ME

Lam, 22, 2.8, ME

Chang, 18, 2.5, CS

Vera, 17, 3.9, EE

Louis, 32, 4, LS

Martha, 29, 3.8, CS

James, 24, 3.1, ME

Pat, 19, 2.8, EE

Chris, 22, 3.9, CS

Chad, 28, 2.3, LS

Leila, 20, 3.5, LS

Shideh, 16, 4, CS

(21, (1, 1))

(17, (2,4))

(29, (3,2))

(24, (1, 2))

(32, (3,1))

(20, (1,3))

(18, (1, 4))

(22, (2,2))

(22, (4,1))(19, (2, 1))

(28, (4,2))

(20, (4,3))

(16, (4,4))

0123

(19, (3, 4))(18, (2,3))

(24, (3,3))

Page 6: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 6

Clustered Hash Index

• A clustered hash index on the age attribute with 4 buckets,

• h(age) = age % B

Bob, 21, 3.7, CS

Mary, 24, 3, ECE

Tom, 20, 3.2, EE

Kathy, 18, 3.8, LS

Kane, 19, 3.8, MELam, 22, 2.8, ME

Chang, 18, 2.5, CS

Vera, 17, 3.9, EE

Louis, 32, 4, LS

Martha, 29, 3.8, CS

James, 24, 3.1, ME

Pat, 19, 2.8, EEChris, 22, 3.9, CS

Chad, 28, 2.3, LS

Leila, 20, 3.5, LS

Shideh, 16, 4, CS

0123

Page 7: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 7

Non-Clustered Secondary B+-Tree

• A non-clustered secondary B+-tree on the gpa attribute

Bob, 21, 3.7, CS

Mary, 24, 3, ECE

Tom, 20, 3.2, EE

Kathy, 18, 3.8, LS

Kane, 19, 3.8, ME

Lam, 22, 2.8, ME

Chang, 18, 2.5, CS

Vera, 17, 3.9, EE

Louis, 32, 4, LS

Martha, 29, 3.8, CS

James, 24, 3.1, ME

Pat, 19, 2.8, EE

Chris, 22, 3.9, CS

Chad, 28, 2.3, LS

Leila, 20, 3.5, LS

Shideh, 16, 4, CS

(3.7, (1, 1))

(3.8, (3,2))

(3.8, (2,1))

(3.9, (2,4))

(4, (3,1))

(3.8, (1,4))

(3.9, (4,1))

(4, (4,4))

(2.3, (4, 2))

(2.5, (2,3))

(2.8, (2,2))

(3.1, (3,3))

(3.2, (1,3)

(2.8, (3,4))

(3, (1,2))

(3.5, (4,3))

3.6

Page 8: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 8

Non-Clustered Primary B+-Tree

• A non-clustered primary B+-tree on the gpa attribute

Bob, 21, 3.7, CSMary, 24, 3, ECE

Tom, 20, 3.2, EE

Kathy, 18, 3.8, LS

Kane, 19, 3.8, MELam, 22, 2.8, ME

Chang, 18, 2.5, CS Vera, 17, 3.9, EE

Louis, 32, 4, LS

Martha, 29, 3.8, CS

James, 24, 3.1, ME

Pat, 19, 2.8, EE

Chris, 22, 3.9, CSChad, 28, 2.3, LS

Leila, 20, 3.5, LS Shideh, 16, 4, CS

(3.7, (3, 1))

(3.8, (3,2))

(3.8, (3,3))

(3.9, (4,2))

(4, (4,3))

(3.8, (3,4))

(3.9, (4,1))

(4, (4,4))

(2.3, (1, 1))

(2.5, (1,2))

(2.8, (1,3))

(3.1, (2,2))

(3.2, (2,3)

(2.8, (1,4))

(3, (2,1))

(3.5, (2,4))

3.6

Page 9: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 9

Clustered B+-Tree

• A clustered B+-tree on the gpa attribute

• It is impossible to have a clustered secondary B+-tree on an attribute.

Bob, 21, 3.7, CSMary, 24, 3, ECE

Tom, 20, 3.2, EE

Kathy, 18, 3.8, LS

Kane, 19, 3.8, MELam, 22, 2.8, ME

Chang, 18, 2.5, CS Vera, 17, 3.9, EE

Louis, 32, 4, LS

Martha, 29, 3.8, CS

James, 24, 3.1, ME

Pat, 19, 2.8, EE

Chris, 22, 3.9, CSChad, 28, 2.3, LS

Leila, 20, 3.5, LS Shideh, 16, 4, CS

2.9 3.6 3.8

Page 10: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 10

COST OF PERFORMING SELECT(бθ (R)) OPERATOR

Exact match queries (θ = attribute A equals constant C):

• Heap: Scan the relation one page after another, until end of relation: P(R)

• Primary B+-tree: Use the constant to traverse the depth of the tree to the leftmost data record, begin to search forward:

d(I) + P(R) / ν(A,R)

• Secondary B+-tree : Locate the left most record using the constant, initiate the search using the records in the leaf nodes. For each index record that matches, perform a random I/O to the data file:

d(I) + lp(I) / ν(A,R) + t(R) / ν(A,R)

• Hash index assuming a uniform distribution of tuples across pages: probe the hash index with the constant:

P(R) / B(I)

Page 11: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 11

COST OF PERFORMING SELECT(бθ (R)) OPERATOR (Cont…)

Range queries (θ = attribute A is greater than constant C):

Number of tuples that satisfy the selection predicate:

t(θ =‘A>C’) = [max(A,R)-C] / [max(A,R)-min(A,R)] × t(R)

• Heap: Scan the relation one page after another, until end of relation: P(R)

• Primary B+-tree:

d(I) + [t(θ =‘A>C’) × P(R) / t(R)]

• Secondary B+-tree:

d(I) + t(θ =‘A>C’) / [t(R) / Ip(I)] + t(θ =‘A>C’)

Page 12: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 12

ESTIMATING NUMBER OF RESULTING RECORDS

• For exact match selection predicates assume a uniform distribution of records across the number of unique values. E.g., the selection predicate is gpa = 3.3

• For range selection predicates assume a uniform distribution of records across the range of available values defined by min and max. In this case, one must think about the interval. E.g., gpa > 3.5

0

0.5

1

1.5

2

2.3 2.5 2.8 3 3.1 3.2 3.5 3.7 3.8 3.9 4

0

0.2

0.4

0.6

0.8

1

43.93.83.73.53.23.132.82.52.3

Page 13: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 13

ESTIMATING NUMBER OF RESULTING RECORDS (Cont…)

0

1

2

3

4

2.3 2.5 2.8 3 3.1 3.2 3.5 3.7 3.8 3.9 4

Actual GPA Values

Page 14: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 14

PROJECTION

• The previous lecture described the selection operator and techniques to estimate both its costs and produced number of tuples. These techniques assume a uniform distribution of values within the minimum and maximum values of an attribute, uniform distribution of tuples across the buckets of a hash index.

• Projection (πA( R))

Logically, the system scans R and produces as output the unique values for attribute A (eliminates duplicates). The number of tuples produced by this operator is (πA( R)) with duplicate elimination and t(R) without duplicate elimination. Without duplicate elimination, the cost of this operator is one scan of relation R; cost equals P(R). With duplicate elimination, its cost equals: sort(R) + P(R).

Page 15: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 15

EXTERNAL SORTING

• Sort a relation that is larger than the available main memory.

• Consider a relation R with P(R) pages and w buffer pages (w ≥ 3).

Assume that P(R)/w=2k=(w-1)l

• First approach, 2-way merge sort:1: Read in, sort in main memory, and write out chunks of w pages. This step produces

2k chunks of w pages.

2: Read in, merge in main memory, and write out pairs of sorted chunks of w pages. This produces 2k-1 sorted chunks of w × 2 pages.

3: Read in, merge in main memory, and write out pairs of sorted chunks of w pages. This produces 2k-2 sorted chunks of w × 22 pages.

.: .....

k: Read in, merge in main memory, and write out pairs of sorted chunks of w × 2 k-2 pages. This produces 2 sorted chunks of w × 2k-1 pages.

k+1:Read in, merge in main memory, and write out pairs of sorted chunks of w × 2k-1 pages. This produces 1 sorted chunk of w × 2k = P(R) pages.

Page 16: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 16

EXTERNAL SORTING (Cont…)

• Example: Sort a 20 page relation assuming a five page buffer pool.

Merge sort

Page 17: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 17

EXTERNAL SORTING (Cont…)

• Each pass requires reading and writing of the entire file. The algorithm's I/O cost is:

cost(2-way sort) = 2 × P(R) × [log2(P(R)/w) +1]

• If P(R)/w is not a power of two then we take the ceiling of the logarithm.

Page 18: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 18

EXTERNAL SORTING (Cont…)

• Second approach, (w-1)-way merge sort:1: Read in, sort in main memory, and write out chunks of w pages. This step produces

(w-1)l sorted chunks of w pages.

2: Read in, merge in main memory, and write out sets of (w-1) sorted chunks of w pages. This produces (w-1)l-1 sorted chunks of (w-1) w pages.

3: Read in, merge in main memory, and write out sets of (w-1) sorted chunks of (w-1)w pages. This produces (w-1)l-2 sorted chunks of (w-1)2 w pages.

.: .....

l: Read in, merge in main memory, and write out sets of (w-1) sorted chunks of (w-1)l-

2w pages. This produces w-1 sorted chunks of (w-1)l-1w pages.

l+1:Read in, merge in main memory, and write out sets of (w-1) sorted chunks of (w-1)l-1w pages. This produces 1 sorted chunk of (w-1)lw = P(R) pages.

Page 19: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 19

EXTERNAL SORTING (Cont…)

• Merging (w-1) temporary files is done by reading one page from each stream and simultaneously comparing the values of (w-1) tuples, one from each stream.

• Example: Sort a 20 page relation assuming a five page buffer pool.

Page 20: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 20

EXTERNAL SORTING (Cont…)

• Each pass requires reading and writing of the entire file. The algorithm's I/O cost is:

cost((w-1)way sort) = 2 × P(R) × [l +1] = 2 × P(R) × [logw-1(P(R)/w) + 1]

• How does the I/O cost of 2-way merge sort compares with (w-1)-way merge sort for the 20 page relation?

• 2-way merge sort is more expensive in I/O, but it is simpler to implement. Overall it looses because the time attributed to performing I/O operations is more significant as compared to CPU execution time of a simple routine.

Page 21: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 21

EQUALITY JOIN

• Estimated number of output tuples is: t(R) × t(S) / ν(A,R)

• Two algorithms for performing the join operator: nested loops and merge-scan.

• Nested-loops:

• Tuple nested loops: for each tuple r in R do

for each tuple s in S do

if r.A=s.A then output r,s in the result relation

end-for

end-for

• Estimated cost of tuple nested loops:– P(R) + [t(R) × P(S)]

P(S)

P(R)

Page 22: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 22

EQUALITY JOIN (Cont…)

• Page nested loops: for each page rp in R do

fix rp in the buffer pool

for each page sp in S do

for each tuple r in rp do

for each tuple s in sp do

if r.A=s.A then output r,s in the result

end-for

end-for

end-for

unfix rp

end-for

• Estimated cost of page nested loops:– P(R) + [P(R) × P(S)]

P(R)

P(S)

Page 23: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 23

EQUALITY JOIN (Cont…)

• Merge-scan:1. Sort R on attribute A

2. Sort S on attribute A

3. Scan R and S in parallel, merging tuples with matching A values

Estimated cost of merge scan: sort(R) + sort(S) + P(R) + P(S)

• Tuple nested-loops with alternative index structures:– Heap: P(R) + [t(R) × P(S)]

– Clustered hash index: P(R) + [t(R) × P(S) / B(I)]

– Non-clustered hash index: P(R) + [t(R) × (P(I) / B(I) + t(S) / ν(A,S))]

– Clustered B+-tree: P(R) + [t(R) × (d(IR,A) + P(S) / ν(A,S))]

– Non-clustered B+-tree: P(R) + [t(R) × (d(IR,A) + lp(I) / ν(A,S) + t(S) / ν(A,S))]

Page 24: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 24

EQUALITY JOIN (Cont…)

• Merge scan with alternative index structures. It is not important whether a relation is hashed or not. Assume w buffer pages. We sort a relation using either a 2-way or (w-1)-way merge sort:

• Example: Assume the following statistics on the Employee and Department relations: t(Dept)=1000 tuples, P(Dept)=100 disk pages, ν(Dept,dno)=1000, ν(Dept,dname)=500. t(Employee)=100,000 tuples and P(Employee)=10,000 pages. Note that 10 tuples of each relation fit on a disk page. Assume that a concatenation of one Employee and one Dept record is wide enough to enable a disk page to hold five such records.

Lets compare the cost of two alternative algebraic expressions for processing a query that retrieves those employees that work for the toy department:

– б dname=Toy(Emp Dept)

– Emp б dname=Toy(Dept)

Page 25: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 25

EQUALITY JOIN (Cont…)

• б dname=Toy(Emp Dept)

Emp Dept

Tmp1

б dname=Toy

Tmp2t(Tmp1) = t(Emp) × t(Dept) / ν(Dept, dno)

= 100,000 ×1000 / 1000 = 100,000

P(Tmp1) = 100,000 / 5 = 20,000

C( ) = P(Dept) + P(Emp) ×P(Dept)

= 100 +10,000 ×100 = 1,000,100 (page nested loop)

Cw(Tmp1) = 20,000

Cw(б) = 20,000

t(Tmp2) = t(Tmp1) / ν(Dept, dname)

= 100,000 / 500 = 200

P(Tmp2) = 200 / 5 = 40

Cw(Tmp2) = 40

Cost = C( ) + Cw(Tmp1) + C(б) + Cw(Tmp2)

= 1,000,100 + 20,000 + 20,000 + 40

= 1,040,140 I/O

Page 26: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 26

EQUALITY JOIN (Cont…)

• Emp б dname=Toy(Dept)

Emp

Dept

Tmp1

б dname=Toy

Tmp2Cw(б) = 100

t(Tmp1) = t(Dept) / ν(Dept, dname)

= 1000 / 500 = 2

P(Tmp1) = 1

Cw(Tmp1) = 1

C( ) = P(Tmp1) + P(Tmp1) ×P(Emp)

= 1 +1×10,000 = 10,001 (page nested loop)

t(Tmp2) = t(Emp) × t(Tmp1) / ν(Emp,dno)

= 100,000 ×2 / 1000 = 200

P(Tmp2) = 200 / 5 = 40

Cw(Tmp2) = 40

Cost = C(б) + Cw(Tmp1) + C( ) + Cw(Tmp2)

= 100 + 1 + 10,001 + 40

= 10,142 I/O

Page 27: 2015-6-27Prof. Ghandeharizadeh1 QUERY PROCESSING (CHAPTER 12)

23/4/18Prof. Ghandeharizadeh 27

EQUALITY JOIN (Cont…)

The role of a query optimizer is to re-arrange operators of a query-tree to minimize the cost of executing a query. It employs heuristic search to compute a low-cost execution-tree:

1. Given a query plan, push the selection and projection operators down the query-tree. The system must be careful with the projection operator.

2. Combine two operators whenever possible, e.g., selection and join can be combined, projection and selection can be combined, projection and join can be combined.

3. Join is associative, consider different orderings of the join operators:

(R S) T = R (S T)– Never compute the Cartesian product of two relations. For example if the join

clause is (R.A = S.A and R.B = T.B) then it would be a mistake to use the following clause (S Cartesian product T) and R.A = ST.A and R.B = ST.B

– Always join an intermediate result with an original relation:• (((R S) T) U) is Okay• (R S) (T U) is not Okay