Theoremcore
Equivalence rules of relational algebra
Also called algebraic laws of query rewriting.
Laws such as join commutativity, associativity and the pushing of selections and projections, which let an optimizer rewrite a plan. Some hold for sets only, others for sets and bags.
The equivalence rules of relational algebra are equations between two algebra expressions that hold on every database. An optimizer uses them to rewrite a query plan into a cheaper plan with the same result: it moves selections and projections down, reorders joins and turns subqueries into joins. This page states the laws as one theorem. For each law it says whether it holds under set semantics, under bag semantics (the semantics of SQL) or under both, and it gives the side conditions, and the proof section proves each law. Predicate pushdown is the rewrite strategy that applies the selection laws of this page to a plan; that page describes the strategy, and this page holds the laws and their proofs.
Statement
Relations. A relation over a finite set of attributes is a function from tuples over to the natural numbers: is the number of copies of in . Under bag semantics any number is allowed. Under set semantics every value is 0 or 1. Write for 1 if the statement is true and 0 if it is false, and for duplicate elimination. Below, , and are relations over , and , and . A condition is TRUE or FALSE on each tuple, except in law 3, which treats the value UNKNOWN of SQL.
Operators under bag semantics. For tuples , and over the right attributes:
- , and for .
- for over . The product is the case , and .
- For and over the same attributes: , which is UNION ALL; ; and , the monus, which is EXCEPT ALL.
- , the semijoin.
Operators under set semantics. They are the same functions on relations with values 0 and 1, except that union and projection apply to the result: and of the sum above. On values 0 and 1 the monus is the set difference.
Two expressions are equivalent under a semantics if they have the same attributes and give the same relation on every database of that semantics. Attributes have names, so the order of the columns does not matter.
Theorem. The following equivalences hold under the semantics and the side conditions given.
| law | equivalence | side condition | holds under |
|---|---|---|---|
| 1 | none | both | |
| 2 | none | sets only | |
| 3 | is never UNKNOWN on a tuple of | both | |
| 4 | both | ||
| 5 | , and the same for and | reads only attributes in | both |
| 6 | for in , and | both | |
| 7 | both | ||
| 8 | reads only attributes in | both | |
| 9 | , , | both | |
| 10 | both | ||
| 11 | , and no two different tuples with a positive value in or in agree on | both | |
| 12 | , and the same for , and | none | both |
| 13 | , and the same for , and | none | both |
| 14 | , , pairwise disjoint, reads only , reads only | both | |
| 15 | both | ||
| 16 | both | ||
| 17 | none | sets only | |
| 18 | sets only | ||
| 19 | sets only | ||
| 20 | none | sets only | |
| 21 | none | both |
"Sets only" means that the law holds under set semantics and fails under bag semantics. Law 11 fails under both semantics without its side condition. Moreover (law 22, transfer): if two expressions use only , , , , and , and they are equivalent under bag semantics, then they are equivalent under set semantics.
In plain words
A database can compute the same answer in many ways. The laws on this page say which changes to a query never change the answer, on any data. Two kinds matter most. A filter can move down, closer to the tables, so that the later steps see fewer rows. And the joins can be done in any order. An optimizer applies the laws again and again to find a cheaper form of the same query.
SQL tables can hold the same row twice. Some laws that are true for sets of rows are then false. For example, if you put a list of rows together with itself and keep the copies, every row is there twice, so the result is not the list you started with. So each law here says if it is true for sets, for tables with copies, or for both.
Proof
Under set semantics, the operators , , , , and are the bag operators restricted to relations with values 0 and 1, and they give such relations again. So a proof of a law in these operators under bag semantics is also a proof under set semantics. For the laws with or and no difference, law 22 gives the set case from the bag case. The other set cases are proved directly. Below, is a tuple over , a tuple over , a tuple over , and a tuple over all attributes of the expression.
Law 22. First, commutes with each positive operator, in the sense . For selection, . For join and product, . For union, , which is the set union of and . For projection, a sum of natural numbers is positive exactly when one of its terms is positive, so , the set projection of . For intersection, . By induction on the expression, for every positive expression and every database . If is a set database, then , so . Difference does not commute with : for and , but . So the law needs the restriction to positive expressions.
Law 1. , and multiplication is commutative. In SQL a conjunction is TRUE exactly when both parts are TRUE, and selection keeps a tuple exactly when its condition is TRUE, so the law also holds with UNKNOWN.
Law 2. Under sets, and , so the left side is , which is , the right side. Under bags, take with and both true: the left side holds once and the right side twice.
Law 3. , because is TRUE or FALSE. If is UNKNOWN, then is UNKNOWN too, so is in neither nor , and the right side keeps while the left side drops it.
Law 4. This is the definition of .
Law 5. The left side at is . Because reads only attributes in , , so this equals , the right side. A join condition adds the same factor to both sides.
Law 6. and . Because is 0 or 1, , and this also equals : if the two are the same, and if both are 0. The case of under sets follows by law 22, and the other cases are among the operators that sets and bags share.
Law 7. , because gives , so each is counted once. Law 22 gives the set case.
Law 8. If , then , because reads only attributes in . So . Law 22 gives the set case.
Law 9. Write and , and let be a tuple over . Then over the with and the with . Each such pair agrees on , because and both agree with there, so it forms one tuple over with . Each such comes from exactly one pair, and . So . Since , law 7 gives . Law 22 gives the set case.
Law 10. A sum over the with of splits into two sums. Law 22 gives the set case.
Law 11. By the side condition, for each tuple over at most one tuple with has a positive value in or in . If there is none, both sides are 0 at . Otherwise both sides are . The same computation with values 0 and 1 is the set case. Without the condition the law fails under both semantics: for and over and , , but .
Laws 12 and 13. The two sides of each law are the products and , the sums or the minimums of the same numbers, grouped or ordered in another way. Multiplication, addition and the minimum are commutative and associative. Law 22 gives the case of under sets.
Law 14. By law 4 and law 5 (with on the left input ), the left side is . By law 5 and law 12 (with on the right input ), the right side is . Law 1 makes the two equal. The side conditions are what makes readable on and readable on .
Law 15. . Law 22 gives the set case.
Law 16. Because , .
Law 17. Under sets, when the values of are 0 and 1. Under bags, , which differs from when .
Law 18. Under sets, the law is the distributive law of AND over OR at each tuple. Under bags, take : the left side is copy and the right side is copies.
Law 19. Under sets, at each tuple. Under bags, take : the left side is copy and the right side is copies.
Law 20. Under bags, , so each copy of is repeated once for each match. Under sets this value is positive exactly when and some match exists, which is . For bags, take over and over : holds twice, and holds it once.
Law 21. Since , the join has the attributes , and . The sum is positive exactly when some with has , so this is . With values 0 and 1 it is the set case.
Consequences
- Predicate pushdown. Laws 1, 5, 6 and 8 move a selection below joins, unions and projections, and law 4 turns a selection over a product into a join. They hold under both semantics, so they are safe for SQL. Predicate pushdown applies them to plans, also through outer joins on the preserved side, which needs rules that are not on this page.
- Join reordering. By laws 12 and 13, all join trees over the same relations give the same result, under bags too. This is why the join ordering problem can choose the tree by cost alone. Law 14 says that a theta-join condition must move with the relations it reads.
- Narrow rows. Law 9 lets a plan drop the columns that no later operator reads, as soon as each relation is read, if it keeps the join columns.
- OR under bags. Law 2 fails under bags, but holds under both semantics for conditions that are TRUE or FALSE, because . In SQL, write the second condition with IS NOT TRUE in place of NOT .
- Semijoins. Law 21 is the rewrite of EXISTS into a join with the distinct join values of the inner table, as in subquery unnesting. Law 20 is right only after duplicate elimination, so a plan for SQL must use law 21.
- Example. Take and as bags. Then holds twice, and . Selecting gives twice, and so does , as law 5 says. is itself. holds twice and twice, which is not , as law 20 says. and , as law 21 says.
Tightness
- Sets and bags. Laws 2, 17, 18, 19 and 20 hold under sets and fail under bags, by the examples in the proof. So law 22 has no converse: a law that holds under sets need not hold under bags. Law 22 also fails for expressions with difference, by the example in its proof.
- Side conditions. Law 11 fails without its condition under both semantics. Law 3 fails when the condition can be UNKNOWN: for over and equal to , , but . In laws 5, 8 and 14 the side condition is what makes the right side defined.
- Outer joins. Joins and outer joins do not reassociate freely. For , and , LEFT JOIN is . But LEFT JOIN is , and its join with is empty, because NULL = 5 is not TRUE.
- Aggregation. No law on this page moves a grouping. That needs its own conditions, given on eager aggregation.
History
Codd (1970) defined the operations of the relational model on sets of tuples. Textbooks list the algebraic laws that optimizers use; Garcia-Molina, Ullman and Widom give them in Section 16.2 of their book, with the laws for selections, projections, joins and products and for duplicate elimination. Green, Karvounarakis and Tannen (2007) put sets and bags in one model, relations annotated by a commutative semiring, with the Boolean semiring for sets and the natural numbers for bags, and showed that positive queries commute with semiring homomorphisms. Law 22 is the case of the homomorphism from the natural numbers to the Boolean values.
References
- Hector Garcia-Molina, Jeffrey D. Ullman and Jennifer Widom, Database Systems: The Complete Book, second edition, Pearson Prentice Hall (2009), Section 16.2, Algebraic laws for improving query plans (commutative and associative laws, laws involving selection, pushing selections, laws involving projection, laws about joins and products, laws involving duplicate elimination)
- Todd J. Green, Grigoris Karvounarakis and Val Tannen, Provenance semirings, Proceedings of the 26th ACM Symposium on Principles of Database Systems (PODS), 31 to 40 (2007), K-relations, with the Boolean semiring for sets and the natural numbers for bags, and the commutation of positive queries with semiring homomorphisms
- Edgar F. Codd, A relational model of data for large shared data banks, Communications of the ACM 13(6), 377 to 387 (1970), the operations on relations
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 00WBVB Edit this page History Report an error Markdown