| 1 |
Joins |
| 2 |
===== |
| 3 |
|
| 4 |
DataFrame joins are a common and expensive computation that benefit from a |
| 5 |
variety of optimizations in different situations. Understanding how your data |
| 6 |
is laid out and what you're trying to accomplish can have a large impact on |
| 7 |
performance. This documentation page goes through the various different |
| 8 |
options and their performance impacts. |
| 9 |
|
| 10 |
Large to Large Unsorted Joins |
| 11 |
----------------------------- |
| 12 |
|
| 13 |
In the worst case scenario you have two large tables with many partitions each |
| 14 |
and you want to join them both along a column that may not be sorted. |
| 15 |
|
| 16 |
This can be slow. In this case Dask DataFrame will need to move all of your |
| 17 |
data around so that rows with matching values in the joining columns are in the |
| 18 |
same partition. This large-scale movement can create communication costs, and |
| 19 |
can require a large amount of memory. If enough memory can not be found then |
| 20 |
Dask will have to read and write data to disk, which may cause other |
| 21 |
performance costs. |
| 22 |
|
| 23 |
These problems are solvable, but will be significantly slower than many other |
| 24 |
operations. They are best avoided if possible. |
| 25 |
|
| 26 |
Large to Small Joins |
| 27 |
-------------------- |
| 28 |
|
| 29 |
Many join or merge computations combine a large table with one small one. If |
| 30 |
the small table is either a single partition Dask DataFrame or even just a |
| 31 |
normal Pandas DataFrame then the computation can proceed in an embarrassingly |
| 32 |
parallel way, where each partition of the large DataFrame is joined against the |
| 33 |
single small table. This incurs almost no overhead relative to Pandas joins. |
| 34 |
|
| 35 |
If your smaller table can easily fit in memory, then you might want to ensure |
| 36 |
that it is a single partition with the repartition method. |
| 37 |
|
| 38 |
.. code-block:: python |
| 39 |
|
| 40 |
import dask |
| 41 |
large = dask.datasets.timeseries(freq="10s", npartitions=10) |
| 42 |
small = dask.datasets.timeseries(freq="1D", dtypes={"z": int}) |
| 43 |
|
| 44 |
small = small.repartition(npartitions=1) |
| 45 |
result = large.merge(small, how="left", on=["timestamp"]) |
| 46 |
|
| 47 |
Sorted Joins |
| 48 |
------------ |
| 49 |
|
| 50 |
The Pandas merge API supports the ``left_index=`` and ``right_index=`` options |
| 51 |
to perform joins on the index. For Dask DataFrames these keyword options hold |
| 52 |
special significance if the index has known divisions |
| 53 |
(see :ref:`dataframe-design-partitions`). |
| 54 |
In this case the DataFrame partitions are aligned along these divisions (which |
| 55 |
is generally fast) and then an embarrassingly parallel Pandas join happens |
| 56 |
across partition pairs. This is generally relatively fast. |
| 57 |
|
| 58 |
Sorted or indexed joins are a good solution to the large-large join problem. |
| 59 |
If you plan to join against a dataset repeatedly then it may be worthwhile to |
| 60 |
set the index ahead of time, and possibly store the data in a format that |
| 61 |
maintains that index, like Parquet. |
| 62 |
|
| 63 |
.. code-block:: python |
| 64 |
|
| 65 |
import dask |
| 66 |
import dask.dataframe as dd |
| 67 |
|
| 68 |
left = dask.datasets.timeseries(dtypes={"foo": int}) |
| 69 |
|
| 70 |
# timeseries returns a dataframe indexed by |
| 71 |
# timestamp, we don't need to set_index. |
| 72 |
|
| 73 |
# left.set_index("timestamp") |
| 74 |
|
| 75 |
left.to_parquet("left", overwrite=True) |
| 76 |
left = dd.read_parquet("left") |
| 77 |
|
| 78 |
right_one = dask.datasets.timeseries(dtypes={"bar": int}) |
| 79 |
right_two = dask.datasets.timeseries(dtypes={"baz": int}) |
| 80 |
|
| 81 |
result = left.merge( |
| 82 |
right_one, how="left", left_index=True, right_index=True) |
| 83 |
result = result.merge( |
| 84 |
right_two, how="left", left_index=True, right_index=True) |