← Sat, Nov 7 digest

Can GROUP BY still be optimized after all these years?

read article ↗ hn thread ↗

Pivot introduced a slot-based partition pruning optimization for top-K GROUP BY queries, avoiding unnecessary merge work and improving query performance by skipping irrelevant partitions.

read it A detailed engineering breakdown of a novel database optimization that rewards systems programmers with a clear look at query execution improvements.

summary

Analytical databases traditionally merge all partitions during a GROUP BY operation, even when executing a top-K query where most partitions are irrelevant. Pivot solves this by assigning groups to slots within partitions using hashes, tracking bounds to safely prune entire partitions during the merge stage if their maximum possible values fall below the current top-K threshold. This optimization reduces compute and I/O overhead, yielding a 1.6x speedup on relevant ClickBench queries by skipping over 70 percent of partitions.

key points

  1. Traditional databases waste CPU cycles merging all data partitions during top-K GROUP BY queries even when most partitions cannot contain winning groups.
  2. Pivot uses a slot-based hashing scheme within partitions to track aggregate ranges, mirroring count-min sketch concepts for fine-grained pruning.
  3. If every slot's total in a partition is below the current top-K threshold, the entire partition is skipped during the merge phase.
  4. The optimization accelerates ClickBench Q16 by 1.6x while pruning over 70 percent of partitions.

who should read it

Database engineers, query optimizer developers, and systems programmers interested in analytical database performance.

discussion warm

consensus

Commenters appreciate the deep technical breakdown, with one noting the data structure resembles a count-min sketch and an author clarifying Pivot's positioning compared to DuckDB.

pushback

A minor pedantic debate arose over whether 1.6x faster is mathematically precise compared to 60 percent faster.

best comment by malisper ↗

Neat! For others, the data structure they are describing is a count-min sketch. It looks like they're using it to set a bound on elements that can enter the group-by and using it to skip a bunch of extra work

It instantly connects the custom-described structure to the standard computer science concept of a count-min sketch.

Generated by an LLM from the article and thread. Check the sources before relying on it.