Reading performance & plans
Concept  ·  SQL & Querying

Sorts/joins that exceed memory spill to disk — the ORDER-BY-everything habit is expensive.

Memory-bound operators hold their working set: a sort holds what it sorts, a hash join holds a build side, a hash aggregate holds its groups. Under the cap, engines refuse (an OOM error) or spill (partial results to disk, merge later — silent except in the plan and the wall clock). The everyday offenders are habits: ORDER BY on a million-row result (limit_row_goal's TOP_N is the cure when the LIMIT is there), sorting before filtering, DISTINCT over SELECT-* width.

See it break
inv
itemqtynote
a3x
b9y
c5z
The trap
SELECT
  item,
  qty,
  note
FROM
  inv
ORDER BY
  qty DESC
;
The fix
SELECT
  item
FROM
  inv
ORDER BY
  qty DESC
LIMIT
  3;
The trap
b9y
c5z
a3x
The fix
b
c
a

The export sorted every column of every row when only the top items were read — a sort holds its whole working set in memory, so an ORDER-BY-everything on a big wide table spills to disk (or errors under a cap); narrowing and LIMITing sorts far less.

The rule

Late/narrow sorts; filter-then-sort; LIMIT where top-k is meant; plan-check on the bimodal-duration tell.

Shows up elsewhere
Row-goal effects & paginationSELECT * cost on columnar
Try one →Prove it in practice →