-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Execute_many optimisation for inserts? #123
Comments
It works only for a simple For example, I can write a DML like: UPDATE table SET value='on' WHERE other_value = $1 and run it like async conn.executemany(sql, [('cat',),('dog',),('pig'),]) just because sometimes it is faster than UPDATE table
SET value='on'
WHERE other_value = 'cat' OR other_value = 'dog' OR other_value = 'pig' With your proposal parsing an SQL is a wasting of a time, that optimisation can be done at the upper level by a user's code or by sort of AR library. PG allows to insert multiple values without impacting MVCC penalty with a static DML query. INSERT INTO tbl(col1, col2 [...])
SELECT val1, val2...
FROM ROWS FROM(
unnest($1::type4col1[]),
unnest($2::type4col2[]) [...]
) AS t(val1, val2 [...]) and give array of values by columns. In your case it would be: col1 = (1, 1, 1, 2,)
col2 = (1, 2, 3, 1,)
async conn.execute(sql, (col1, col2,)) [1] https://www.postgresql.org/docs/9.4/static/sql-select.html |
I think the issue of inserting large numbers of rows will be best served by |
@elprans I mostly agree with you. Moreover the Nevertheless it has some limitations.
As for me the best way to insert new values is to use the P.S.: +1 to support the [1] https://www.postgresql.org/docs/9.6/static/sql-copy.html#AEN77698 |
Please try out 0.11 relase with COPY IN support! |
It seems that
execute_many
would run an INSERT as many times as I have rows to insert. Is there an optimisation available to perform a multi row inserts as part of one INSERT statement?Or would I have to manually create that statement with the desired batch size? Basically a helper would be amazing, ideally for better insert performance
The text was updated successfully, but these errors were encountered: