Course contents 10 / 35

10 / 35 · Distributed Computation

Distributed SQL and MPP

Follow a sales query from its coordinator through local scans to a final result. Distinguish gathering rows from repartitioning them.

The sales report has 600 GB of order lines spread over four workers. Its SQL still refers to one table. To execute that query, the database has to decide which part of the plan runs beside each set of rows and which results need to cross the network.

Dispatch a plan beside the data#

A shared-nothing system gives each node its own processor, memory and storage resources. Nodes exchange messages to coordinate the work. In a massively parallel processing system, or MPP system, several workers execute operators for the same query over different parts of the input. In Greenplum a client sends SQL to the coordinator. Greenplum 6 documentation calls this role the master. Segment processes execute plan fragments against their parts of the data. A segment is a database instance and several can run on one host. We use one segment per worker in the toy model. (DeWitt and Gray, 1992, §3; Greenplum 6, Query processing)

SELECT store_id,
       SUM(quantity * unit_price_cents) AS revenue_cents
FROM order_lines
GROUP BY store_id
ORDER BY store_id;

Assume that every row for one store is placed on the same worker, that the four parts are disjoint and that all workers read the chosen snapshot. Each segment scans its rows and computes complete totals for its own stores. With the 600 GB model, four equal parts mean 150 GB to scan per worker. The coordinator does not have to receive those rows. If the report covers 500 stores, the workers can send 500 total rows altogether. The output still needs to be ordered by store. A final sort or a merge of ordered streams can implement that requirement.

Figure

Four scans and one result

Four scans and one resultWorkers A through D each scan 150 GB and send local store totals to a receiver. Together they produce 500 totals.A150 GBB150 GBC150 GBD150 GBGather · 500 totals
Each segment scans 150 GB locally. With all rows of a store on one segment, gathering 500 store totals needs only 500 rows.

Move data where the next operator needs it#

A gather combines the outputs of several workers at one receiver. An exchange transfers rows between pieces of a distributed plan. Greenplum represents movements with Motion operators. A redistribution sends rows to destinations selected by a new key. A broadcast makes a copy of its input available at each destination. A gather is useful for returning a small final result. A broad redistribution lets downstream operators continue on several workers. (Greenplum 6, Query processing)

The same distinction appears in other implementations. ClickHouse’s Distributed table engine can send work to remote shards, aggregate there and pass intermediate states to the initiating server. The operator names and optimization rules depend on the engine. A filter on a known distribution key can sometimes direct work to one shard. A query whose input overlaps all shards still needs their contributions. (ClickHouse, Distributed table engine)

Next#

The plan was local per store because one worker owned all its rows. If each day’s files contain the same store keys, each worker can compute only a partial total. Distributed aggregation and shuffle shows which partial results need to meet and how much data must move.

References#

  1. David J. DeWitt and Jim Gray. Parallel Database Systems. University of Wisconsin, TR1079, 1992, §§2–3. Describes parallel dataflow and shared-nothing architecture.
  2. Greenplum Project. About Greenplum Query Processing. Greenplum 6 documentation on dispatch, segment-local operators and Motion.
  3. ClickHouse. Distributed table engine. Documents remote shard reads and transfer of partial aggregate states.