Sub topic 12
Shuffles, joins and the index: what is expensive
This chapter assumes you can build a Dask DataFrame, know that work is lazy, and can read a task graph at a basic level. It explains which operations are cheap and which are costly, and why.
Cheap work and costly work
Most operations touch each partition on its own. Filters, assign, and element-wise math need no data from other partitions, so they run in parallel with no communication.
Other operations need rows that share a key to be in the same place. A groupby with a large number of distinct keys, a join on a column, and a sort all fall in this group. Dask must then do a shuffle: it moves rows between partitions over the network or through disk, based on their key. A shuffle is usually the most expensive step in a Dask job.
Every input partition can send data to every output partition. The cost grows with the data size and with the number of partitions, and all inputs must finish before the outputs can start.
Aggregations are not all equal
A groupby(...).mean() with few groups is cheap. Dask computes a small partial result per partition, then combines them. A groupby with many groups, or groupby.apply with custom code, needs a shuffle. Prefer built-in aggregations such as sum, count and mean when you can.
Joins
How a join runs depends on what you join.
- Dask with pandas, or with a small Dask frame: Dask copies the small table to every partition of the big one. No shuffle is needed.
- Two large Dask frames on a column: Both sides are shuffled by the join key. This is expensive.
- Two Dask frames already sorted by the join key as their index, with known divisions: Dask can match partitions directly and skip the shuffle.
import pandas as pd
import dask.dataframe as dd
trips = dd.from_pandas(
pd.DataFrame({"zone_id": [1, 2, 1, 3], "fare": [10.0, 20.0, 30.0, 40.0]}),
npartitions=2,
)
zones = pd.DataFrame({
"zone_id": [1, 2, 3],
"zone_name": ["Airport", "Center", "Harbor"],
})
# Small pandas table: sent to each partition, no shuffle
joined = trips.merge(zones, on="zone_id", how="left")
print(joined.compute())The index and divisions
A Dask DataFrame can record divisions, which are the index boundaries of each partition. When divisions are known, Dask knows which partition holds a given index value. loc lookups then read only that partition, and index-aligned joins can skip shuffles.
set_index sorts the data by a column, which is a shuffle. You pay for it once and benefit afterward.
indexed = trips.set_index("zone_id")
print(indexed.known_divisions) # True
print(indexed.loc[1].compute()) # reads only the partitions that can hold 1If your data is already sorted by the column, pass sorted=True to set_index. Dask then skips the shuffle and only computes the divisions. If the data is not actually sorted, the results will be wrong, so use it only when you are sure.
Reducing the cost
- Filter rows and select columns before a join or shuffle, so less data moves.
- Broadcast small tables as pandas objects instead of joining two large Dask frames.
- Set the index once, then
persist()the result so later steps reuse the sorted data. - Write the sorted data to Parquet. Later jobs can start from the stored order and divisions.
- Check the dashboard for long transfer times and rising memory. These point to a shuffle.
Exercise
You join a 500 GB Dask DataFrame to a 2 MB lookup table on zone_id. A colleague suggests converting both to Dask frames and calling set_index("zone_id") on each first. Is this a good plan? What would you do instead?
Show answer
No. Setting the index on the 500 GB frame is a full shuffle, which is a large cost for no gain. Keep the lookup table as a pandas DataFrame, or a one-partition Dask frame, and call merge. Dask sends the small table to each partition of the large frame, so no shuffle is needed.