psycopg2: insert multiple rows with one query

后端 未结 15 2291
谎友^
谎友^ 2020-11-22 09:11

I need to insert multiple rows with one query (number of rows is not constant), so I need to execute query like this one:

INSERT INTO t (a, b) VALUES (1, 2),         


        
15条回答
  •  盖世英雄少女心
    2020-11-22 09:48

    All of these techniques are called 'Extended Inserts" in Postgres terminology, and as of the 24th of November 2016, it's still a ton faster than psychopg2's executemany() and all the other methods listed in this thread (which i tried before coming to this answer).

    Here's some code which doesnt use cur.mogrify and is nice and simply to get your head around:

    valueSQL = [ '%s', '%s', '%s', ... ] # as many as you have columns.
    sqlrows = []
    rowsPerInsert = 3 # more means faster, but with diminishing returns..
    for row in getSomeData:
            # row == [1, 'a', 'yolo', ... ]
            sqlrows += row
            if ( len(sqlrows)/len(valueSQL) ) % rowsPerInsert == 0:
                    # sqlrows == [ 1, 'a', 'yolo', 2, 'b', 'swag', 3, 'c', 'selfie' ]
                    insertSQL = 'INSERT INTO "twitter" VALUES ' + ','.join(['(' + ','.join(valueSQL) + ')']*rowsPerInsert)
                    cur.execute(insertSQL, sqlrows)
                    con.commit()
                    sqlrows = []
    insertSQL = 'INSERT INTO "twitter" VALUES ' + ','.join(['(' + ','.join(valueSQL) + ')']*len(sqlrows))
    cur.execute(insertSQL, sqlrows)
    con.commit()
    

    But it should be noted that if you can use copy_from(), you should use copy_from ;)

提交回复
热议问题