External Joins Query Optimization 10/4/2017

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Evaluation of Relational Operations Chapter 12, Part A.
Advertisements

Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
Selinger Optimizer Lecture 10 October 15, 2009 Sam Madden.
6.830 Lecture 9 10/1/2014 Join Algorithms. Database Internals Outline Front End Admission Control Connection Management (sql) Parser (parse tree) Rewriter.
Lecture 8 Join Algorithms. Intro Until now, we have used nested loops for joining data – This is slow, n^2 comparisons How can we do better? – Sorting.
CS 540 Database Management Systems
Join Processing in Databases Systems with Large Main Memories
6.830 Lecture 10 Query Optimization 10/6/2014. Selinger Optimizer Algorithm algorithm: compute optimal way to generate every sub-join: size 1, size 2,...
Lecture 10 Query Optimization II Automatic Database Design.
6.830 Lecture 11 Query Optimization & Automatic Database Design 10/8/2014.
CS CS4432: Database Systems II Operator Algorithms Chapter 15.
Database Management Systems 3ed, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 14, Part B.
1  Simple Nested Loops Join:  Block Nested Loops Join  Index Nested Loops Join  Sort Merge Join  Hash Join  Hybrid Hash Join Evaluation of Relational.
Query Optimization 3 Cost Estimation R&G, Chapters 12, 13, 14 Lecture 15.
Introduction to Database Systems 1 Join Algorithms Query Processing: Lecture 1.
1 External Sorting for Query Processing Yanlei Diao UMass Amherst Feb 27, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Evaluation of Relational Operations. Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation.
CSE 444: Lecture 24 Query Execution Monday, March 7, 2005.
Lecture 11 Main Memory Databases Midterm Review. Time breakdown for Shore DBMS Source: “OLTP Under the Looking Glass”, SIGMOD 2008 Systematically removed.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 14 – Join Processing.
CPS216: Advanced Database Systems Notes 07:Query Execution Shivnath Babu.
Status “Lifetime of a Query” –Query Rewrite –Query Optimization –Query Execution Optimization –Use cost-estimation to iterate over all possible plans,
Lecture 9 Query Optimization.
Lecture 5 Cost Estimation and Data Access Methods.
6.830 Lecture 6 9/28/2015 Cost Estimation and Indexing.
CS411 Database Systems Kazuhiro Minami 11: Query Execution.
Lecture 24 Query Execution Monday, November 28, 2005.
CS4432: Database Systems II Query Processing- Part 2.
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations – Join Chapter 14 Ramakrishnan and Gehrke (Section 14.4)
Query Processing CS 405G Introduction to Database Systems.
CS 440 Database Management Systems Lecture 5: Query Processing 1.
More Optimization Exercises. Block Nested Loops Join Suppose there are B buffer pages Cost: M + ceil (M/(B-2))*N where –M is the number of pages of R.
Relational Operator Evaluation. overview Projection Two steps –Remove unwanted attributes –Eliminate any duplicate tuples The expensive part is removing.
Implementation of Database Systems, Jarek Gryz1 Evaluation of Relational Operations Chapter 12, Part A.
CS 540 Database Management Systems
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Evaluation of Relational Operations Chapter 14, Part A (Joins)
CS4432: Database Systems II Query Processing- Part 1 1.
CS 540 Database Management Systems
CS 440 Database Management Systems
Database Systems (資料庫系統)
Database Applications (15-415) DBMS Internals- Part VII Lecture 16, October 25, 2016 Mohammad Hammoud.
Lecture 16: Relational Operators
Evaluation of Relational Operations
Column Stores Join Algorithms 10/2/2017
Database Management Systems (CS 564)
Evaluation of Relational Operations: Other Operations
Database Management Systems (CS 564)
Lecture#12: External Sorting (R&G, Ch13)
Database Applications (15-415) DBMS Internals- Part VII Lecture 19, March 27, 2018 Mohammad Hammoud.
CS222P: Principles of Data Management Notes #12 Joins!
Yan Huang - CSCI5330 Database Implementation – Access Methods
CS222: Principles of Data Management Notes #12 Joins!
Selected Topics: External Sorting, Join Algorithms, …
Chapters 15 and 16b: Query Optimization
Lecture 2- Query Processing (continued)
Slides adapted from Donghui Zhang, UC Riverside
CS505: Intermediate Topics in Database Systems
Lecture 23: Query Execution
Evaluation of Relational Operations: Other Techniques
Overview of Query Evaluation: JOINS
External Sorting.
Sorting We may build an index on the relation, and then use the index to read the relation in sorted order. May lead to one disk block access for each.
CSE 444: Lecture 25 Query Execution
Lecture 22: Query Execution
Database Systems (資料庫系統)
Lecture 11: B+ Trees and Query Execution
Evaluation of Relational Operations: Other Techniques
CS222/CS122C: Principles of Data Management UCI, Fall Notes #11 Join!
Lecture 20: Query Execution
CS222P: Principles of Data Management UCI, Fall 2018 Notes #11 Join!
Presentation transcript:

