Live data from Hacker News

Inserting a billion rows in SQLite under a minute

avi.im

21–30 of 164 posts

Re: Inserting a billion rows in SQLite under a minute

#21
post #10

Creator of RediSQL / zeeSQL ( https://zeesql.com/ ) Insertion performance on a single table are very very hard to optimize. A single process looping it is your best bet. I would just increase the batch size, which is the most influent factor. Then another point... When you do batches, you do BEGIN TRANSACTION; for i in range(1, 50): execute_stmt COMMIT; You do not create a long list of parameters. https://github.com/…

Even though merging strings seems faster...

Re: Inserting a billion rows in SQLite under a minute

#22
post #11

Earlier quoted context omitted.

Are you suggesting have a template and then updating rows with random values where necessary?

You just do raw byte copies from sample DB, no SQL or "inserts" or anything similar. Imagine test database consists of 3 parts (all raw bytes) ### PROLOGUE ### Sample row ### EPILOGUE You copy & write prologue, write 1B sample raws (can optimize this at will, large writes, etc) Copy & write epilogue and fsync the data. You probably need to modify some metadata, but that should be a few writes at most. That should be…

Do you mean crafting all the various database page btree structures and entries yourself? I'd be concerned about subtle bugs.

Re: Inserting a billion rows in SQLite under a minute

#23
post #3

I wonder if defining a virtual table ( https://www.sqlite.org/vtab.html ) and just doing INSERT INTO t (...) SELECT ... from virtual_table would be any faster.

From my armchair here I thought something similar, I think I heard 1) that inserting into the same table has some speed limitations and 2) one of the authors ideas was to spin up 4 processes…

I wonder if you could insert into 10 different tables from 10 threads or processes then

insert into single_table (…) select (…) union.

No idea if insert select does the trick or not but you’re almost at the point of partitioning here. If the application called for it you could do some sort of poor mans partition in order to write all the data and then push some complexity into the read.

I also wondered what if any impact the ID primary key had on inserting at the level of frequency.

/armchair

Re: Inserting a billion rows in SQLite under a minute

#24

If it's single, in memory table, is there really need to use database? Won't language provided data structures suffice?

Yes, if you want to run non-trivial queries on that data. Although, frankly, SQLite would not be my choice.

I can't think of a single case where in-memory database is a good option, aside playing w/ SQL.

Whatever queries that might be a hashmap/tree/skiplist, etc. would be a lot better.

Re: Inserting a billion rows in SQLite under a minute

#25
post #21
post #10

Creator of RediSQL / zeeSQL ( https://zeesql.com/ ) Insertion performance on a single table are very very hard to optimize. A single process looping it is your best bet. I would just increase the batch size, which is the most influent factor. Then another point... When you do batches, you do BEGIN TRANSACTION; for i in range(1, 50): execute_stmt COMMIT; You do not create a long list of parameters. https://github.com/…

Even though merging strings seems faster...

[deleted]

Re: Inserting a billion rows in SQLite under a minute

#27
post #24

Earlier quoted context omitted.

Yes, if you want to run non-trivial queries on that data. Although, frankly, SQLite would not be my choice.

I can't think of a single case where in-memory database is a good option, aside playing w/ SQL. Whatever queries that might be a hashmap/tree/skiplist, etc. would be a lot better.

If the set of queries you want to run is fixed you can probably beat SQLite with a lot of work, if the queries are dynamic I doubt that you can do much better than SQLite without herculean efforts. A in-memory database is thus a good option if you either don't care too much about the runtime of your queries and you want to save a bunch of development time, or if you don't know enough about your workload to be able to beat a general purpose solution.

Re: Inserting a billion rows in SQLite under a minute

#30
post #24

Earlier quoted context omitted.

Yes, if you want to run non-trivial queries on that data. Although, frankly, SQLite would not be my choice.

I can't think of a single case where in-memory database is a good option, aside playing w/ SQL. Whatever queries that might be a hashmap/tree/skiplist, etc. would be a lot better.

For the case where I want the ability to run one-off analytics queries, having access to the usual set of join/filter/summarize/etc. operations is a lot more convenient and less error-prone than having to manually write them. But dplyr [1] is my go-to rather than SQLite personally for in-memory data sizes.

[1] https://dplyr.tidyverse.org/

Post reply on HN