Debian Codegraph @

docs/source/dataframe-optimizer.rst

raw
1 Optimizer
2 =========
3
4 .. currentmodule:: dask.dataframe
5
6 .. note::
7 Dask DataFrame supports Query Planning since version 2024.03.0
8
9 Optimization steps
10 ------------------
11
12 Dask DataFrame will run several optimizations before executing the computation. These
13 computations are aimed towards improving the efficiency of the query.
14
15 The optimizations entail the following steps (this list is not complete):
16
17 - **Projection Pushdown:**
18
19 Select only the required columns in every step. This reduces the amount of data
20 that needs to be read from storage but also the amount of data that is processed
21 along the way. Columns are dropped at the earliest stage in the query.
22
23 - **Filter Pushdown:**
24
25 Push filters down as far as possible, potentially into the IO step. Filters are
26 executed in the earliest stage in the query.
27
28 - **Partition Pruning:**
29
30 Partition selections are pushed down as far as possible, potentially into the IO
31 step.
32
33 - **Avoiding Shuffles:**
34
35 Dask DataFrame will try to avoid shuffling data between workers as much as possible.
36 This can be achieved if the column layout is already known, i.e. if the DataFrame
37 was shuffled on the same column before. For example, executing a
38 ``df.groupby(...).apply(...)`` after a merge operation will not shuffle the data
39 again if the groupby happens on the merge columns.
40
41 .. figure:: images/optimizer/avoiding-shuffles.svg
42 :align: center
43
44 Similarly, performing two subsequent Joins/Merges on the same join-column(s) will avoid shuffling the
45 data again. The optimizer identifies that the partitioning of the DataFrame is already as
46 expected and thus simplifies the operation to a single Shuffle and a trivial merge operation.
47
48 - **Automatically resizing partitions:**
49
50 The IO layers automatically adjust the partition count based on the column subset
51 that is selected from the dataset. Very small partitions impact the scheduler and
52 expensive operations like shuffling negatively. This is addressed by adjusting
53 the partition count automatically.
54
55 .. figure:: images/optimizer/automatic-repartitioning.svg
56 :align: center
57
58 Selecting two columns that together have 40 MB per 200 MB file. The optimizer reduces the number of partitions by a factor of 5.
59
60 Exploring the optimized query
61 -----------------------------
62
63 Dask will call ``df.optimize()`` before executing the computation. This method applies
64 the steps mentioned above and returns a new Dask DataFrame that represents the optimized
65 query.
66
67 A rudimentary representation of the optimized query can be obtained by calling
68 ``df.pprint()``. This will print the query plan in a human-readable format to the
69 command line/console. The advantage of this method is that it doesn't require any
70 additional dependencies.
71
72 .. code-block::
73
74 pdf = pd.DataFrame({"a": [1, 2, 3] * 5, "b": [1, 2, 3] * 5})
75 df = dd.from_pandas(pdf, npartitions=2)
76 df = df.replace(1, 5)[["a"]]
77
78 df.optimize().pprint()
79
80 Replace: to_replace=1 value=5
81 FromPandas: frame='<pandas>' npartitions=2 columns=['a'] pyarrow_strings_enabled=True
82
83 A more advanced and easier to read reprepresentation can be obtained by calling
84 ``df.explain()``. This method requires the ``graphviz`` package to be installed.
85 The method will return a graph that represents the query plan and create an image
86 from it.
87
88 .. code-block::
89
90 df.explain()
91
92 .. figure:: images/dataframe-optimize-explain.png
93 :alt: Optimized Query
94 :align: center
95
96 We can see in both representations that ``FromPandas`` consumed the column
97 projection, only selecting the column ``a``.
98
99 The ``explain()`` method is significantly easier to understand for more complex
100 queries.