executemany (one transaction) is ~1.25× faster than execute() per row
Batching the calls on the Python side is ~25% faster even with a single commit in both cases — a one-line change.
Result
| Run | executemany (one transaction) | execute() per row | Ratio |
|---|---|---|---|
| Run 1 | 15.737 | 19.655 | 0.80× |
| Run 2 | 14.310 | 17.495 | 0.82× |
| Run 3 | 14.122 | 18.150 | 0.78× |
Spread over 3 runs: executemany (one transaction) CV 5.0% / execute() per row CV 5.0% / Ratio CV 2.0%
The statement
con.executemany('INSERT INTO items VALUES (?,?,?,?,?)', rows); con.commit() for r in rows: con.execute('INSERT INTO items VALUES (?,?,?,?,?)', r)
con.commit() Conditions
5,000 rows inserted into an 8,000-row table, no indexes, `synchronous=NORMAL`.
Where this number does not apply
With very large rows, or when you must commit mid-way, the gap narrows.
Reproduce
cd bench && python sqbench.py --samples 9 --out ../data/run.json