python postgres can I fetchall() 1 million rows?

后端 未结 5 948
北荒
北荒 2020-12-14 08:42

I am using psycopg2 module in python to read from postgres database, I need to some operation on all rows in a column, that has more than 1 million rows.

5条回答
  •  清歌不尽
    2020-12-14 09:08

    Here is the code to use for simple server side cursor with the speed of fetchmany management.

    The principle is to use named cursor in Psycopg2 and give it a good itersize to load many rows at once like fetchmany would do but with a single loop of for rec in cursor that does an implicit fetchnone().

    With this code I make queries of 150 millions rows from multi-billion rows table within 1 hour and 200 meg ram.

提交回复
热议问题