Extended Operators in SQL and Relational Algebra
Total Page:16
File Type:pdf, Size:1020Kb
Bags Versus Sets Extended Operators Joins Extended Operators in SQL and Relational Algebra T. M. Murali September 16, 2009 T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra I Performance is one of the main reasons; duplicate elimination is expensive since it requires sorting. I If we use bag semantics, we may have to redefine the meaning of each relation algebra operator. Bags Versus Sets Extended Operators Joins Bags or Sets? I So far, we have said that relational algebra and SQL operate on relations that are sets of tuples. I Real RDBMSs treat relations as bags of tuples. I A tuple can appear multiple times in a relation. T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra duplicate elimination is expensive since it requires sorting. I If we use bag semantics, we may have to redefine the meaning of each relation algebra operator. Bags Versus Sets Extended Operators Joins Bags or Sets? I So far, we have said that relational algebra and SQL operate on relations that are sets of tuples. I Real RDBMSs treat relations as bags of tuples. I A tuple can appear multiple times in a relation. I Performance is one of the main reasons; T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra Bags Versus Sets Extended Operators Joins Bags or Sets? I So far, we have said that relational algebra and SQL operate on relations that are sets of tuples. I Real RDBMSs treat relations as bags of tuples. I A tuple can appear multiple times in a relation. I Performance is one of the main reasons; duplicate elimination is expensive since it requires sorting. I If we use bag semantics, we may have to redefine the meaning of each relation algebra operator. T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra R πA;B (R) σC≥3(R) A B C A B A B C 1 2 3 1 2 1 2 3 1 2 4 1 2 1 2 4 2 3 4 2 3 2 3 4 2 3 4 2 3 2 3 4 Bags Versus Sets Extended Operators Joins Bag Semantics: Projection and Selection I Projection (π()): process each tuple independently; a tuple may appear in the resulting relation multiple times. I Selection (σ()): process each tuple independently; a tuple may appear in the resulting relation multiple times. T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra πA;B (R) σC≥3(R) A B A B C 1 2 1 2 3 1 2 1 2 4 2 3 2 3 4 2 3 2 3 4 Bags Versus Sets Extended Operators Joins Bag Semantics: Projection and Selection I Projection (π()): process each tuple independently; a tuple may appear in the resulting relation multiple times. I Selection (σ()): process each tuple independently; a tuple may appear in the resulting relation multiple times. R A B C 1 2 3 1 2 4 2 3 4 2 3 4 T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra σC≥3(R) A B A B C 1 2 1 2 3 1 2 1 2 4 2 3 2 3 4 2 3 2 3 4 Bags Versus Sets Extended Operators Joins Bag Semantics: Projection and Selection I Projection (π()): process each tuple independently; a tuple may appear in the resulting relation multiple times. I Selection (σ()): process each tuple independently; a tuple may appear in the resulting relation multiple times. R πA;B (R) A B C 1 2 3 1 2 4 2 3 4 2 3 4 T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra σC≥3(R) A B C 1 2 3 1 2 4 2 3 4 2 3 4 Bags Versus Sets Extended Operators Joins Bag Semantics: Projection and Selection I Projection (π()): process each tuple independently; a tuple may appear in the resulting relation multiple times. I Selection (σ()): process each tuple independently; a tuple may appear in the resulting relation multiple times. R πA;B (R) A B C A B 1 2 3 1 2 1 2 4 1 2 2 3 4 2 3 2 3 4 2 3 T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra A B C 1 2 3 1 2 4 2 3 4 2 3 4 Bags Versus Sets Extended Operators Joins Bag Semantics: Projection and Selection I Projection (π()): process each tuple independently; a tuple may appear in the resulting relation multiple times. I Selection (σ()): process each tuple independently; a tuple may appear in the resulting relation multiple times. R πA;B (R) σC≥3(R) A B C A B 1 2 3 1 2 1 2 4 1 2 2 3 4 2 3 2 3 4 2 3 T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra Bags Versus Sets Extended Operators Joins Bag Semantics: Projection and Selection I Projection (π()): process each tuple independently; a tuple may appear in the resulting relation multiple times. I Selection (σ()): process each tuple independently; a tuple may appear in the resulting relation multiple times. R πA;B (R) σC≥3(R) A B C A B A B C 1 2 3 1 2 1 2 3 1 2 4 1 2 1 2 4 2 3 4 2 3 2 3 4 2 3 4 2 3 2 3 4 T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra A B 1 2 1 2 1 2 1 2 1 2 2 3 2 3 2 3 2 4 A B 1 2 A B 1 2 2 3 2 3 R [ S R \ S R − S S R A B A B 1 2 1 2 1 2 1 2 1 2 2 3 I R \ S: if tuple t appears k times 2 3 2 3 in R and l times in S, t appears 2 4 in R \ S minfk; lg times. I R − S: if tuple t appears k times in R and l times in S, t appears in R − S maxf0; k − lg times. Bags Versus Sets Extended Operators Joins Bag Semantics: Union, Intersection, and Difference I R [ S: if tuple t appears k times in R and l times in S, t appears in R [ S k + l times. T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra A B 1 2 A B 1 2 2 3 2 3 A B 1 2 1 2 1 2 1 2 1 2 I R \ S: if tuple t appears k times 2 3 in R and l times in S, t appears 2 3 in R \ S minfk; lg times. 2 3 2 4 I R − S: if tuple t appears k times in R and l times in S, t appears R \ S in R − S maxf0; k − lg times. R − S Bags Versus Sets Extended Operators Joins Bag Semantics: Union, Intersection, and Difference R [ S S R A B I R [ S: if tuple t appears k A B 1 2 times in R and l times in S, t 1 2 1 2 appears in R [ S k + l times. 1 2 1 2 2 3 2 3 2 3 2 4 T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra A B 1 2 A B 1 2 2 3 2 3 I R \ S: if tuple t appears k times in R and l times in S, t appears in R \ S minfk; lg times. I R − S: if tuple t appears k times in R and l times in S, t appears R \ S in R − S maxf0; k − lg times. R − S Bags Versus Sets Extended Operators Joins Bag Semantics: Union, Intersection, and Difference R [ S A B S R 1 2 A B 1 2 I R [ S: if tuple t appears k A B 1 2 1 2 times in R and l times in S, t 1 2 1 2 1 2 appears in R [ S k + l times. 1 2 1 2 1 2 2 3 2 3 2 3 2 3 2 4 2 3 2 3 2 4 T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra A B 2 3 I R − S: if tuple t appears k times in R and l times in S, t appears in R − S maxf0; k − lg times. A B R − S 1 2 1 2 2 3 Bags Versus Sets Extended Operators Joins Bag Semantics: Union, Intersection, and Difference R [ S A B S R 1 2 A B 1 2 I R [ S: if tuple t appears k A B 1 2 1 2 times in R and l times in S, t 1 2 1 2 1 2 appears in R [ S k + l times. 1 2 1 2 1 2 2 3 I R \ S: if tuple t appears k times 2 3 2 3 2 3 in R and l times in S, t appears 2 4 2 3 in R \ S minfk; lg times. 2 3 2 4 R \ S T. M. Murali September 16, 2009 Extended Operators in SQL and Relational Algebra A B 2 3 I R − S: if tuple t appears k times in R and l times in S, t appears in R − S maxf0; k − lg times.