An events table of 5 billion rows grows by 20 million rows a day and is moving from a PostgreSQL replica to a massively parallel (MPP) warehouse (Greenplum or Redshift). Almost every query filters by a range of a few days and often by user_id; many join events to a 50-million-row users table on user_id; data older than 400 days must be removed. One teammate says a B-tree index on (event_date, user_id) is all the table needs. Another wants to distribute it by event_date so a one-day query touches a single segment. For an index, date partitioning, a sort key and a distribution key, say what work each lets the engine skip, then choose the layout for events and explain what is wrong with each proposal.
An index finds a few rows by key (OLTP); date partitions prune whole ranges and drop old days cheaply; a sort key on the filter columns lets block min/max skip data inside a partition; a distribution key only places rows. Distribute by user_id, never by date.
- Distributing by the date column, which puts each day's load and each day's queries on one segment
- Expecting a distribution key to filter data the way partition pruning does
- Carrying OLTP B-tree index habits into a columnar MPP warehouse built for scans
- Why does a compound sort key in the Redshift warehouse prune well on its leading column but barely on the second?
- How would you check that
user_idspreads rows evenly before choosing it as the distribution key?
What each structure skips.
- A B-tree index (OLTP) finds a few rows by key. In a columnar MPP warehouse queries read millions of rows over several days, and an index barely helps: Redshift has no indexes at all, and in Greenplum they pay off mainly for queries that return few rows.
- Date partitioning prunes whole partitions outside the range and lets an old day or month go as a partition
DROP/TRUNCATE, with noDELETEover billions of rows. - A sort key (Redshift's sort key, ClickHouse's
ORDER BY, an Iceberg sort order) orders the rows, so each block's min/max is narrow and a filter skips blocks inside a partition. Column order matters: a compound key prunes on its prefix. - A distribution key decides which segment holds a row. It filters nothing: it gives even load and collocated joins. Greenplum sends an equality query on the key to one segment (direct dispatch), but that segment still reads its whole share of the table.
Layout for events. Distribute by user_id — its high cardinality gives even segments, and users, distributed on the same key, joins with no data movement. Date pruning depends on the engine:
- Greenplum (and its forks Arenadata DB and Greengage DB): partition by
event_date(daily or monthly), and old data goes as a partitionDROP/TRUNCATE. There is no sort key object — data is loaded into the partitions in date order. - Redshift: ordinary tables have no partitioning. The compound sort key
(event_date, user_id)prunes by block min/max, and retention is aDELETEplusVACUUM, or per-month tables behind aUNION ALLview.
What is wrong with the proposals. The index solves an OLTP problem and replaces neither pruning nor sorting, and retention with it stays an expensive DELETE. Distributing by date puts all of one day's rows on one segment: today's load runs on one node, and a one-day query uses one segment out of all of them — no MPP parallelism.