Skip to content

Use psycopg2 execute_values() instead of execute_batch() #4623

@ydinari

Description

@ydinari

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteersperformancewhere performance can be improved. add "bug" only if it's a performance degradationpostgresqlsql

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions