Back to questionsA sales mart is moving into Greenplum (48 segments).
MiddleDesignVery common
A sales mart is moving into Greenplum (48 segments). orders has 2 billion rows: order_id, customer_id (NULL for the 15% of guest orders), order_date, a six-value status and is_paid. order_items has 9 billion rows and joins orders on order_id in almost every report. customers has 40 million rows and joins orders on customer_id; currency_rates has 3,000 rows. Most reports filter by a date range. A colleague proposes DISTRIBUTED BY (order_date) for orders, so a day's report touches one segment, and DISTRIBUTED BY (is_paid) for order_items. Choose the distribution for each table, say what is wrong with the proposal, and how you would check a candidate key before moving 9 billion rows.
Distribute orders and order_items on order_id: unique, even, and the biggest join runs with no motion. Replicate currency_rates; put customers on customer_id and accept a motion on that join. A date key sends each day to one segment; a boolean uses two at most.
- Distributing by the date column that queries filter on, so each day's load and each day's report land on one segment
- Choosing a low-cardinality key such as a status or a boolean, which can use at most as many segments as it has values
- Picking a join key with many NULLs, which sends every NULL row to the same segment
- When would you distribute
customersoncustomer_idinstead of replicating it, given the 40 million rows? - Why must both join columns have the same data type for a collocated join in Greenplum?
The keys (Greenplum and its forks Arenadata DB and Greengage DB):
CREATE TABLE orders (...) DISTRIBUTED BY (order_id);
CREATE TABLE order_items (...) DISTRIBUTED BY (order_id); -- same key and same type as in orders
CREATE TABLE customers (...) DISTRIBUTED BY (customer_id);
CREATE TABLE currency_rates (...) DISTRIBUTED REPLICATED;ordersandorder_itemsonorder_id. The key is unique inordersand high-cardinality inorder_items, so rows spread evenly. The join of the two largest tables, the most frequent one in reports, runs on every segment over its own rows with no Redistribute Motion.order_idmust have the same type in both tables: values of different types can hash differently.customersoncustomer_id. Joiningordersto customers redistributes one side. That is the price of not being able to distributeorderson two keys at once, and it is paid on the rarer join. Distributingordersoncustomer_idis out anyway, because the 15% of NULL rows would all land on one segment. Replicating 40 million rows is possible if the table fits on every segment and is rarely reloaded — measure it.currency_rates—DISTRIBUTED REPLICATED(available in Greenplum 6 and later and in its forks). 3,000 rows on every segment, and every join to rates is local.
What is wrong with the proposal.
order_date: all rows of one day hash to one segment. The nightly load of a day writes to one segment out of 48, and a one-day report runs on one segment while 47 sit idle. A distribution key filters nothing; date pruning comes from partitions. Columns used inWHEREmake poor distribution keys in general: the query's work goes to a few segments.is_paid: two values, so 9 billion rows occupy at most two segments. The six-valuestatuswould occupy at most six.
Check before the move. Cardinality and the most frequent values: SELECT count(DISTINCT order_id), count(*) - count(order_id) AS nulls FROM ... and GROUP BY key ORDER BY count(*) DESC LIMIT 10. Then a sample into a temporary table distributed on the candidate, and rows per segment:
CREATE TEMP TABLE probe AS
SELECT order_id FROM stage_order_items WHERE random() < 0.01
DISTRIBUTED BY (order_id);
SELECT gp_segment_id, count(*) FROM probe GROUP BY 1 ORDER BY 2 DESC LIMIT 5;
-- for an existing table, gp_toolkit.gp_skew_coefficients shows the same