Skip to content

BigQuery: Decrease peak memory usage of to_dataframe #8107

Closed

Description

@tswast

From: googleapis/python-bigquery-pandas#275 (comment)

via read_gbq(use_bqstorage_api=False)
mem_test_baseball games_post_wide

via read_gbq(use_bqstorage_api=True)
mem_test_gbq_storage_api

via fastavro
mem_test_baseball games_post_wide avro

via pd.read_csv(compression='gzip')
mem_use_gzip

via `pd.read_parquet(), using the gzip-imported dataframe -> exported as parquet as sourcefile
mem_test_parquet

I suspect that if we del pages as we finish converting them to dataframe in the to_dataframe loop, we can decrease peak memory usage / overshoot by quite a lot.

Activity

  1. self-assigned this
    on May 23, 2019
  2. added
    api: bigquery
    type: feature request
    api: bigquerystorage
    on May 23, 2019
  3. tswast commented on Jan 10, 2020

    @tswast
    ContributorAuthor

    Now that we're using pyarrow with the BQ Storage API, I suspect that memory usage has decreased by quite a bit.

    It's worth re-running these tests to see if we can close this issue out.

  4. assigned and unassigned on Jan 10, 2020
  5. plamut commented on Jan 20, 2020

    @plamut
    Contributor

    TL;DR - I profiled the code again on the baseball public data using different BQ client versions and with(out) the bqstorage client, and the latter makes a huge difference.

    Versions used

    Python: 3.7.0+
    pyarrow: 0.15.1
    google-cloud-bigquery-storage: 0.7.0
    

    Test script

    from google.cloud import bigquery
    
    
    @profile
    def run_mem_test():
        client = bigquery.Client()
    
        table_path = "bigquery-public-data.baseball.games_post_wide"
        query_string = f"SELECT * from `{table_path}` LIMIT 4000"
    
        # Use the BigQuery Storage API to speed-up downloads of large tables.
        df = client.query(query_string).to_dataframe(create_bqstorage_client=False)  # or True
    
        print(df.info())
        print(f"Dataframe memory usage {table_path}: {df.memory_usage().sum()}")
    
    
    if __name__ == "__main__":
        run_mem_test()

    Testing procedure

    Install the tools:

    $ pip install -U memory_profiler
    $ pip install -U matplotlib
    

    Run the profiler and visualize the results:

    $ mprof run reproduce/iss_8107/reproduce_8107.py
    $ mprof plot
    

    I compared the current BigQuery client version from master with version 1.12.1 (the latest released version at the time this issue was created).

    Results

    Option create_bqstorage_client=False:

    BigQuery client 1.21.1
    to_dataframe_storage_api_False_BQ_1 12 1

    BigQuery client master
    to_dataframe_storage_api_False_master

    Option create_bqstorage_client=True:

    BigQuery client 1.21.1
    to_dataframe_storage_api_True_BQ_1 12 1

    BigQuery client master
    to_dataframe_storage_api_True_master

    Summary

    There doesn't seem to be much difference when not using the bqstorage client, although the peak memory usage has decreased a bit.

    On the other hand, memory savings are huge when bqstorage client is used. The latter used less than half as much memory in tests.

  6. plamut commented on Jan 22, 2020

    @plamut
    Contributor

    @tswast I suppose the memory consumption improvement is large enough to close this issue?

  7. tswast commented on Jan 23, 2020

    @tswast
    ContributorAuthor

    @tswast I suppose the memory consumption improvement is large enough to close this issue?

    👍

    Let's close now that https://github.com/googleapis/google-cloud-python/issues/10185 is open, which will remove code paths where we have less control over memory usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: bigqueryapi: bigquerystoragetype: feature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions