









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The first half of the course will emphasize taxonomies of databases. The second half will delve deeper into database theory and examine topics in theoretical and applied databases. Query Processing, Fast Joins, Parsing and Translation, Optimization, Evaluation, Query Cost, Selection Operation, Sorting, Sort-merge, Join Operation, Nested-loop, Merge-join
Typology: Slides
1 / 17
This page cannot be seen from the preview
Don't miss anything!










Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
Monday, 06 November 2006
William H. Hsu Department of Computing and Information Sciences, KSU
KSOL course page: http://snipurl.com/va Course web site: http://www.kddresearch.org/Courses/Fall-2006/CIS Instructor home page: http://www.cis.ksu.edu/~bhsu
Reading for Next Class: Second half of Chapter 13, Silberschatz et al. , 5 th^ edition
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Overview z Measures of Query Cost z Selection Operation z Sorting z Join Operation z Other Operations z Evaluation of Expressions
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Cost is generally measured as total elapsed time for answering query Many factors contribute to time cost Ö disk accesses, CPU , or even network communication z Typically disk access is the predominant cost, and is also relatively easy to estimate. Measured by taking into account Number of seeks * average-seek-cost Number of blocks read * average-block-read-cost Number of blocks written * average-block-write-cost Ö Cost to write a block is greater than cost to read a block data is read back after being written to ensure that the write was successful
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
Assume that the blocks of a relation are stored contiguously Cost estimate (number of disk blocks to be scanned): Ö cost of locating the first tuple by a binary search on the blocks ⎡ log 2 ( br ) ⎤ *** (** tT + tS ) Ö If there are multiple records satisfying selection Add transfer cost of the number of blocks containing records that satisfy selection condition Will see how to estimate this cost in Chapter 14
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Index scan – search algorithms that use an index selection condition must be on search-key of index. z A3 ( primary index on candidate key, equality ). Retrieve a single record that satisfies the corresponding equality condition Cost = ( h (^) i + 1) * ( t (^) T + t (^) S ) z A4 ( primary index on nonkey, equality) Retrieve multiple records. Records will be on consecutive blocks Ö Let b = number of blocks containing matching records Cost = h (^) i ** ( t (^) T + t (^) S ) + tS + t (^) T *** b* z A5 ( equality on search-key of secondary index). Retrieve a single record if the search-key is a candidate key Ö Cost = (hi + 1) * ( tT + tS ) Retrieve multiple records if search-key is not a candidate key Ö each of n matching records may be on a different block Ö Cost = ( hi + *n) ** ( tT + t (^) S ) Can be very expensive!
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Can implement selections of the form σ A ≤ V ( r ) or σ A ≥ V ( r ) by using a linear file scan or binary search, or by using indices in the following ways: z A6 ( primary index, comparison). (Relation is sorted on A) Ö For σ A ≥ V(r) use index to find first tuple ≥ v and scan relation sequentially from there Ö For σ A ≤ V ( r ) just scan relation sequentially till first tuple > v; do not use index z A7 ( secondary index, comparison ). Ö For σ A ≥ V(r) use index to find first index entry ≥ v and scan index sequentially from there, to find pointers to records. Ö For σ A ≤ V ( r ) just scan leaf pages of index finding pointers to records, till first entry > v Ö In either case, retrieve records that are pointed to requires an I/O for each record Linear file scan may be cheaper
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Conjunction: σ (^) θ 1 ∧ (^) θ 2 ∧... (^) θ n ( r) z A8 ( conjunctive selection using one index). Select a combination of θ i and algorithms A1 through A7 that results in the least cost for σθ i ( r). Test other conditions on tuple after fetching it into memory buffer. z A9 ( conjunctive selection using multiple-key index ). Use appropriate composite (multiple-key) index if available. z A10 ( conjunctive selection by intersection of identifiers). Requires indices with record pointers. Use corresponding index for each condition, and take intersection of all the obtained sets of record pointers. Then fetch records from file If some conditions do not have appropriate indices, apply test in memory.
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
In each pass, contiguous groups of M - 1 runs are merged. A pass reduces the number of runs by a factor of M -1, and creates runs longer by the same factor. Ö E.g. If M=11, and there are 90 runs, one pass reduces the number of runs to 9, each 10 times the size of the initial runs Repeated passes are performed till all runs have been merged into one.
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Several different algorithms to implement joins Nested-loop join Block nested-loop join Indexed nested-loop join Merge-join Hash-join z Choice based on cost estimate z Examples use the following information Number of records of customer : 10,000 depositor : 5000 Number of blocks of customer : 400 depositor : 100
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z To compute the theta join r (^) θ s for each tuple tr in r do begin for each tuple ts in s do begin test pair ( tr,ts ) to see if they satisfy the join condition θ if they do, add tr • ts to the result. end end z r is called the outer relation and s the inner relation of the join. z Requires no indices and can be used with any kind of join condition. z Expensive since it examines every pair of tuples in the two relations.
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z In the worst case, if there is enough memory only to hold one block of each relation, the estimated cost is n (^) r ∗ b (^) s + b (^) r block transfers, plus n (^) r + b (^) r seeks z If the smaller relation fits entirely in memory, use that as the inner relation. Reduces cost to b (^) r + b (^) s block transfers and 2 seeks z Assuming worst case memory availability cost estimate is with depositor as outer relation: Ö 5000 ∗ 400 + 100 = 2,000,100 block transfers, Ö 5000 + 100 = 5100 seeks with customer as the outer relation Ö 10000 ∗ 100 + 400 = 1,000,400 block transfers and 10,400 seeks z If smaller relation ( depositor) fits entirely in memory, the cost estimate will be 500 block transfers. z Block nested-loops algorithm (next slide) is preferable.
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Variant of nested-loop join in which every block of inner relation is paired with every block of outer relation. for each block Br of r do begin for each block Bs of s do begin for each tuple tr in Br do begin for each tuple ts in Bs do begin Check if ( tr,ts) satisfy the join condition if they do, add tr • ts to the result. end end end end
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Compute depositor customer, with depositor as the outer relation. z Let customer have a primary B+-tree index on the join attribute customer-name, which contains 20 entries in each index node. z Since customer has 10,000 tuples, the height of the tree is 4, and one more access is needed to find the actual data z depositor has 5000 tuples z Cost of block nested loops join 400*100 + 100 = 40,100 block transfers + 2 * 100 = 200 seeks Ö assuming worst case memory Ö may be significantly less with more memory z Cost of indexed nested loops join 100 + 5000 * 5 = 25,100 block transfers and seeks. CPU cost likely to be less than that for block nested loops join
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Can be used only for equi-joins and natural joins z Each block needs to be read only once (assuming all tuples for any given value of the join attributes fit in memory z Thus the cost of merge join is: b (^) r + b (^) s block transfers + ⎡ b (^) r / b (^) b ⎤ + ⎡ b (^) s / b (^) b ⎤ seeks + the cost of sorting if relations are unsorted. z hybrid merge-join: If one relation is sorted, and the other has a secondary B+^ -tree index on the join attribute Merge the sorted relation with the leaf entries of the B+-tree. Sort the result on the addresses of the unsorted relation’s tuples Scan the unsorted relation in physical address order and merge with previous result, to replace addresses by the actual tuples Ö Sequential scan more efficient than random lookup
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Applicable for equi-joins and natural joins. z A hash function h is used to partition tuples of both relations z h maps JoinAttrs values to {0, 1, ..., n }, where JoinAttrs denotes the common attributes of r and s used in the natural join. r 0 , r 1 ,.. ., rn denote partitions of r tuples Ö Each tuple t (^) r ∈ r is put in partition ri where i = h(tr [JoinAttrs]). r 0 ,, r 1.. ., rn denotes partitions of s tuples Ö Each tuple t (^) s ∈ s is put in partition s (^) i , where i = h(ts [JoinAttrs]).
z Note: In book, r (^) i is denoted as H (^) ri, s (^) i is denoted as H (^) s i and n is denoted as n (^) h.
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
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.
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z The value n and the hash function h is chosen such that each si should fit in memory. Typically n is chosen as ⎡bs /M⎤ * f where f is a “fudge factor”, typically around 1. The probe relation partitions s (^) i need not fit in memory z Recursive partitioning required if number of partitions n is greater than number of pages M of memory. instead of partitioning n ways, use M – 1 partitions for s Further partition the M – 1 partitions using a different hash function Use same partitioning method on r Rarely required: e.g., recursive partitioning not needed for relations of 1GB or less with memory size of 2MB, with block size of 4KB.
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z Partitioning is said to be skewed if some partitions have significantly more tuples than some others z Hash-table overflow occurs in partition si if si does not fit in memory. Reasons could be Many tuples in s with same value for join attributes Bad hash function z Overflow resolution can be done in build phase Partition s (^) i is further partitioned using different hash function. Partition r (^) i must be similarly partitioned. z Overflow avoidance performs partitioning carefully to avoid overflows during build phase E.g. partition build relation into many partitions, then combine them z Both approaches fail with large numbers of duplicates Fallback option: use block nested loops join on overflowed partitions
Computing & Information Sciences CIS 560: Database System Concepts Monday, 06 Nov 2006 Kansas State University
z If recursive partitioning is not required: cost of hash join is 3( b (^) r + bs) +4 ∗ n (^) h block transfers +
z If recursive partitioning required: number of passes required for partitioning build relation s is ⎡ logM– 1 ( bs ) – 1⎤ best to choose the smaller relation as the build relation. Total cost estimate is: 2 (br + bs ⎡ logM– 1 ( bs ) – 1⎤ + br + bs block transfers + 2 (⎡ br / bb ⎤ + ⎡ bs / bb ⎤) ⎡ logM– 1 ( bs ) – 1⎤ seeks z If the entire build input can be kept in main memory no partitioning is required Cost estimate goes down to b (^) r + bs.