Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Join Techniques
Objectives
Illustrate fundamental differences between join techniques in DSS and OLTP environments. Describe performance characteristics for major join techniques found in a DSS environment. Describe resource requirements for major join techniques found in a DSS environment.
Topics
DSS vs. OLTP Queries. Nested Loop Join. Sort Merge Join. Merge Join. Hash Join. Pointer-based Join. Query Optimization.
Traditional DSS
Important but secondary issue. Minutes/hours. What if analysis. Changing data relationships. 16*6. MTTR sometimes hours. Periodic. Batch. Complex. Ad hoc. Aggregates. Joins. Many one time only queries. Rarely prime key. Secondary index where helpful. Varies. Can be very large. Answer sets often loaded in other tools for analysis Operational systems Handled in operational systems and checked by local programs or after loads complete.
= 'BRA' then 1
High selectivity based on specification of customer_id. Returns a relatively small number of rows.
Join all customers with transactions since start of year. Returns a large set of rows which will be further analyzed in subsequent queries. Aggregation of detailed data.
Nested Loop Joins. (Sort) Merge Joins. Hash Joins. Pointer-Based Joins.
T1 is referred to as the Outer table. T2 is referred to as the Inner table. Simplest form of join algorithm. No sorting required.
10
Inner table is scanned once for each qualifying row in the outer table. Indices are (almost always) essential for high performance with nested loop joins by narrowing scan of inner table. Alternatively, the RDBMS can use block nested loop joins to reduce the number of scans against the inner table.
11
Sales Detail
Search
Each qualifying row in Store table results in an interrogation of Sales Detail table for matching rows.
12
Naive nested loop join with no indices requires (R*S) I/O operations even with (simple) blocking. Nested loop join with index on inner table may require fewer I/Os, depending on index selectivity. Index selectivity, for these purposes, should be measured in terms of blocks, not rows. Access to inner table using index will usually involve random I/O operations (as opposed to sequential I/O).
Note: R and S are the number of blocks in the inner and outer tables, respectively.
13
14
Want number of I/Os to index and data blocks for inner table to be small compared to total number of blocks in inner table. Can approximate break-even analysis by calculating number of qualifying rows in outer table times the sum of the indexing cost plus the average number of inner table rows retrieved per qualifying outer table row versus total number of blocks in inner table.
15
Note: Break even point will be even more optimistic if we assume that substantial portions of the index on the inner table can be cached in memory (first term in square brackets goes to zero) or if a large sort can be avoided.
*Index depth = # of I/Os to get to the desired RID list.
16
Traditional DSS queries are not typically provided with a selection criteria using a primary or foreign key value with extreme selectivity as is found in OLTP queries. Traditional DSS queries often join tables containing (hundreds of) millions of rows to tables with (billions) tens of millions of rows in one-to-many relationships.
Access through a clustering index implies that the data file is physically ordered on the index value, so locality of reference is guaranteed to be very high when matching on the join predicate.
17
T1
Filter: age < 30
T2
Sort T1
Sort T2
Merge
Match on customer_id
R
18
Create initial runs with in-memory sorts and then merge runs into increasingly longer (and fewer) runs until a single sorted result set is produced. Need substantial disk space to manage execution of the large sort.
19
Input
...2K 2K
R/2
...Merge
...2K 2K R/2
...K K
Expected number of passes for sorting a large file is: log( R/K ) Each pass requires R reads and R writes, as well as CPU for merging sorted runs.
Note: K is the size of the output set resulting from in-memory sort (constrained by availability of main memory); R is the total number of blocks in the data set to be sorted after unneeded columns are eliminated.
20
Sort T1
Sort T2
Merge
21
Make use of in-place sorts whenever possible to avoid additional storage overhead. Amount of main memory available for sorting and merging can have a significant impact on performance.
22
Eliminate this overhead whenever the sorted result from a previous join step can be re-used to avoid this operation. May be able to avoid sort step if base tables are presorted or pre-hashed on join key.
T1
Filter
T2
Filter
Sort
Sort
Merge
Match IDs
R 23
24
Hash Joins
Join tables T1 and T2 by building an in-memory hash table from the smaller of the tables (after considering qualifying rows) and then probing the hash table for matches from the other table.
Table to be used in hash table construction is referred to as build input. Table to be used to search into hash table is referred to as probe input.
25
Hash Joins
When build input can be hashed all into main memory, performance can be quite attractive using the hash join technique.
No sorting required. No need to build any temporary files even when using very large probe input.
26
Hash Joins
When build input can be hashed all into main memory the number of I/Os is linearly proportional to the combined size of both tables: Estimate of total I/O cost for hash join: O( R+S ) Beware: Memory availability can be a major performance issue when build input is of significant size.
27
Hash Joins
When the build input has a large number of rows that must be placed into the hash table, overflow to disk must be considered.
Recursive partitioning must be used so that build input partitions can fit into memory. Partitioning needs to be performed using same splitting algorithm for both build and probe inputs. Binary hybrid hash operations are used to perform the partitioning.
28
Hash Joins
Hash-based binary matching operations work particularly well when the two input sizes are quite different because the recursion depth is proportional to the smaller of the two inputs.
Estimate of total I/O cost for hash join: O( R * log(R/K) ) + O( S * log(R/K) ) + O( R+S )
Partition T1
Partition T2
Hash Match
Assume R < S. Critical to avoid skew in hashing function results for good performance!
29
Pointer-Based Joins
Join tables T1 and T2 by tracing explicit links between related data items.
Found primarily in object databases in the form of object identifiers (OIDs). The links pre-compute the join path between data items. Cost of storing and maintaining links can be very expensive.
30
Query Optimization
Quality of query optimizer is critical in VLDB environments targeted to data warehouse applications.
Rule-Based Optimizer
31
Query Optimization
Require cost-based optimizer to make good decisions regarding join plans for sophisticated DSS queries.
Need to collect statistics on tables and individual columns within tables. Optimizer choices should be independent of table ordering in from clause, ordering or structure of where clause predicates, and so on - but reality is that many optimizers are very sensitive to these nuances. Optimizer should understand available resources within machine (e.g., CPU bound versus I/O bound machine configuration may influence optimization choices).
Note that the quality of optimizer software is a significant differentiator among database products currently on the market.
32
Bottom Line
Join technique is a dominant factor in determining the performance characteristics in a VLDB data warehouse environment. Join techniques required for a decision support environment are significantly more sophisticated than what is required in an OLTP environment. No one join technique is appropriate in all cases must rely on cost-based optimizer to determine optimal plan.
33