Pandas SQL chunksize

后端 未结 3 592
情话喂你
情话喂你 2021-01-31 19:04

This is more of a question on understanding than programming. I am quite new to Pandas and SQL. I am using pandas to read data from SQL with some specific chunksize. When I run

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 19:29

    When you do not provide a chunksize, the full result of the query is put in a dataframe at once.

    When you do provide a chunksize, the return value of read_sql_query is an iterator of multiple dataframes. This means that you can iterate through this like:

    for df in result:
        print df
    

    and in each step df is a dataframe (not an array!) that holds the data of a part of the query. See the docs on this: http://pandas.pydata.org/pandas-docs/stable/io.html#querying

    To answer your question regarding memory, you have to know that there are two steps in retrieving the data from the database: execute and fetch.
    First the query is executed (result = con.execute()) and then the data are fetched from this result set as a list of tuples (data = result.fetch()). When fetching you can specify how many rows at once you want to fetch. And this is what pandas does when you provide a chunksize.
    But, many database drivers already put all data into memory in the execute step, and not only when fetching the data. So in that regard, it should not matter much for the memory. Apart from the fact the copying of the data into a DataFrame only happens in different steps while iterating with chunksize.

提交回复
热议问题