← Graph

Prefer SQL Window Functions over Ruby Aggregation

takeaway 2 connections

Computations like 'average salary per department', 'cheapest product per category' or cumulative sums are dramatically faster as a single query using Postgres window functions (OVER / PARTITION BY / ROW_NUMBER / ROWS BETWEEN) than by issuing two queries and combining in Ruby. They compose inside Rails, encapsulate cleanly in a DTO, and pair well with Ruby pattern matching on the result.

type
recommendation
takeaway Prefer SQL Window Functions over Ruby Aggregation
about
Explicitly about choosing window functions over Ruby-side aggregation.
takeaway Prefer SQL Window Functions over Ruby Aggregation
from_talk
Core lesson of the window functions section.

Provenance

Read by
4 extractions