Lecture: Lecture 16 - Physical Design
Related: Query Optimization Pipeline and Relational Algebra, Join Algorithms and Execution Plan Selection
What “push down” means in a query tree
In a relational-algebra query tree, execution flows from the bottom toward the top:
- Bottom (“down”) = earlier (starting at the raw base tables on disk).
- Top (“up”) = later (producing the final query output).
TOP (happens LAST) ──► Final result returned to user
▲
│ intermediate rows flow UP
│
BOTTOM (happens FIRST) ──► Base tables (SHOP, SALES, PRODUCT)Just as algebraic optimization pushes selections () and projections () down so joins process fewer rows and columns, group-by push down anticipates a GROUP BY—moving it lower (earlier) in the query tree, before joins—to collapse large detailed tables into much smaller partial aggregates first (Slide 18).
Everyday analogy: counting 1,000,000 daily receipts
Suppose you have a box of 1,000,000 daily receipts (SALES), each listing (Prod#, Shop#, Date, Qty), plus two small lookup booklets:
SHOP: maps eachShop#to itsProvinceandRegion.PRODUCT: maps eachProd#to itsPType(“Shoes”, “Jackets”, etc.).
You want total Qty by (PType, Province) for Piemonte:
- Without push down (
GROUP BYat the top): You take all 1,000,000 receipts and look upProvinceandPTypefor every single receipt—even if Shop #12 sold Product #7 on 365 different days—and add them up only at the very end. - With push down (
GROUP BYpushed down): Before opening the lookup booklets, you collapse the 365 daily receipts for each(Prod#, Shop#)into one subtotal. Now the joins only process those subtotals instead of 1,000,000 raw rows!
Important limit on aggregates: Splitting a
GROUP BYworks seamlessly forSUM(because adding partial sums yields the exact total: ), as well asMIN,MAX, and summing partialCOUNTs. You cannot naively average partialAVGs without carrying bothSUMandCOUNT.
Step-by-step trace of the source example (Slides 19–22)
Consider the retail schema from Slide 19:
PRODUCT(Prod#,PName,PType,PCategory)SHOP(Shop#,City,Province,Region,State)SALES(Prod#,Shop#,Date,Qty) — very large table (daily grain)
SQL query:
SELECT PType, Province, SUM(Qty)
FROM Sales S, Shop SH, Product P
WHERE S.Shop# = SH.Shop#
AND S.Prod# = P.Prod#
AND Region = 'Piemonte'
GROUP BY PType, Province;1. Initial query tree (Slide 20)
- Filter
SHOPwith . - Join filtered
SHOPwith all raw daily rows ofSALESonShop#. - Join that large intermediate result with
PRODUCTonProd#. - Perform
GROUP BY PType, ProvincewithSUM(Qty)only at the top.
2. First push down: below the PRODUCT join (Slides 20 → 21)
Below the PRODUCT join, PType is not available yet—only Prod# is. Because Prod# uniquely determines PType (each product belongs to one product type), we can pre-aggregate by GROUP BY Prod#, Province right above the SHOP ⋈ SALES join:
- Multiple rows for the same product across different shops in the same province collapse into one subtotal per
(Prod#, Province). Prod#stays in the pushed-downGROUP BYso the upper join withPRODUCTcan still match onProd#, followed by the finalGROUP BY PType, Provinceat the top.
3. Second push down: directly onto SALES below the SHOP join (Slides 21 → 22)
Directly on SALES, Province is not available yet—only Prod#, Shop#, and Date. Since Date is neither projected in the SELECT list nor used in any WHERE/join condition, we can immediately pre-aggregate SALES by GROUP BY Prod#, Shop#:
- Every daily sale for a given product in a given shop collapses into a single subtotal before joining with
SHOP. - Both
Prod#andShop#remain in the grouping key so theSHOPjoin (Shop#) andPRODUCTjoin (Prod#) still work above it.
BEFORE (Slide 20) AFTER: Group-By Push Down (Slide 22)
----------------- ------------------------------------
GROUP BY PType, Province GROUP BY PType, Province
│ │
JOIN ─── PRODUCT JOIN ─── PRODUCT
│ │
JOIN ─── SALES GROUP BY Prod#, Province
│ │
σ(Region='Piemonte') JOIN
│ / \
SHOP σ(Region='Piemonte') GROUP BY Prod#, Shop#
│ │
SHOP SALESAutomatic DBMS optimization vs. manual SQL rewrite (Slide 18)
In an ideal DBMS, the query optimizer performs group-by push down automatically on the standard SQL query without changing application logic. However, Slide 18 warns: “not available in all systems — observe the execution plan.”
- First inspect the execution plan (
EXPLAIN) to see whether theGROUP BYhappens after all joins or before them. - If the DBMS does not push the grouping down and the join is a bottleneck, you can rewrite the SQL manually using a pre-aggregated subquery (an added study example showing how to express Slide 22 in SQL):
SELECT P.PType, SH.Province, SUM(S_sub.SubQty)
FROM (
SELECT Prod#, Shop#, SUM(Qty) AS SubQty
FROM Sales
GROUP BY Prod#, Shop#
) S_sub
JOIN Shop SH ON S_sub.Shop# = SH.Shop#
JOIN Product P ON S_sub.Prod# = P.Prod#
WHERE SH.Region = 'Piemonte'
GROUP BY P.PType, SH.Province;Unlike Oracle-style optimizer hints (Slide 23), rewriting with a standard SQL subquery preserves data independence because it relies on logical relational equivalence rather than physical index names.
Common confusions
- Why can’t we just push
GROUP BY PType, Provincestraight ontoSALES? BecauseSALESdoes not containPTypeorProvince! Pushing aGROUP BYbelow a join requires replacing columns from the upper table (PType,Province) with the foreign/join key available in the lower table (Prod#,Shop#), provided the join key functionally determines those attributes (oneShop#has oneProvince, oneProd#has onePType). - Does pushing
GROUP BYdown eliminate the topGROUP BY? No. MultipleShop#values map to the sameProvince, and multipleProd#values map to the samePType, so the higherGROUP BYsteps are still needed to combine the partial subtotals into the final groups.
Retrieval questions
1. Suppose we add AND Date >= '2025-01-01' to the WHERE clause of the SALES / SHOP / PRODUCT query. In the rewritten tree (Slide 22), must go below or above GROUP BY Prod#, Shop# on SALES, and why?
It must go below GROUP BY Prod#, Shop# (directly on SALES before grouping). Once GROUP BY Prod#, Shop# collapses all dates into one subtotal row per (Prod#, Shop#), the Date attribute is gone—so filtering by Date afterward would be impossible.
2. Why must Prod# remain in the middle GROUP BY Prod#, Province even though the final SELECT only asks for PType, Province?
Because PRODUCT has not been joined yet at that point in the tree. Keeping Prod# preserves the join key needed to look up PType from PRODUCT in the step above, after which the top GROUP BY PType, Province combines products that share the same PType.
3. If your DBMS execution plan shows GROUP BY running only at the very top after joining a huge fact table, how can you achieve push-down without breaking data independence?
Rewrite the query in standard SQL using an inner GROUP BY subquery on the join keys (rather than embedding DBMS-specific physical optimizer hints).