























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























User select * from R, S where … R, B+Tree on R.a S, Hash Index on S.a … Results
Resolve the references, Syntax errors etc. Converts the query to an internal format relational algebra like
Find the best way to evaluate the query Which index to use? What join method to use? …
Read the data from the files Do the query processing joins, selections, aggregates …
Read the relation start to end and look for “123” Can always be used (not true for the other options) Cost? Let br = Number of relation blocks Then: 1 seek and br block transfers So: tS + br * tT sec Improvements: If SSN is a key, then can stop when found So on average, br/2 blocks accessed
Pre-condition: The relation is sorted on SSN Selection condition is an equality E.g. can’t apply to “ Name like ‘%424%’” Do binary search Cost of finding the first tuple that matches log 2 ( br) * ( tT + tS ) All I/Os are random, so need a seek for all The last few are closeby, but we ignore such small effects Not quite: What if 10000 tuples match the condition? Incurs additional cost
Retrieve all the tuples that match by following the pointers If primary index, the relation is sorted by the search key Go to the relation and read blocks sequentially If secondary index, must follow all pointers using the index
n * (tT + tS) n = number of records that match This can be bad secondary index, not a hi * (tT + tS) key, equality secondary index, hi * (tT + tS) 1 * (tT + tS) candidate key, equality 1 * (tT + tS) + (b – 1) * tT Note: primary == sorted b = number of pages that contain the matches primary index, not a key, hi * (tT + tS) equality primary index, candidate hi * (tT + tS) 1 * (tT + tS) key, equality
hi = height of the index
Complex selections Conjunctive: select * from accounts where balance > 100000 and SSN = “123” Disjunctive: select * from accounts where balance > 100000 or SSN = “123” Option 4: Conjunction or disjunction of record identifiers Use indexes to find all RIDs that match each of the conditions Do an intersection (for conjunction) or a union (for disjunction) Sort the records and fetch them in one shot Called “Index-ANDing” or “Index-ORing” Heavily used in commercial systems
Called an “ equi-join”
Not an “equi-join”
As opposed to some algorithms we will see later
One to hold the current R block, one for current S block, one for the result being produced Blocks transferred: Must scan R tuples once: br For each R tuple, must scan S: nr * bs Seeks? nr + br
Block at a time for each block Br in R for each block Bs in S for each tuple r in Br for each tuple s in Bs check if r.a = s.a (or whether |r.a – s.a| < 0.5)
Blocks transferred: br * bs + br Seeks: 2 * br
blocks: 40400, seeks: 800
Case 1: Minimum memory required = 3 blocks
Case 2: S fits in memory
What about in between?
We only have to read S a total of br/48 times (instead of br times) Blocks transferred: br * bs / 48 + br Seeks: 2 * br / 48
Can always be applied irrespective of the join condition If the smaller relation fits in memory, then cost: br + bs This is the best we can hope if we have to read the relations once each CPU cost of the inner loop is high Typically used when the smaller relation is really small (few tuples) and index nested-loops can’t be used
Only applies if an appropriate index exists Very useful when we have selections that return small number of tuples select balance from customer, accounts where customer.name = “j. s.” and customer.SSN = accounts.SSN
CPU cost is much better (even though we don’t care about it too much) Performs much better than nested-loops join when S doesn’t fit in memory (next)
Read the relation R block by block and partition it using a hash function, h1(a) Create one partition for each possible value of h1(a) Write the partitions to disk R gets partitioned into R1, R2, …, Rk Similarly, read and partition S, and write partitions S1, S2, …, Sk to disk Only requirement: Each S partition fits in memory
Say S = 10000 blocks, Memory = M = 100 blocks Use a hash function that hashes to 100 different values? Eg. h1(a) = a % 100? Problem: Impossible to guarantee uniform split Some partitions will be larger than 100 blocks, some will be smaller Use a hash function that hashes to 100*f different values f is called fudge factor, typically around 1. So we may consider h1(a) = a % 120. This is okay IF a is uniformly distributed
Memory required? Say S = 10000 blocks, Memory = M = 100 blocks So 120 different partitions During phase 1: Need 1 block for storing R Need 120 blocks for storing each partition of R So must have at least 121 blocks of memory We only have 100 blocks Typically need SQRT(|S| * f) blocks of memory So if S is 10000 blocks, and f = 1.2, need 110 blocks of memory If memory = 10000 blocks = 10000 * 4 KB = 40MB (not unreasonable) Then, S can be as large as 10000*10000/1.2 blocks = 333 GB
What if we don’t have enough memory? Recursive Partitioning Rarely used, but can be done What if the hash function turns out to be bad? We used h1(a) = a % 100 Turns out all values of a are multiple of 100 So h1(a) is always = 0 Called hash-table overflow Overflow avoidance: Use a good hash function Overflow resolution: Repartition using a different hash function
Can always be applied irrespective of the join condition
Only applies if an appropriate index exists Very useful when we have selections that return small number of tuples select balance from customer, accounts where customer.name = “j. s.” and customer.SSN = accounts.SSN
Join algorithm of choice when the relations are large Only applies to equi-joins (since it is hash-based)
An optimization on hash join that is always implemented
Step:
_1. Compare the tuples at pr and ps
br + bs block transfers, some seeks depending on memory size
Then sort the relations first In many cases, still very good performance Typically comparable to hash join
E.g. duplicate elimination
Can always be applied irrespective of the join condition
Only applies if an appropriate index exists
Join algorithm of choice when the relations are large
An optimization on hash join that is always implemented
Very commonly used – especially since relations are typically sorted Sorted results commonly desired at the output To answer group by queries, for duplicate elimination, because of ASC/DSC