Skip to content

groupby performance degrades rapidly as column cardinality increases #9292

Open

Description

@MrPowers

Dask can perform groupby operations relatively well for columns with low cardinality, but performance seems to degrade significantly for columns with more distinct values.

Let's look at the h2o groupby benchmarks for some examples on the 1e9 dataset (contains one billion rows and is 50 GB on disk when stored as an uncompressed CSV file).

Let's query this dataset with a 16 node i3.large cluster (244 GB of cluster RAM total). Here's a query that returns 10,000 distinct row groups (i.e. the result has 10,000 rows) and takes 37 seconds to execute. Here's the notebook with complete details on how to build the software environment, the cluster info, all the code, etc.

ddf = dd.read_parquet(
    "s3://coiled-datasets/h2o-benchmark/N_1e9_K_1e2_parquet",
    storage_options={"anon": True, "use_ssl": True},
    columns=["id1", "id2", "v1"],
    engine="pyarrow",
)

ddf.groupby(["id1", "id2"], dropna=False, observed=True).agg({"v1": "sum"}).compute()

Here's a different groupby query on the same dataset that returns a million row groups. I ran this query for a half hour or so and then it errored out with a KilledWorker exception. Here's the notebook.

ddf = dd.read_parquet(
    "s3://coiled-datasets/h2o-benchmark/N_1e9_K_1e2_parquet",
    storage_options={"anon": True, "use_ssl": True},
    columns=["id3", "v1", "v3"],
    engine="pyarrow",
)

ddf.groupby("id3", dropna=False, observed=True).agg(
    {"v1": "sum", "v3": "mean"}
).compute()

Let's try to get this running by using the pyarrow[string] data type and by repartitioning first. The pyarrow[string] dtype massively shrinks the memory footprint of the DataFrame from 224GB to 37GB. This query still errors out with a KilledWorker exception.

ddf["id3"] = ddf["id3"].astype("string[pyarrow]")

ddf.repartition(200).groupby("id3", dropna=False, observed=True).agg(
    {"v1": "sum", "v3": "mean"}
).compute()

It's the same dataset and the same cluster, so it seems like the issue is caused by the cardinality of the groupby column. I'm interested in hearing what other folks think is the root cause. Let me know if there is any additional info I can provide to give you with more context. I'm happy to take pictures of dashboards, look at logs, etc.

Activity

  1. added
    needs triage
    on Jul 19, 2022
  2. added
    discussion
    and removed
    needs triage
    on Jul 20, 2022
  3. rjzamora commented on Jul 21, 2022

    @rjzamora
    Member

    Thanks for raising this @MrPowers - I will certainly try to help with this effort. Minor Note: You should specify index=False in your dd.read_parquet commands to avoid reading the unnecessary int64 "null" index into memory (you can also specify write_index=False when you write the dataset to avoid writing the "null" index).

  4. rjzamora commented on Jul 26, 2022

    @rjzamora
    Member

    Update: I downloaded the billion-record csv file to my local system and played with this a bit on a LocalCluster with a memory allowance similar to the h2o instance. I found it a bit challenging to get the default groupby(..).agg operation to succeed with the existing ACA-based groupby algorithm. It should work with smaller split_every settings and/or larger split_out settings. However, the operation was significantly more stable when I redirected the aggregate method to use the groupby_agg function I added to dask_cudf a couple years back. For example, the following code ran in ~3.5mins:

    import time
    import dask.dataframe as dd
    
    from dask.distributed import LocalCluster, Client, wait
    
    cluster = LocalCluster(
        local_directory="/.../dask-worker-space",
        n_workers=10,
        memory_limit="24GB",
    )
    client = Client(cluster)
    
    ddf = dd.read_csv(
        "/.../h2o/N_1e9_K_1e2_single.csv",
    )
    columns = ["id3", "v1", "v3"]
    ddf = ddf[columns]
    ddf["id3"] = ddf["id3"].astype("string[pyarrow]")
    agg = ddf.groupby("id3", dropna=False).agg(
        {"v1": "sum", "v3": "mean"},
    )
    
    t0 = time.time()
    result = agg.persist()
    wait(result)
    total_time = time.time() - t0
    
    print(total_time, len(result))

    Note that groupby_agg lives in dask_cudf, but it only uses cudf if the input data is a cudf.DataFrame. The logic is backend agnostic, because I had originally intended to upstream the code to dask.dataframe. The only reason I didn't follow through with this plan was because light preliminary benchmarking suggested that the "optimized" code path offered little-to-no benefit for Pandas-backed Dask-DataFrame. This particular example clearly suggest that my preliminary benchmarking was probably too light, and that the real benefit comes at scale.

  5. added
    needs attention
    on Aug 29, 2022
  6. fjetter commented on Sep 7, 2022

    @fjetter
    Member

    FYI I had an incredibly great experience with pre-shuffle compression for high column cardinality problems, see #6259 I never had the time to finish this but the code is straight forward enough if somebody wants to pick it up and give it a try

    This implementation was developed for of ETL pipelines that heavily relied on shuffling for parquet dataset repartitioning, see https://github.com/data-engineering-collective/plateau/blob/main/plateau/io/dask/_shuffle.py

  7. removed
    needs attention
    on Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dataframediscussionparquet

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions