How to efficiently UPDATE a column in a large PostgreSQL table using Python / psycopg2?

后端 未结 1 1502
被撕碎了的回忆
被撕碎了的回忆 2021-01-21 20:51

I have a large table with ca. 10 million rows in PostgreSQL 9.4 database. It looks somewhat like this:

gid | number1 | random | result | ...
 1  |    2    |  NUL         


        
1条回答
  •  抹茶落季
    2021-01-21 21:23

    unnest the data to make it all at once:

    def create_random(i):
        return random() * i
    
    curs.execute("select gid, number from t;")
    data = curs.fetchall()
    
    results = []
    for i in data:
        results.append((create_random(i[1]), i[0]))
    
    curs.execute("""
        update t
        set
            rnd = s.rnd,
            result = number * s.rnd
        from unnest(%s) s(rnd numeric, gid integer)
        where t.gid = s.gid;
    """, (results,))
    
    con.commit()
    

    Table t:

    create table t (
        gid integer,
        number integer,
        rnd float,
        result float
    );
    

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