In the massively parallel warehouse Greenplum, a 5B-row fact join to a 10M-row dimension redistributes both sides — fix it
Neither table is distributed on the join key, so both are reshuffled across the interconnect. Distribute the fact on the join key if it spreads evenly and the dimension on the same key, or replicate the dimension. The busiest segment sets the query time.
- Believing table partitioning decides which segment a row lives on
- Distributing a fact on a low-cardinality or NULL-heavy key and piling rows onto one segment
- Reading the average segment time instead of the slowest one when judging a skewed query
- How do you measure rows per segment in Greenplum, and what does
skew_rowsin the Redshift warehouse tell you? - When is replicating the dimension better than redistributing the fact on the join key?
Plan before the fix (Greenplum; its forks Arenadata DB and Greengage DB behave the same). sales is distributed by sale_id, customers randomly:
Gather Motion 48:1 (slice3; segments: 48)
-> Hash Join
Hash Cond: (s.customer_id = c.customer_id)
-> Redistribute Motion 48:48 (slice1; segments: 48)
Hash Key: s.customer_id
-> Seq Scan on sales s
-> Hash
-> Redistribute Motion 48:48 (slice2; segments: 48)
Hash Key: c.customer_id
-> Seq Scan on customers cBoth sides are redistributed on customer_id: 5 billion fact rows cross the interconnect for the join. The planner may instead broadcast the dimension to every segment (Broadcast Motion) — 10M × 48 rows on every run; the fix below removes that too.
Skew first. Check how the join key would spread across segments:
SELECT gp_segment_id, count(*) FROM sales GROUP BY 1 ORDER BY 2 DESC; -- current layout
SELECT count(*) FILTER (WHERE customer_id IS NULL)::float / count(*) FROM sales;If 30% of sales have customer_id IS NULL (guest checkouts), distributing on it puts every NULL on one segment, and that segment sets the time of every query. In Redshift, svv_table_info.skew_rows and per-slice row counts show the same picture.
Fix.
- The key spreads evenly:
ALTER TABLE sales SET DISTRIBUTED BY (customer_id);andcustomerson the same key; the join runs locally with no motion. Changing the key rewrites the whole table, so plan a window. - The key is skewed and the dimension fits on every segment (10M rows usually does): keep the fact on the even
sale_idand makecustomersDISTRIBUTED REPLICATED(DISTSTYLE ALLin Redshift) — each segment joins its own sales with a local copy.
Date partitioning changes nothing here: it splits the table by value inside every segment but does not decide which segment holds a row.