Batch size one billion: SQLite insert speedups, from the useful to the absurd
voidstar.tech
voidstar.tech
I didn't have the patience of OP though to push it to 1B.
http://smalldatum.blogspot.com/2023/09/trying-out-orioledb.h...
If you're inserting one million rows, even 5 microseconds of parse and planning time per query is five extra seconds on a job that could be done in half a second.
Parsing is linear, planning gets exponential very quickly. It's got to consider data distribution, presence or not of indexes, output ordering, presence of foreign keys, uniqueness, and lots more I can't think of right now.
So, planning is much heavier than parsing (for any non-trivial query).
The table here is unconstrained (from the article "CREATE TABLE integers (value INTEGER);") but suppose that table had a primary key and a foreign key – a simple insertion of a single value into such a table would look trivial but consider what the query plan would look like as it verifies the PK and FK aren't violated. And maybe there are a couple of indexes to be updated as well. And a check constraint or three. Suddenly a simple INSERT of a literal value becomes quite involved under the skin.
(edit: and you can add possible triggers on the table)
CREATE TABLE integers (value INTEGER);Meaning, it's certainly possible to get "the absolute fastest inserts possible" but at the same time, you are impacting both other readers of that table AND writers across the db.
This also gets more messy when you are talking about multiple instances.
When rows/s are maximized SQLite is CPU limited by internal bytecode operations rather than waiting for disk stuff.
create table testInt(num Integer); WITH n(n) AS(SELECT 1 UNION ALL SELECT n+1 FROM n WHERE n < 1000000) insert into testInt select n from n;
On my machine - Mac Mini 3 GHz Dual-Core Intel Core i7 16 GB 1600 MHz DDR3
this takes Run Time: real 0.362 user 0.357236 sys 0.004530