External Joins Query Optimization 10/4/2017 6.830 Lecture 9 External Joins Query Optimization 10/4/2017

Join Algo Summary Algo I/O cost CPU cost In Mem? Nested loops |R|+|S| O({R}x{S}) R in mem {S}|R| + |S| No Index nested loops (R index) |S| + {S}c (c = 1 or 2) O({S}log{R}) Block nested loops |S| + B|R| (B=|S|/M) Sort-merge O({S}log{S}) Both Hash (Hash R) O({S} + {R}) Blocked hash (Hash S) O({S} + B{R}) External Sort-merge 3(|R| + |S|) O(P x {S}/P log {S}/P) Simple hash Grace hash

Sort Merge Join Equi-join of two tables S & R |S| = Pages in S; {S} = Tuples in S |S| ≥ |R| M pages of memory; M > sqrt(|S|) Algorithm: Partition S and R into memory sized sorted runs, write out to disk Merge all runs simultaneously Why M > sqrt(|S|)? If each run is M pages and M > sqrt(|S|), then there are at most |S|/M <= |S|/sqrt(|S|) = sqrt(|S|) runs of S So if |R| = |S|, we actually need M to be 2 x sqrt(|S|) [handwavy argument in paper for why it’s only sqrt(|S|)] Total I/O cost: Read |R| and |S| twice, write once 3(|R| + |S|) I/Os

Simple Hash Algorithm: Given hash function H(x)  [0,…,P-1] (e.g., x mod P) where P is number of partitions for i in [0,…,P-1]: for each r in R: if H(r)=i, add r to in memory hash otherwise, write r back to disk in R’ for each s in S: if H(s)=i, lookup s in hash, output matches otherwise, write s back to disk in S’ replace R with R’, S with S’

Simple Hash I/O Analysis Suppose P=2, and hash uniformly maps tuples to partitions Read |R| + |S| Write 1/2 (|R| + |S|) Read 1/2 (|R| + |S|) = 2 (|R| + |S|) P=3 Write 2/3 (|R| + |S|) Read 2/3 (|R| + |S|) Write 1/3 (|R| + |S|) Read 1/3 (|R| + |S|) = 3 (|R| + |S|) P=4 |R| + |S| + 2 * (3/4 (|R| + |S|)) + 2 * (2/4 (|R| + |S|)) + 2 * (1/4 (|R| + |S|)) = 4 (|R| + |S|)  P = n ; n * (|R| + |S|) I/Os

Grace Hash Need one page of RAM for each of P partitions Since Algorithm: Partition: Suppose we have P partitions, and H(x)  [0…P-1] Choose P = |S| / M  P ≤ sqrt(|S|) //may need to leave a little slop for imperfect hashing Allocate P 1-page output buffers, and P output files for R For each r in R: Write r into buffer H(r) If buffer full, append to file H(r) Allocate P output files for S For each s in S: Write s into buffer H(s) if buffer full, append to file H(s) Join: For i in [0,…,P-1] Read file i of R, build hash table Scan file i of S, probing into hash table and outputting matches Need one page of RAM for each of P partitions Since M > sqrt(|S|) and P ≤ sqrt(|S|), all is well Total I/O cost: Read |R| and |S| twice, write once 3(|R| + |S|) I/Os

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 F0 F1 F2 P output buffers P output files

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 5 F0 F1 F2

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 4 5 F0 F1 F2

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 3 4 5 F0 F1 F2

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 3 4 5 6 F0 F1 F2

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 3 4 5 6 F0 F1 F2 Need to flush R0 to F0!

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 4 5 F0 F1 F2 3 6

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 9 4 5 F0 F1 F2 3 6

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 9 4 5 14 F0 F1 F2 3 6

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 9 4 5 1 14 F0 F1 F2 3 6

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 9 4 5 1 14 F0 F1 F2 3 6

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 9 5 14 F0 F1 F2 3 4 6 1

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 9 7 5 14 F0 F1 F2 3 4 6 1

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 9 7 5 14 F0 F1 F2 3 4 6 1

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 9 7 F0 F1 F2 3 4 5 6 1 14

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 9 7 11 F0 F1 F2 3 4 5 6 1 14

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R0 R1 R2 F0 F1 F2 3 4 5 6 1 14 9 7 11

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6 Load F0 from R into memory

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6 Load F0 from R into memory Scan F0 from S

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: 3,3 R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6 Load F0 from R into memory Scan F0 from S

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: 3,3 R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6 Load F0 from R into memory Scan F0 from S

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: 3,3 9,9 R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6 Load F0 from R into memory Scan F0 from S

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: 3,3 9,9 R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6 Load F0 from R into memory Scan F0 from S

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: 3,3 9,9 6,6 R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6 Load F0 from R into memory Scan F0 from S

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: 3,3 9,9 6,6 R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: 3,3 9,9 6,6 7,7 4,4 R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6

