News for hackers. Stuff that matters.
Re:Batch size one billion: SQLite insert speedups, from the ... by klaussilveira on Tuesday September 26, 2023 @11:01AM (#37660399)
That's a fantastic analysis. I wonder how PostgreSQL performs?
Re:Batch size one billion: SQLite insert speedups, from the ... by eatonphil on Tuesday September 26, 2023 @11:05AM (#37660459)
I've done some comparisons for inserts of various data sizes on mariadb, postgres, and sqlite without indexes.

I didn't have the patience of OP though to push it to 1B.

https://github.com/eatonphil/databases-intuition

Re:Batch size one billion: SQLite insert speedups, from the ... by riku_iki on Tuesday September 26, 2023 @04:23PM (#37665320)
looks like PGSQL 2x slower for uploading.
Re:Batch size one billion: SQLite insert speedups, from the ... by eatonphil on Tuesday September 26, 2023 @04:26PM (#37665362)
Looks like I'm not alone. But I don't, and don't think Mark does (he's a MySQL guy I believe), claim to be a Postgres expert.

http://smalldatum.blogspot.com/2023/09/trying-out-orioledb.h...

Re:Batch size one billion: SQLite insert speedups, from the ... by voidstarcpp on Tuesday September 26, 2023 @12:35PM (#37661841)
PostgreSQL supports heap tables, which should blow away SQLite's mandatory clustered index tables in an unindexed insert test.
Re:Batch size one billion: SQLite insert speedups, from the ... by chmaynard on Tuesday September 26, 2023 @01:39PM (#37662933)
In SQLite, would declaring the table without a rowid obviate the index? For example:

    CREATE TABLE IF NOT EXISTS integers
    (
       value INTEGER NOT NULL, 
    )
    WITHOUT ROWID;
Re:Batch size one billion: SQLite insert speedups, from the ... by cogman10 on Tuesday September 26, 2023 @11:48AM (#37661104)
One big caveat to this sort of test. With DBs you are very often trading throughput for latency.

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.

Re:Batch size one billion: SQLite insert speedups, from the ... by voidstarcpp on Tuesday September 26, 2023 @12:34PM (#37661828)
With SQLite by default nobody else can even read the database while it's being written, so your comment would be better directed to a conventional database server.
Re:Batch size one billion: SQLite insert speedups, from the ... by argiopetech on Tuesday September 26, 2023 @12:39PM (#37661881)
Note that the default can be changed with a PRAGMA at compile time to Write-Ahead Logging (WAL) which is both faster and allows reading during writes.

[0] https://www.sqlite.org/wal.html

Re:Batch size one billion: SQLite insert speedups, from the ... by voidstarcpp on Tuesday September 26, 2023 @12:52PM (#37662141)
In my limited experiments WAL wasn't faster for high-row-speed writes.

When rows/s are maximized SQLite is CPU limited by internal bytecode operations rather than waiting for disk stuff.

Re:Batch size one billion: SQLite insert speedups, from the ... by argiopetech on Tuesday September 26, 2023 @03:50PM (#37664936)
Too late to edit: This should have said "at run time", as it's set by making a relatively normal (can't be inside a transaction) SQL query.
Re:Batch size one billion: SQLite insert speedups, from the ... by RedShift1 on Tuesday September 26, 2023 @01:21PM (#37662660)
If you enable the WAL, reading while writing is possible. IMO it should be the default.
Re:Batch size one billion: SQLite insert speedups, from the ... by LVB on Tuesday September 26, 2023 @12:13PM (#37661486)
The improvements from using a transaction are familiar to me and make intuitive sense. But can someone explain why using a prepared statement results in a roughly 5x improvement? Parsing the very simple SQL doesn't seem like it would account for much time, so is the extra time spent redoing query planning or something else?
Re:Batch size one billion: SQLite insert speedups, from the ... by voidstarcpp on Tuesday September 26, 2023 @12:33PM (#37661799)
>Parsing the very simple SQL doesn't seem like it would account for much time, so is the extra time spent redoing query planning or something else?

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.

Re:Batch size one billion: SQLite insert speedups, from the ... by hot_gril on Tuesday September 26, 2023 @01:05PM (#37662389)
I'm curious which takes longer, parsing or planning.
Re:Batch size one billion: SQLite insert speedups, from the ... by _a_a_a_ on Tuesday September 26, 2023 @01:13PM (#37662531)
Depends on the complexity of the planning, but... typically planning (in mssql, am not familiar with sqlite).

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

Re:Batch size one billion: SQLite insert speedups, from the ... by hot_gril on Tuesday September 26, 2023 @01:23PM (#37662704)
That's what I expect, but for these very simple batch insert queries, it's hard to guess which takes longer.
Re:Batch size one billion: SQLite insert speedups, from the ... by _a_a_a_ on Tuesday September 26, 2023 @01:36PM (#37662887)
A very simple statement may not be as simple as it looks to execute.

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.

Re:Batch size one billion: SQLite insert speedups, from the ... by hot_gril on Tuesday September 26, 2023 @04:36PM (#37665487)
Right, but in this particular example, the table is just

  CREATE TABLE integers (value INTEGER);
Re:Batch size one billion: SQLite insert speedups, from the ... by _a_a_a_ on Tuesday September 26, 2023 @04:52PM (#37665657)
I said exactly that.
Re:Batch size one billion: SQLite insert speedups, from the ... by hot_gril on Tuesday September 26, 2023 @05:08PM (#37665836)
My bad, missed the first part.
Re:Batch size one billion: SQLite insert speedups, from the ... by _a_a_a_ on Tuesday September 26, 2023 @05:37PM (#37666121)
Happen, happen, we've all done it. NP
Re:Batch size one billion: SQLite insert speedups, from the ... by ako on Tuesday September 26, 2023 @01:57PM (#37663251)
One alternative way I’ve been testing recently: load the entire csv file into a single row column in Postgres, and the use a view to parse it into lines and values. Then cache the result with a materialized view. 16mb, 500k rows (formula 1 laptimes) takes only a few seconds to load and map to materialized view records.
Re:Batch size one billion: SQLite insert speedups, from the ... by daza on Tuesday September 26, 2023 @04:07PM (#37665145)
What do you mean by "row column in Postgres"? It's confusing. Could you please clarify that?
Re:Batch size one billion: SQLite insert speedups, from the ... by Linosaurus on Tuesday September 26, 2023 @04:25PM (#37665345)
Sounds like the initial table has just one row and one column, containing the entire 16mb csv string.
Re:Batch size one billion: SQLite insert speedups, from the ... by ako on Tuesday September 26, 2023 @05:13PM (#37665891)
Correct, at least one row for every time you load a csv file.
Re:Batch size one billion: SQLite insert speedups, from the ... by mike_r_parsons on Tuesday September 26, 2023 @04:18PM (#37665276)
How does this compare to the following?

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

Re:Batch size one billion: SQLite insert speedups, from the ... by riku_iki on Tuesday September 26, 2023 @04:20PM (#37665294)
you test just 1M record not 1B?..