CPSC-608 Database Systems Fall 2018 Instructor: Jianer Chen Office: HRBB 315C Phone: 845-4259 Email: chen@cse.tamu.edu Notes #16
parse tree-lqp convertor Query Optimization An input database program P Prepare a collection A of efficient algorithms for operations in relational algebra; parser View processing, Semantic checking parse tree preprocessing parse tree parse tree-lqp convertor logic query plan push selections, group joins apply logic laws logic query plan reduce the size of intermediate results Optimization via logic and size logic query plan Lqp-pqp convertor take care of issues in optimization and security. physical query plan choices of algorithms, data structures, and computational modes Optimization via algorithms and cost Machine executable code
Improving logic plan via logic laws Major Steps: Move selections σ so they can be applied early (σ reduces table size); Combine cross product × with selections σ to make natural joins and theta joins ( has more efficient algorithms); 3. group commutative and associative binary operations (e.g., ∩, U, ) (for later opt.); 4. May consider other operations (e.g., π, δ, τ, γ) C
Improving logic plan via logic laws Major Steps: Move selections σ so they can be applied early (σ reduces table size); Combine cross product × with selections σ to make natural joins and theta joins ( has more efficient algorithms); 3. group commutative and associative binary operations (e.g., ∩, U, ) (for later opt.); 4. May consider other operations (e.g., π, δ, τ, γ) C
Improving logic plan via logic laws Major Steps: Move selections σ so they can be applied early (σ reduces table size); Why is reducing table size important?
Improving logic plan via logic laws Major Steps: Move selections σ so they can be applied early (σ reduces table size); Why is reducing table size important? Tables are large, the intermediate tables can be even larger, so they may have to be written back to disks. Reducing table size will save, or even avoid disk reads/writes.
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R))
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that has all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R))
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that has all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Take the tuples that are either in R or in S and satisfy the condition C
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that has all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Take the tuples that are either in R or in S and satisfy the condition C Take the tuples that are either in R and satisfy the condition C or in U and satisfy the condition C
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that has all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Take the tuples that are either in R or in S and satisfy the condition C Take the tuples that are either in R and satisfy the condition C or in U and satisfy the condition C
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that has all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) R(a,b) S(b,d) σ C ⋃ σ C σ C R(a,b) S(b,d) Take the tuples that are either in R or in S and satisfy the condition C Take the tuples that are either in R and satisfy the condition C or in U and satisfy the condition C
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that has all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) R(a,b) S(b,d) σ C ⋃ 100 100 σ C σ C R(a,b) S(b,d) 10000 20000 10000 20000 Take the tuples that are either in R or in S and satisfy the condition C Take the tuples that are either in R and satisfy the condition C or in U and satisfy the condition C
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that has all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) R(a,b) S(b,d) σ C ⋃ 100 100 σ C σ C 25000 R(a,b) S(b,d) 10000 20000 10000 20000 Take the tuples that are either in R or in S and satisfy the condition C Take the tuples that are either in R and satisfy the condition C or in U and satisfy the condition C
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that has all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) R(a,b) S(b,d) σ C ⋃ 100 100 σ C σ C 45 80 25000 R(a,b) S(b,d) 20000 10000 20000 10000 Take the tuples that are either in R or in S and satisfy the condition C Take the tuples that are either in R and satisfy the condition C or in U and satisfy the condition C
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that has all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) R(a,b) S(b,d) σ C ⋃ 100 100 σ C σ C 45 80 25000 R(a,b) S(b,d) 20000 10000 20000 10000 Take the tuples that are either in R or in S and satisfy the condition C Take the tuples that are either in R and satisfy the condition C or in U and satisfy the condition C
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R))
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) The correctness can be similarly proved
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) The correctness can be similarly proved tuples that are in R but not in S and satisfy the condition C
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) The correctness can be similarly proved tuples that are in R but not in S and satisfy the condition C tuples that are in R and satisfy the condition C but are not in S
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Again pushing the selection σC reduces the size of the intermediate relations
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Remarks.
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Remarks. 1. Set/bag operations are applied on relations of the same schema, so the condition C in σC are applicable on both arguments;
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Remarks. 1. Set/bag operations are applied on relations of the same schema, so the condition C in σC are applicable on both arguments; 2. The condition C may be pushed to only one argument;
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Remarks. 1. Set/bag operations are applied on relations of the same schema, so the condition C in σC are applicable on both arguments; 2. The condition C may be pushed to only one argument; 3. σC(R⋃S) = R⋃σC(S), σC(R⋃S) = σC(R)⋃S, and σC(RS) = RσC(S) are NOT valid
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R))
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Pushing the selection may not even be meaningful
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Pushing the selection may not even be meaningful S(a,b) T(b,d) c × π σ S.a=T.d
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Pushing the selection may not even be meaningful S(a,b) T(b,d) c × π σ S.a=T.d π c × σ S.a=T.d σ S.a=T.d S(a,b) T(b,d)
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Pushing the selection may not even be meaningful S(a,b) T(b,d) c × π σ S.a=T.d π c × σ S.a=T.d σ S.a=T.d ?? S(a,b) T(b,d)
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Even when it is syntactically meaningful, it may not be logically correct.
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Even when it is syntactically meaningful, it may not be logically correct. R(a,b) S(b,c) a b 5 yes … b c yes 4 …
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Even when it is syntactically meaningful, it may not be logically correct. R(a,b) S(b,c) 5 yes 4 a b 5 yes … b c yes 4 … in σa=5 or c=3(R ⨝ S) but may not be in σa=5(R) ⨝ σc=3(S)
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) We need a formal proof for the validity of each rule
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) We need a formal proof for the validity of each rule
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws Theorem. If C contains only attributes in R, then σC(R⨝S) = σC(R)⨝S σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Proof. Let t be in σC(R⨝S). Then t is a join of a tuple t1 in R and a tuple t2 in S such that t1 and t2 agree on the common attributes. Since C contains only attributes in R and t satisfies C, t1 must also satisfy C, i.e., t1 is in σC(R). Thus, t is a join of t1 in σC(R) and t2 in S, i.e., t is in σC(R)⨝S. Thus, all tuples t in σC(R⨝S) are in σC(R)⨝S. Now consider a tuple t in σC(R)⨝S. t is a join of a tuple t1 in σC(R) and a tuple t2 in S such that t1 and t2 agree on the common attributes, and t1 satisfies C. Since t1 is in R so t is in R⨝S. Since t1 satisfies C, t also satisfies C, i.e., t is in σC(R⨝S). Thus, all tuples t in σC(R)⨝S are in σC(R⨝S). Thus, we must have σC(R⨝S) = σC(R)⨝S. ⃞
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R))
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Can be trivially proved
Improving logic plan via logic laws σC(R⋃S) = σC(R)⋃σC(S) σC(R∩S) = σC(R)∩σC(S) = σC(R)∩S = R∩σC(S) σC(RS) = σC(R) σC(S) = σC(R) S ╳, ⨝, ⨝D: σC can only be pushed to the arguments that have all attributes in C. σC and D(R) = σC(σD(R)) = σD(σC(R)) Can be trivially proved Useful when you want to further push some of the selections down.
Improving logic plan via logic laws S(a,b) T(b,d) c × π σ S.b=T.b & d>4
Improving logic plan via logic laws S(a,b) T(b,d) × c π σ d>4 S.b=T.b S(a,b) T(b,d) c × π σ S.b=T.b & d>4 σC&D(R) = σC(σD(R))
Improving logic plan via logic laws S(a,b) T(b,d) × c π σ d>4 S.b=T.b S(a,b) T(b,d) c × π σ S.b=T.b & d>4 σC&D(R) = σC(σD(R)) σC(R×S) = R×σC(S) S(a,b) T(b,d) × c π σ S.b=T.b d>4
Improving logic plan via logic laws S(a,b) T(b,d) × c π σ d>4 S.b=T.b S(a,b) T(b,d) c × π σ S.b=T.b & d>4 σC&D(R) = σC(σD(R)) σC(R×S) = R×σC(S) S(a,b) T(b,d) c π σ d>4 S(a,b) T(b,d) × c π σ S.b=T.b d>4 σS.b=T.b(S×T) = S T
Improving logic plan via logic laws Major Steps: Move selections σ so they can be applied early (σ reduces table size); Combine cross product × with selections σ to make natural joins and theta joins C ( has more efficient algorithms); 3. group commutative and associative binary operations (e.g., ∩, U, ) (for later opt.); 4. May consider other operations (e.g., π, δ, τ, γ)