Re: [psycopg] speed concerns with executemany() - Mailing list psycopg

From Aryeh Leib Taurog
Subject Re: [psycopg] speed concerns with executemany()
Date
Msg-id 20170130221450.GB7081@deb76.aryehleib.com
Whole thread Raw
In response to Re: [psycopg] speed concerns with executemany()  (Daniele Varrazzo <daniele.varrazzo@gmail.com>)
List psycopg
On Mon, Jan 30, 2017 at 11:18:36AM +0000, Daniele Varrazzo wrote:
> 3) a single insert with many params. Plays well with PQexecParams
>    but would need some form of generation by the client
> - insert into table values (...), (...), (...);
>
> While the proposed executemany is a nice low hanging fruit it will
> break on PQexecParams and it's far from being optimal anyway. Wonder
> if there is a way to help users at least to have 3 without bothering
> with mogrify (due to break too with the PQexecParams switch).

Supporting a general case would surely require parsing the sql
statement to some extent, but a simple insert could be done quite
easily, particularly if you change the call signature so the caller
does your work for you:


def insert_batch(cur, sql, template, args):
    argslist = list(args)
    sql_full = sql + ','.join([template] * len(argslist))
    cur.execute(sql_full, reduce(operator.add, argslist))

insert_batch(cur, "insert into testmany (num, data) values ", "(%s, %s)", data)


psycopg by date:

Previous
From: Aryeh Leib Taurog
Date:
Subject: Re: [psycopg] speed concerns with executemany()
Next
From: Daniele Varrazzo
Date:
Subject: Re: [psycopg] speed concerns with executemany()