Psycopg2 uses up memory on large select query

前端 未结 2 1622
春和景丽
春和景丽 2020-12-15 06:14

I am using psycopg2 to query a Postgresql database and trying to process all rows from a table with about 380M rows. There are only 3 columns (id1, id2, count) all of type i

相关标签:
2条回答
  • 2020-12-15 06:29

    You can use server side cursors.

    cur = conn.cursor('cursor-name') # server side cursor
    cur.itersize = 10000 # how much records to buffer on a client
    cur.execute("SELECT * FROM mytable;")
    
    0 讨论(0)
  • 2020-12-15 06:35

    Another way to use server side cursors:

    with psycopg2.connect(database_connection_string) as conn:
        with conn.cursor(name='name_of_cursor') as cursor:
    
            cursor.itersize = 20000
    
            query = "SELECT * FROM ..."
            cursor.execute(query)
    
            for row in cursor:
                # process row 
    

    Psycopg2 will fetch itersize rows to the client at a time. Once the for loop exhausts that batch, it will fetch the next one.

    0 讨论(0)
提交回复
热议问题