cursor.fetchall() vs list(cursor) in Python

后端 未结 5 1092
面向向阳花
面向向阳花 2020-11-30 01:59

Both methods return a list of the returned items of the query, did I miss something here?
Or they have identical usages indeed?
Any differences performance-wise?

5条回答
  •  余生分开走
    2020-11-30 02:14

    If you are using the default cursor, a MySQLdb.cursors.Cursor, the entire result set will be stored on the client side (i.e. in a Python list) by the time the cursor.execute() is completed.

    Therefore, even if you use

    for row in cursor:
    

    you will not be getting any reduction in memory footprint. The entire result set has already been stored in a list (See self._rows in MySQLdb/cursors.py).

    However, if you use an SSCursor or SSDictCursor:

    import MySQLdb
    import MySQLdb.cursors as cursors
    
    conn = MySQLdb.connect(..., cursorclass=cursors.SSCursor)
    

    then the result set is stored in the server, mysqld. Now you can write

    cursor = conn.cursor()
    cursor.execute('SELECT * FROM HUGETABLE')
    for row in cursor:
        print(row)
    

    and the rows will be fetched one-by-one from the server, thus not requiring Python to build a huge list of tuples first, and thus saving on memory.

    Otherwise, as others have already stated, cursor.fetchall() and list(cursor) are essentially the same.

提交回复
热议问题