Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)
The two methods return a list of the returned things of the inquiry, did I miss something here?

Or on the other hand, they have indistinguishable uses undoubtedly?

Are any differences in performance-wise?

1 Answer

0 votes
by (26.4k points)

On the off chance that you are utilizing the default cursor, a MySQLdb.cursors.Cursor, the whole outcome set will be put away on the customer side (for example in a Python list) when the cursor.execute() is finished. 

Even if you utilize,

for row in cursor:

you won't get any decrease in memory impression. The whole outcome set has effectively been put away in a list (See self._rows in MySQLdb/cursors.py).

In case, if you utilize SSCursor or SSDictCursor:

import MySQLdb

import MySQLdb.cursors as cursors

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

Then, the result set will be stored in the mysqld (Server). Now, You can compose

cursor = conn.cursor()

cursor.execute('SELECT * FROM HUGETABLE')

for row in cursor:

    print(row)

also, the rows will be brought individually from the server, in this way not expecting Python to construct a tremendous list of tuples first, and hence saving money on memory. 

Something else, as others have effectively expressed, cursor.fetchall() and list(cursor) are basically something very similar.

Wanna become a Python expert? Come and join the python certification course and get certified.

Related questions

0 votes
1 answer
asked Mar 6, 2021 in Python by laddulakshana (16.4k points)
+3 votes
2 answers
0 votes
1 answer
asked Jul 29, 2019 in Python by Rajesh Malhotra (19.9k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...