Search code examples
sqliteexecutemany

How does executemany() work


I have been using c++ and work with sqlite. In python, I have an executemany operation in the library but the c++ library I am using does not have that operation.

I was wondering how the executemany operation optimizes queries to make them faster.

I was looking at the sqlite c/c++ api and saw that there were two commands, sqlite3_reset and sqlite3_clear_bindings, that can be used to clear and reuse prepared statements.

Is this what python does to batch and speedup executemany queries (at least for inserts)? Thanks for your time.


Solution

  • executemany just binds the parameters, executes the statements, and calls sqlite3_reset, in a loop.

    Python does not give you direct access to the statement after it has been prepared, so this is the only way to reuse it. However, SQLite does not take much time for preparing statements, so this is unlikely to have much of an effect on performance.

    The most important thing for performance is to batch statements in a transaction; Python tries to be clever and to do this automatically (independently from executemany).