Skip to content
Gold 7 min read 2 practice problems ↓

How aggregations execute

Partial and final aggregation, hash vs sort aggregation, and why COUNT(DISTINCT) costs more.

Step 2 · Spark internals · Gold · Under the hood, lesson 4 of 5

Read first: Narrow vs wide transformations · Catalyst and physical plans (skip if you know them)

In plain words first

Counting votes across 50 polling stations, each station counts its own votes first and phones in one number per candidate. Head office adds 50 small numbers instead of receiving every single ballot.

That is partial aggregation: each tasktask: The work for one partition in one stage, run on one CPU core. Learn more → sums its own rows before the shuffleshuffle: Moving rows between machines so that all rows with the same key end up together. Needed by joins, groupBy and sorting, and usually the most expensive step of a job. Learn more →, so only a few rows per key travel.

Unlock the full lesson

You have read the free part. The rest of this advanced lesson:

  • Partial and final aggregation
  • The three aggregation operators
  • Distinct aggregates
  • Spotting trouble in a plan
  • Try it yourself
  • Common mistakes
  • Key takeaways
  • Interview prep with 3 follow-up questions
  • Check yourself: 3 questions

Unlock it with the Complete pass (every advanced problem and lesson) or on its own.

Practise this

Interview problems on spark performance, graded on hidden tests

Keep going

Up next · Under the hood · lesson 5 of 5 · 7 min read
Serialization: Java, Kryo, Tungsten and Arrow
Where Spark serializes data and code, and which serializer matters for RDDs, DataFrames and Python.

Related lessons

Previous: Shuffle internals

Primary sources: Performance tuning · EXPLAIN