Docsity

Database Query Processing: Lecture 17 - Query Processing Techniques - Prof. Mihai Pop, Study notes of Principles of Database Management

Partial preview of the text

Download Database Query Processing: Lecture 17 - Query Processing Techniques - Prof. Mihai Pop and more Study notes Principles of Database Management in PDF only on Docsity!

CMSC 424 – Database design

Lecture 17

Query processing

Mihai Pop

Admin

• Homework 3 is on the website

• Project part 1 due

• Midterm answers/results

> 90 – A (6)

75-90 – B (9)

50-75 – C (6)

< 50 – D (1)

• 10 improved, 10 got worse, 2 about the same

Complex Selections

• conjunction s1 = # of tuples satisfying

s2 = # of tuples satisfying

combined SC =

assuming independence of predicates

• disjunction

combined SC =

this 1 minus the probability of all predicates are satisfied at

once (s1/n(r) + s2/n(r) – s1/n(r)*s2/n(r))- union of results

• negation

n( ) = n(r) - n( )

s1 ∗ s2 / nr ∗ nr   1 ∨ 2 1 − 1 − s1 / nr ∗ 1 − s2 / nr  ¬

σ ¬ θ ( r ) σ^ θ ( r )

θ 1 θ 2

1 ∧ 2

Multiple Index Selection

GOAL: apply the most restrictive one and combine multiple of them to

reduce the intermediate results AS EARLY AS POSSIBLE

  • (^) conjunctive selection using one index A: select using A and then

apply the remaining of the predicates on the retrieved tuple values

  • (^) conjunctive selection using a composite key index (R.A,R.B)- then

create a composite key or range from the query values and search

directly (range search on the first attribute only)

  • (^) conjunctive selection using two indexes A and B: search each

separately and intersect the tuple identifiers (TIDs)

  • (^) disjunctive selection using two indexes A and B: search each

separately and take the union of the TIDs

Cost of Block-Oriented Nested Loop

• cost depends on the number of buffers and the buffer

replacement strategy

– fasten 1 block from the outer relation, M for the inner

and LRU

cost: b(r) + b(r)*b(s) assuming that b(s) > M

– fasten M blocks from the outer relation, and 1 for the

inner

1: read M from the outer cost: M blocks

2: for each block of s join 1 X M blocks cost: b(s) -”-

3: repeat with the next M blocks of r until all done

repeated b(r)/M times

cost = [ (M + b(s)]* b(r)/M = b(r)+[b(r)*b(s)]/M

• which relation should be the outer?

Buffer size M+

Join Methods: Sort-Merge-Join

• two phases

– sorting phase: sort both relations (this can be done in

parallel)

– merging phase: join tuples during the merge

• cost with M buffers

cost = b r ( 2 log M – ( b r / M)  + 1) + b r

  • b s ( 2 log M – ( b s / M)  + 1) + b s
  • b r
  • b s sort R on joining attribute sort S on joining attribute merge(sorted-R,sorted-S) Sorting R (^) Sorting S Merge Write sorted WriteR sorted S if one pass is required the expressionslog M –1( br/M)=1 andlog M –1( bs/M)=1 so the total cost is 3b(r)+b(r)+b(r)+ 3b(s)+b(s)+b(s) =5b(r)+ 5b(s) However, if M> b(r) and b(s) then the expression evaluates to 3b(r)+3b(s)).**

Hash-Join Algorithm Details

1. Partition the relation s using hashing function h. When

partitioning a relation, one block of memory is reserved as

the output buffer for each partition.

2. Partition r similarly.

3. For each i:

(a) Load s

i

into memory and build an in-memory hash index

on it using the join attribute. This hash index uses a

different hash function than the earlier one h.

(b)Read the tuples in r

i

from the disk one by one. For each

tuple t

r

locate each matching tuple t

s

in s

i

using the in-

memory hash index. Output the concatenation of their

attributes.

The hash-join of r and s is computed as follows. Relation s is called the build input and r is called the probe input.

Example of Cost of Hash-Join

• M= 20 blocks

• b

depositor

• b

customer

• depositor is the build input. Partition it into 5 partitions, each of

size 20 blocks. This partitioning can be done in one pass.

• partition customer into 5 partitions,each of size 80. This is also

done in one pass.

• Do the partition joins- for each j put 20 blocks of partition

depositor(j) in memory, built the hash index, and do the probes

with the 80 blocks of customer(j)

• Therefore total cost, ignoring cost of writing partially filled

blocks:

– 3(100 + 400) = 1500 I/Os

customer depositor

Join Methods: Indexed-Join

• inner relation has an index (clustering or not)

• cost = b(r) + n(r)* cost(σ(S.A=c))

where cost(σ(S.A=c)) is as computed for indexed selection

for each block b(r) in r do begin for each tuple t(r) in b(r) do begin search the index B on s with the value t.A of the joining attr. A and join(t(r),t.A) end end

Estimation of Join Size:

  • (^) 0 <= <= n(r)*n(s) (0 when nothing joins and when everything joins)
    • if joining attribute is a key of R then <= n(s) /* each value of S.A would join to at most one value of R.A */
    • (^) if -”- is a key of R and a foreign key of S then = n(s) /* each value of S.A would join to exactly one value of R.A */
    • (^) if is not a key then each value of A in R appears n(s)/V(A,s) times in S, therefore, n(r) tuples of R produce: = n(r)n(s) / V(A,s) symmetrically we can obtain: = n(r)n(s) / V(A,r) if the values are different we use: min{n(r)n(s) / V(A,s) , n(r)n(s) / V(A,r)} n(R S) n(R S) n(R S) n(R S) n( R X S ) n(R S) n(R S) n(R S) n(R S) n(R S)

External Sorting with Sort-Merge

●external vs internal sorting: relation/file does not fit in memory ●create runs phase: ●merge-runs phase: ● ● (^) this assumes that a block from each run can be kept in main memory. If not, then the same algorithm has to be applied in multiple passes repeat until done read M blocks of the relation (or rest if <=M) internal sort using any sort method, e.g QuickSort(M) write the sorted tuples into a run R data file end read one block from each run; merge tuples on the result; advance the pointer from the run you appended last; if the block of a run is empty, read the next one until all blocks of all runs are done

External Merge Sort Cost

  • (^) Cost analysis:
    • Initial number of runs: br /M
    • Total number of merge passes required: log M –1( br/M) .
    • Block transfers for initial run creation is br + br =2 br
      • (^) for final pass, we don’t count write cost we ignore final write cost for all operations since the output of an operation may be pipelined to the display or to a parent operation without being written to disk. If pipelined, it will be counted in the cost of the follow up operator
      • (^) Thus total number of block transfers for external sorting: 2 br ( log M –1( br / M) ) + br = br ( 2 log M –1( br / M)  + 1)
    • If M ≥  br/M  (only one pass is required) the expression log M –1( br/M)  = total cost = 3 b r
    • However, if M >^ br then this expression evaluates to 0 total cost = b r

ONLY