-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Use psycopg2 execute_values() instead of execute_batch() #4623
Copy link
Copy link
Closed
Labels
PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteersa fix or feature which is appropriate to be implemented by volunteersperformancewhere performance can be improved. add "bug" only if it's a performance degradationwhere performance can be improved. add "bug" only if it's a performance degradationpostgresqlsql
Milestone
Metadata
Metadata
Assignees
Labels
PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteersa fix or feature which is appropriate to be implemented by volunteersperformancewhere performance can be improved. add "bug" only if it's a performance degradationwhere performance can be improved. add "bug" only if it's a performance degradationpostgresqlsql
Hi,
When working with psycopg2 and setting flag use_batch_mode, a call to psycopg2.extras.execute_batch() is used instead of cursor.executemany(). But there's a faster function - psycopg2.extras.execute_values().
execute_batch works by concatenating single insert statements (using ';') and sending them together to the server.
execute_values works by creating a single insert statement with multiple tuples, which is much faster
(for both the page_size parameter determines the number of rows that will be inserted in each call to the database).
In my case I saw a 40% improvement using execute_values().