Example P = 3; H(x) = x mod P R=5,4,3,6,9,14,1,7,11 S=2,3,7,12,9,8,4,15,6 Matches: 3,3 9,9 6,6 7,7 4,4 R Files S Files F0 F1 F2 3 4 5 6 1 14 9 7 11 F0 F1 F2 3 7 2 12 4 8 9 15 6

Summary Notation: P partitions / passes over data; assuming hash is O(1) Sort-Merge Simple Hash Grace Hash I/O: 3 (|R| + |S|) CPU: O(P x {S}/P log {S}/P) I/O: P (|R| + |S|) CPU: O({R} + {S}) I/O: 3 (|R| + |S|) Grace hash is generally a safe bet, unless memory is close to size of tables, in which case simple can be preferable Extra cost of sorting makes sort merge unattractive unless there is a way to access tables in sorted order (e.g., a clustered index), or a need to output data in sorted order (e.g., for a subsequent ORDER BY)

Join Algo Summary Algo I/O cost CPU cost In Mem? Nested loops |R|+|S| O({R}x{S}) R in mem {S}|R| + |S| No Index nested loops (R index) |S| + {S}c (c = 1 or 2) O({S}log{R}) Block nested loops |S| + B|R| (B=|S|/M) Sort-merge O({S}log{S}) Both Hash (Hash R) O({S} + {R}) Blocked hash (Hash S) O({S} + B{R}) (*) External Sort-merge 3(|R| + |S|) O(P x {S}/P log {S}/P) Simple hash P(|R|+|S|) (P=|S|/M) O({R} + {S}) Grace hash Grace hash is generally a safe bet, unless memory is close to size of tables, in which case simple can be preferable Extra cost of sorting makes sort merge unattractive unless there is a way to access tables in sorted order (e.g., a clustered index), or a need to output data in sorted order (e.g., for a subsequent ORDER BY) (*) Note that this was changed on 10/14/2016 from O({S} + B{R}) to O({S} + {R})

Database Internals Outline Front End Admission Control Connection Management Query System (sql) Parser (parse tree) Rewriter Planner & Optimizer (query plan) Executor This time Storage System Access Methods Lock Manager Buffer Manager Log Manager

Selinger Optimizer Algorithm algorithm: compute optimal way to generate every sub-join: size 1, size 2, ... n (in that order) e.g. {A}, {B}, {C}, {AB}, {AC}, {BC}, {ABC} R set of relations to join For i in {1...|R|}: for S in {all length i subsets of R}: optjoin(S) = a join (S-a), where a is the relation that minimizes: cost(optjoin(S-a)) + min. cost to join (S-a) to a + min. access cost for a Precomputed in previous iteration!

Selinger, as code R set of relations to join For i in {1...|R|}: for S in {all length i subsets of R}: optcosts = ∞ optjoinS = ø for a in S: //a is a relation csa = optcosts-a + min. cost to join (S-a) to a + min. access cost for a if csa < optcosts : optcosts = csa optjoins = optjoin(S-a) joined optimally w/ a This is the same algorithm as previous one, written differently Pre-computed in previous iteration!

Example 4 Relations: ABCD (only consider NL join) Optjoin: A = best way to access A (e.g., sequential scan, or predicate pushdown into index...) B = " " " " B C = " " " " C D = " " " " D {A,B} = AB or BA {A,C} = AC or CA {B,C} = BC or CB {A,D} {B,D} {C,D} R set of relations to join For i in {1...|R|}: for S in {all length i subsets of R}: optjoin(S) = a join (S-a), where a is the relation that minimizes: cost(optjoin(S-a)) + min. cost to join (S-a) to a + min. access cost for a Optjoin

Example (con’t) Optjoin R set of relations to join For i in {1...|R|}: for S in {all length i subsets of R}: optjoin(S) = a join (S-a), where a is the relation that minimizes: cost(optjoin(S-a)) + min. cost to join (S-a) to a + min. access cost for a Optjoin Optjoin {A,B,C} = remove A: compare A({B,C}) to ({B,C})A remove B: compare ({A,C})B to B({A,C}) remove C: compare C({A,B}) to ({A,B})C {A,C,D} = … {A,B,D} = … {B,C,D} = … … {A,B,C,D} = remove A: compare A({B,C,D}) to ({B,C,D})A remove B: compare B({A,C,D}) to ({A,C,D})B remove C: compare C({A,B,D}) to ({A,B,D})C remove D: compare D({A,C,C}) to ({A,B,C})D

Complexity Number of subsets of set of size n = |power set of n| = 2n (here, n is number of relations) How much work per subset? Have to iterate through each element of each subset, so this at most n n2n complexity (vs n!) n=12  49K vs 479M R set of relations to join For i in {1...|R|}: for S in {all length i subsets of R}: optjoin(S) = a join (S-a), where a is the relation that minimizes: cost(optjoin(S-a)) + min. cost to join (S-a) to a + min. access cost for a Optjoin (string of length n, 0 if element is in, 1 if it is out; clearly, 2^n such strings)