Measurements over Estimates

Concepts covered: Adaptive Query Execution

An estimate and a measurement answer one question, how big is this, from opposite ends. An estimate starts at file metadata and reasons forward through each operation, applying an assumption at every step. A measurement waits until the bytes exist and counts them. The estimate can be wrong in ways that multiply. The measurement is exact about the past and silent about everything that has not run.

An estimate drifts with each operation

Point in the queryWhat the estimate assumesHow far off it can be
Scan of a tableThe bytes of its filesUsually close
After a filterThe filter keeps every rowAbout 700x in the new-releases job
After a joinA formula over the input sizesAny amount, when keys repeat
After another joinThe previous guess, fed forwardThe earlier errors, multiplied

Each row's error feeds the next one. The duplicate-key blowup from the joins lesson is the sharpest case: the size of a join's output depends on how often each key repeats on both sides, and no file size reveals that. A few operations into a long query, the number the optimizer is working with is a guess built on a guess.

The decisions each one makes

Estimates decide
  • Everything up to the first finished shuffle, including how each table is scanned
  • Whether the first join starts out as a broadcast or as a shuffle
  • The entire plan of a query that has no shuffle
  • The entire plan whenever the AQE flag is false
Measurements decide
  • The strategy of any join fed by a shuffle that has finished
  • How a finished shuffle's output is divided among the tasks reading it
  • Each later re-plan, which uses the newest sizes available
  • Only the work downstream of a shuffle, never the work before it

The boundary is the first shuffle to finish. Before it, a prediction is all Spark has, so the prediction rules. After it, a counted size exists for that part of the data, and Spark trusts the count over the prediction it replaces.

A good estimate still saves a shuffle

In the new-releases job, the correction arrived after views and titles had each written their shuffle output. Had the first plan been right, views would never have been shuffled. A hint keeps that value on an adaptive cluster for a side that is small by construction yet estimated large: a 3GB stores table filtered to the 5 stores one team owns always leaves a few rows, while the estimate stays at 3GB. Only the hint spares the big side its shuffle. A 50-row lookup needs no hint, since its files already sit under 10MB. A table that is small only on today's data is better left to the measurement.

Do
  • Leave AQE on, so measured sizes can correct join and partition choices after each shuffle
  • Hint a broadcast for a side always small after its filter yet estimated large before the run
  • Compare the Initial Plan with the Final Plan when a run is slow
Avoid
  • Treat the plan printed before a run as the plan that ran
  • Expect AQE to change a query with no shuffle, where nothing is ever measured
  • Count a corrected plan as free: the shuffle that supplied the sizes was still paid for

Predict, run, compare

Before running a query, write down 2 numbers for each side of a join: the size the optimizer will assume, which is the file size, and the size you believe that side really has after its filters. Run the query, then open the Final Plan. Expect a changed join node only where the assumed size is above the broadcast threshold, 10MB by default, your size is below it, and that side comes out of a shuffle. Where both numbers sit on the same side of the threshold, expect matching join nodes even though they disagree.

The cell below is the measuring half of that habit on the practice data. Guess the row count of the join before you run it. Nothing in the file sizes of orders or order_items would have told you the answer.

That count is a measurement in AQE's sense: Spark ran the work and counted the output. The drill below is that habit at its shortest, a narrowing step followed by the action that returns the count.

AQE's counts need a finished shuffle. A query that never shuffles gives it nothing to measure.

A query filters a 50GB table, selects 3 columns and writes the result to storage. AQE is on. Which statement about its plan is right?

The estimate opens every query and owns it until a shuffle finishes. From there the measurement owns the remainder, and each new shuffle hands it fresher numbers.

About this lesson

This section is part of the Adaptive Query Execution: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.