Wroclove.rb 2018 lightning talk replicating the heavy Rails products/reviews example from an earlier talk. Starts from a 300-ms products#index rendering 100 products with per-row avg-rating computation — two queries per row yield the slowness. Compares three SQL approaches on a dataset of 2,000 products, 2,000 users and 300,000 reviews: (1) classic SQL with AVG + JOIN + GROUP BY + ORDER BY + LIMIT 10 → ~140 ms; (2) Common Table Expression (Postgres ≥ 8.4) pre-computing averages → ~99 ms; (3) LATERAL join (Postgres ≥ 9.3) correlating a subquery per product → similar time without filtering but dramatic gains with filtering (classic ~47 ms, CTE unchanged, LATERAL ~5 ms). Final page load drops to 37 ms.