<p>There are several ways to iterate over a result set. What are the tradeoff of each?</p>

[{'answer_id': 595, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 7, 'selected': True, 'text': "<p>The canonical way is to use the built-in cursor iterator.</p>\n\n<pre><code>curs.execute('select * from people')\nfor row in curs:\n    print row\n</code></pre>\n\n<hr>\n\n<p>You can use <code>fetchall()</code> to get all rows at once.</p>\n\n<pre><code>for row in curs.fetchall():\n    print row\n</code></pre>\n\n<p>It can be convenient to use this to create a Python list containing the values returned:</p>\n\n<pre><code>curs.execute('select first_name from people')\nnames = [row[0] for row in curs.fetchall()]\n</code></pre>\n\n<p>This can be useful for smaller result sets, but can have bad side effects if the result set is large.</p>\n\n<ul>\n<li><p>You have to wait for the entire result set to be returned to\nyour client process.</p></li>\n<li><p>You may eat up a lot of memory in your client to hold\nthe built-up list.</p></li>\n<li><p>It may take a while for Python to construct and deconstruct the\nlist which you are going to immediately discard anyways.</p></li>\n</ul>\n\n<hr>\n\n<p>If you know there's a single row being returned in the result set you can call <code>fetchone()</code> to get the single row.</p>\n\n<pre><code>curs.execute('select max(x) from t')\nmaxValue = curs.fetchone()[0]\n</code></pre>\n\n<hr>\n\n<p>Finally, you can loop over the result set fetching one row at a time.  In general, there's no particular advantage in doing this over using the iterator.</p>\n\n<pre><code>row = curs.fetchone()\nwhile row:\n    print row\n    row = curs.fetchone()\n</code></pre>\n"}, {'answer_id': 25213, 'author': 'Anders Eurenius', 'author_id': 1421, 'author_profile': 'https://Stackoverflow.com/users/1421', 'pm_score': 3, 'selected': False, 'text': '<p>There\'s also the way <code>psyco-pg</code> seems to do it... From what I gather, it seems to create dictionary-like row-proxies to map key lookup into the memory block returned by the query. In that case, fetching the whole answer and working with a similar proxy-factory over the rows seems like useful idea. Come to think of it though, it feels more like Lua than Python.</p>\n\n<p>Also, this should be applicable to all <a href="http://www.python.org/dev/peps/pep-0249/" rel="noreferrer">PEP-249 DBAPI2.0</a> interfaces, not just Oracle, or did you mean just <em>fastest</em> using <em>Oracle</em>?</p>\n'}, {'answer_id': 125140, 'author': 'Aurelio Martin Massoni', 'author_id': 20037, 'author_profile': 'https://Stackoverflow.com/users/20037', 'pm_score': 5, 'selected': False, 'text': "<p>My preferred way is the cursor iterator, but setting first the arraysize property of the cursor. </p>\n\n<pre><code>curs.execute('select * from people')\ncurs.arraysize = 256\nfor row in curs:\n    print row\n</code></pre>\n\n<p>In this example, cx_Oracle will fetch rows from Oracle 256 rows at a time, reducing the number of network round trips that need to be performed</p>\n"}]
['https://Stackoverflow.com/questions/594', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/116/']