Formulate the foundational Equivalence Rules of Relational Algebra and explain how heuristic rule-based optimization exploits predicate pushdown.
Relational algebra forms the formal mathematical substrate of relational databases, modeling data transformations as operators on relations (sets or multisets of tuples). Two relational algebra expressions E_1 and E_2 are defined as equivalent (E_1 ≡ E_2) if they produce the identical multiset of tuples on every valid database instance.
Foundational Equivalence Theorems:
1. Commutativity and Associativity of Joins and Cartesian Products:
- Commutativity: R ⋈_θ S ≡ S ⋈_θ R, and R × S ≡ S × R.
- Associativity: (R ⋈_θ1 S) ⋈_θ2 T ≡ R ⋈_θ1 (S ⋈_θ2 T).
Join commutativity and associativity allow the query optimizer to evaluate all possible permutations of join orderings, searching for the permutation that minimizes intermediate result sizes.
2. Commutativity and Cascading of Selections:
- Cascading: σ_{θ_1 ∧ θ_2}(R) ≡ σ_{θ_1}(σ_{θ_2}(R)).
- Commutativity: σ_{θ_1}(σ_{θ_2}(R)) ≡ σ_{θ_2}(σ_{θ_1}(R)).
3. Commuting Selection with Cartesian Products and Joins (Predicate Pushdown):
- If selection predicate θ involves only attributes belonging to relation R:
σ_θ(R × S) ≡ (σ_θ(R)) × S, and σ_θ(R ⋈ S) ≡ (σ_θ(R)) ⋈ S.
- If selection predicate θ decomposes into θ_1 ∧ θ_2, where θ_1 contains only attributes of R and θ_2 contains only attributes of S:
σ_{θ_1 ∧ θ_2}(R ⋈ S) ≡ (σ_{θ_1}(R)) ⋈ (σ_{θ_2}(S)).
4. Commuting Projections with Joins (Projection Pruning):
- If L is a list of attributes, π_L(R ⋈ S) can push projections down to π_{L ∩ attr(R)}(R) and π_{L ∩ attr(S)}(S), provided all attributes referenced in the join predicate θ are temporarily retained.
Heuristic Rule-Based Optimization (Predicate Pushdown):
In a naive unoptimized query, the database engine would execute a Cartesian product or full join across multi-million-tuple relations (producing an enormous intermediate relation of size |R| × |S|), and only then apply filtering predicates σ_θ. By systematically applying the Predicate Pushdown transformation rule, the optimizer pushes selection operators down the expression tree directly to the leaf scan operators. Filtering tuples at the storage layer reduces the cardinality of intermediate relations from millions of tuples to dozens of tuples before expensive join operators are invoked. This single heuristic rule reduces disk I/O, memory buffer consumption, and CPU comparison cycles by several orders of magnitude.