Python db-api: fetchone vs fetchmany vs fetchall

前端 未结 3 1779
后悔当初
后悔当初 2020-12-07 14:16

I just had a discussion today with some coworkers about python\'s db-api fetchone vs fetchmany vs fetchall.

I\'m sure the use case for each of these is dependent on

3条回答
  •  清歌不尽
    2020-12-07 15:01

    These are implementation specific.

    • fetchall

    Will get all the results from the table. This will work better when size of the table is small. If the table size is bigger, fetchall will fail in those cases.

    Will use most of the memory.

    Will cause some issues will can occur if the queries is done on network.

    • fetchmany

    fetchmany will get only required number of results. You can yield the results and process. Simple Snippet of implementation of fetchmany.

       while True:
        results = cursor.fetchmany(arraysize)
        if not results:
            break
        for result in results:
            yield result
    

提交回复
热议问题