22 ms·
Inserting a billion rows in SQLite under a minute
- quest88 5y agoIf it's a test database, can you simply generate it once, commit it, and use that in all future tests?
- ThePadawan 5y agoThere are some cool ideas in there! I wonder how much I could speed up my test suites (that don't rely on transaction rollbacks) by disabling journalling. Those milliseconds per query add up to seconds per tests and minutes per PR.
- lmz 5y agoI wonder if defining a virtual table (https://www.sqlite.org/vtab.html https://www.sqlite.org/vtab.html) and just doing INSERT INTO t (...) SELECT ... from virtual_table would be any faster.
- rubyfan 5y agoFrom 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
- roberto 5y agoIf you define the virtual table would you even need to do the `INSERT`? Just query the virtual table instead of inserting into `t` and querying `t`.
- bob1029 5y agoYou can go even faster if you can organize your problem such that you simply start from a copy of a template SQLite database each time (it is just a file/byte sequence). We do SQL evaluation for a lot of business logic throughout our product, and we have found that starting from a template database (i.e. one with the schema predefined and canonical values populated) can save a lot of time when working in tight loops.
- jonnycomputer 5y agoAre you suggesting have a template and then updating rows with random values where necessary?
- nezirus 5y agoYou 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 as good as it gets, providing your IO is optimal.
- perlgeek 5y agoI wonder if something like https://paulbradley.org/sqlite-test-data/ https://paulbradley.org/sqlite-test-data/ could be used to generate the test data in sqlite3 directly, and if it would be any faster. (It would likely be faster for other DB engines, because there is network overhead in the communication between the program and the DB; no such things for sqlite).
- mirekrusin 5y agoIf it's single, in memory table, is there really need to use database? Won't language provided data structures suffice?
- jonnycomputer 5y agoIt is a good question, but I'm guessing that the whole reason for creating the database in the first place is to test interactions with the database, in which case you'd have to mock up all the interactions being tested (selects, deletes, joins, etc.).
- einpoklum 5y agoYes, if you want to run non-trivial queries on that data. Although, frankly, SQLite would not be my choice.
- xxs 5y agoI 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.
- adrianN 5y agoIf 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.
- xxs 5y ago>you can =probably= beat SQLite with =a lot of= work, Actually, I am beyond certain. When it's all about the memory no database comes even remotely close to a properly picked datastructures + structure/objects layout. If I need transaction log + persistence, databases have a decent application. In more than 20y, I have never had a case: Yay, I can use relation structures in memory b/c I don't know what I am going to do with the data.
- rmetzler 5y agoIs PRAGMA temp_store = MEMORY the same as putting the file on a TempFS?
- elteto 5y agoMaybe, maybe not. I imagine writing directly to memory will always be faster than writing to a filesystem, even if it’s ultimately backed by RAM. Might be a negligible difference though.
- DemocracyFTW 5y agoI tried using tmpfs to create and exercise a file-based SQLite DB, and it performed significantly worse than a RAM-based DB (opened with an empty string or ':memory:').
- rmetzler 5y agoThank you :)
- siscia 5y agoCreator of RediSQL / zeeSQL (https://zeesql.com/ 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/avinassh/fast-sqlite3-inserts/blob/master/src/bin/basic_batched.rs#L38 https://github.com/avinassh/fast-sqlite3-inserts/blob/master... ;)
- avinassh 5y agoHey all, author here. I didn't expect this to reach front page of HN. I came here to submit and it was here already! I am looking for more ideas to experiment. Here's one idea which someone from another forum gave me: exploring recursive queries and using SQLite random methods to do the insertions. Another crazy idea is to learn about SQLite file format and just write the pages to disk.
- eatonphil 5y agoI am also interested in writing the SQLite or PostgreSQL file format straight to disk as a faster way to do ETL. I'd be curious to hear if anyone has actually tried this. I'd be as curious if you end up trying this too.
- tyingq 5y agoSqlite has a btree module/api, though there's a lot of "TODO:" notes in the document: https://sqlite.org/btreemodule.html https://sqlite.org/btreemodule.html
- avinassh 5y agoTIL, this is great! I think this could make things even easier instead of writing the raw pages
- oldtablet 5y agoI really like sqlite but I'm curious how fast it would be to write the data to parquet files and read it with duckdb. I've done something similar before and in my case duckdb was faster than sqlite.
- avinassh 5y agoyes, I plan to try this next and measure the performance. Others also suggested similar idea to try. > I am also interested in writing the SQLite or PostgreSQL file format straight to disk as a faster way to do ETL. what exactly you are trying to do here?
- deleted 5y ago
- asicsp 5y ago>The larger DB required more than 30 minutes to generate. So I spent 30+ hours to reduce the 30 mins of running time :p Worth it. >I could just run s the script again. Found a typo.
- ericlewis 5y agoCool
- jonnycomputer 5y ago>The generated data would be random with following constraints: The area column would hold six digits area code (any six digits would do, no validation). The age would be any of 5, 10, or 15. The active column is either 0 or 1. That is about 6 million combinations. This is not such a big space that it would be impractical to precompute all possible combinations. I wonder if hashing in to such a precomputed table might help. Might not if the overhead of maintaining the lookup table is too high.
- roman-holovin 5y agoINSERT INTO user (area, age, active) SELECT abs(random()) % 1000000, (abs(random()) % 3 + 1) * 5, abs(random()) % 2 FROM generate_series(1, 100000000, 1) Faster by 10% than fastest author implementation on my machine - 19 seconds against 21 for 'threaded_batched'.
- gfodor 5y agoI have to assume this was rejected as a valid approach, but if not, this whole thread gets 10x more interesting than it already was. I hope the author responds.
- roman-holovin 5y agoWell, even if you just insert zeros instead of random values, it takes 9 seconds on my computer to insert 100M rows, so even that is not a 1B rows per minute. And I think INSERT INTO ... SELECT is the fastest way to bulk insert data into sqlite. Also, I have tried to use carray sqlite feature that allow to share memory with sqlite and use recursive CTE to query it, but it is slower. Though, you can pass values you've generated from Rust instead of using random().
- nojvek 5y agoThis is with in memory database and journaling disabled?
- roman-holovin 5y agoI have used pragmas from the article. Since it is only 100M rows, it takes 1.8 GB on the disk, so I've used tmpfs for this which essentially is a ramdisk. But I have a gen4 pcie nvme SSD - it can reliably write at 4GB/s sequentially, so writing takes a ~500ms for 100M rows, it is not a bottleneck here. random() takes ~half of the insert time. Generating those values with Rust, for example, is faster, but sharing this data with sqlite takes more time than generating it with random(). Maybe implementing custom virtual table in C or Rust like build-in generate_series, but the one that will produce user table fields will be faster, but that is significantly more effort than my query. This query with random() and generate_series executed in sqlite CLI takes whooping 8MB of the RAM, so you don't even have to close all Electron-based applications to run it on a computer with 8GB of RAM.
- mimir 5y agoDatabase optimization posts are always interesting, but it's really hard to do any apples to apples comparison. Your performance is going to depend mostly on your hardware, internal database settings and tunings, and OS level tunings. I'm glad this one included some insight into the SQLite settings disabled, but there's always going to be too many factors to easily compare this to your own setup. For most SQL systems, the fastest way to do inserts is always just going to batched inserts. There's maybe some extra tricks to reduce network costs/optimize batches [0], but at it's core you are still essentially inserting into the table through the normal insert path. You can basically then only try and reduce the amount of work done on the DB side per insert, or optimize your OS for your workload. Some other DB systems (more common in NoSQL) let you actually do real bulk loads [1] where you are writing direct(ish) database files and actually bypassing much of the normal write path. [0] https://dev.mysql.com/doc/refman/5.7/en/insert-optimization.html https://dev.mysql.com/doc/refman/5.7/en/insert-optimization.... [1] https://blog.cloudera.com/how-to-use-hbase-bulk-loading-and-why/ https://blog.cloudera.com/how-to-use-hbase-bulk-loading-and-...
- vajrabum 5y agoOracle, DB2, MySQL, SQL Server, and PostgreSQL all support bulk insert. Two obvious use cases are QA Databases and the L part of ETL which pretty much require it.
- sharken 5y agoFor relational databases in the enterprise with large amounts of data, batched inserts work great. For large deletes it is often better to move the rows that won't be deleted to a new table and rename the table when done. With large updates it is important to look at the query plan and optimize it with good indexes. Batching also works well in this scenario.
- orf 5y agoHow long does it take when using the native CSV import features? https://www.sqlite.org/csv.html https://www.sqlite.org/csv.html
- motoboi 5y agoIt’s fast. But then we need a “1 billion CSV lines” article.
- orf 5y agoWhich I expect is significantly faster to generate. You could probably combine it with a pipe to avoid all disk IO.
- tanin 5y agoI built https://superintendent.app https://superintendent.app and experimented a lot with this feature. I can import 1GB CSV file in 10 seconds on my MacBook. This queries from a virtual table and puts it in actual table
- freeqaz 5y agoThis is what I did the last time I ran into SQLite being slow with inserts. I just shat out a CSV file and then imported it. It was much quicker! (like 1000x faster) This article has some useful notes for me now to try the next time I play with my pet projects. :)
- svdr 5y agoI thought one billion was 1000M (and not 100M)?
- throwaway210222 5y ago1,000,000 million. In old money.
- thanksforfish 5y agoRef: https://en.m.wikipedia.org/wiki/Billion https://en.m.wikipedia.org/wiki/Billion
- deleted 5y ago[deleted]
- kylec 5y agoThis is an important comment. It's not a typo in the article, even the source code only does 100M rows, not 1B. The author definitely does not hit the target of 1B rows in a minute.
- chrisseaton 5y agoThe author doesn’t say a billion is 100 million. They say they’d like to be able to insert a billion, and say they’re able to insert 100 million. It’s not a contradiction.
- minitoar 5y agoOk but that is a rather clickbaity title. The title makes it sound like they are successfully doing that.
- dclowd9901 5y agoI agree. While the title reflects the eventual goal of the effort, the goal has yet to be achieved (and may or may not be achievable at all). I think it’s a bit irresponsible to use a title like that for a post that neglects to have achieved what was described in the title.
- einpoklum 5y ago> Recently, I ran into a situation where I needed a test database with lots of rows and needed it fast. So I did what any programmer would do: wrote a Python script to generate the DB. The author must have a rather low opinion of programmers... generating data with a script is good for 1,000 rows. Maybe 10,000 rows. 100,000 is probably stretching it. Beyond that the slowness will be meaningful. Anyway, if the "test database" data is given in advance, you want an in-memory DBMS which can load data from files. If it isn't - use an in-memory DBMS which supports generated columns, or apply your random-number-generating function on a trivial column. (MonetDB standalone/embedded would be a decent option; maybe the columnar storage mechanism in MariaDB? And of course there are commercial offerings.) > Unfortunately, it was slow. Really slow. So I did what any programmer would do: went down the rabbit hole of learning more about SQLite... That's the wrong approach. SQLite isn't called "lite" for nothing. It's not what you would use for bulk operations on large tables. Read the SQLite FAQ: https://www.sqlite.org/whentouse.html https://www.sqlite.org/whentouse.html it oversells itself a bit, but still says its (only) advantages for data analysis are: "easier to install and use and the resulting database is a single file that can be written to a USB memory stick or emailed."
- geofft 5y agoI suspect these opinions would be less likely to get downvoted if you could back them up with data, specifically: - Assuming you do have a test database in advance that's represented in a file, but for some reason not a SQLite file or other database, how long does it take to load 100M rows in a database of your choice, and how does that compare to the author's results? - The author does not in fact have a test database in advance, and is generating random data, which I think is what you're saying by "apply your random-number-generating function on a trivial column" - how long would your approach take to generate 100M rows? - If you generated a database of about the same size in a database of your choice, how do bulk operations on large tables perform in comparison to SQLite? (My understanding is that it's "lite" because it's not client/server, not because its performance is poor.) I don't think any of these experiments would be terribly hard to run.
- arusahni 5y ago[ @dang, please delete if/when you get the chance, thank you ]
- kebman 5y agoMini summary: Do not use this in production. xD
- MobiusHorizons 5y agoI would recommend looking into dtrace or other observability tools to figure out what to optimize next. So far you have basically had to guess where the slowness is and optimize that. Lastly, how long does it take to make a copy of an already prepared 1b row SQLite file?that seems easier than generating a new one.
- bilekas 5y ago> Threads / async may not be faster always This is actually a little bit mind blowing for me. I'm gonna go and play with this. But what a cool read!
- mongol 5y ago1 billion rows, 8GB RAM. I think there will be some limitation from available RAM as well.
- pachico 5y agoI've been dealing with lots of data and SQLite and my outtakes are: - language has very little to do since the bottleneck will most likely be the way you insert data - indeed prepared statements are useful but the performance didn't change much when I did long transactions and commit every certain amount of thousand of rows - having lots of rows in your table is good but certain queries, like aggregation over many rows, are not what SQLite is great about. - ClickHouse can easily ingest that and more in a laptop without even any scripting language.
- mpweiher 5y agoInteresting! I was actually just working on SQLite speed for Objective-S (http://objective.st http://objective.st), partly as a driver for getting some of the more glaring inefficiencies out. Using a "to do list" schema, I currently get the 100M rows out in 56 seconds, which is around half the speed of the Rust example given here, but 3 times the speed of PyPy and almost 10x faster than Python. This is from an interpreted script that not only does the inserts and creates the objects to insert in the first place, but also defines the actual class. The lower-level code is written in Objective-C, like the rest of Objective-S. Class definition: class Task { var <int> id. var <bool> done. var <NSString> title. -description { "<Task: title: {this:title} done: {this:done}>". } +sqlForCreate { '( [id] INTEGER PRIMARY KEY, [title] NVARCHAR(220) NOT NULL, [done] INTEGER );'. } }. Code to insert a computed array of tasks 10 times: 1 to:10 do: { this:tasksTable insert:taskList. }.
- IfOnlyYouKnew 5y agoI ran this and got the following results: /fast-sqlite3-inserts (master)> time make busy-rust Sun Jul 18 17:04:59 UTC 2021 [RUST] busy.rs (100_000_000) iterations real 0m9.816s user 0m9.380s sys 0m0.433s ________________________________________________________ Executed in 9.92 secs fish external usr time 9.43 secs 0.20 millis 9.43 secs sys time 0.47 secs 1.07 millis 0.47 secs fast-sqlite3-inserts (master)> time make busy-rust-thread Sun Jul 18 17:04:48 UTC 2021 [RUST] threaded_busy.rs (100_000_000) iterations real 0m2.104s user 0m13.640s sys 0m0.724s ________________________________________________________ Executed in 2.33 secs fish external usr time 13.68 secs 0.20 millis 13.68 secs sys time 0.78 secs 1.18 millis 0.78 secs I'm probably doing something wrong. Or I'm getting the pace needed for the billion? This is on a M1 MacBook Air.
- nzealand 5y agoIf you insert 4 rows into a table, and join it on itself without any conditional criteria, it will result in a cartesian product of 16 rows (Select * from a, a) Do that cartesian join three more times, and you have over 4 billion results. Then you simply need to use the random function in conjunction with division, rounding and case statements to get the desired random numbers.
- elephantum 5y agoI bet, that this is mostly random.randint benchmark and not SQLite. Also interesting whether batched numpy version would compare better to Rust.
- elephantum 5y agoCorrection: random.randint + string interpolation
- pksebben 5y agoI'm pretty new to this sort of optimization stuff, so forgive me if this is ridiculous for any reason, but I'm wondering about two things reading this: 1: is it useful / feasible to set a sort of "lower bound" to these optimizations by profiling the raw write time on a set of a certain size? 2. assuming that writes are ultimately the limiting factor, could you gain performance by calculating the minimum batch size as it's write time intersects in-memory calculation, and as soon as you hit that size, pop a thread to write that batch to disk - then return some async signal when the write is done to trigger the next batch of writes? is it possible to "stitch together" these batches post-writes? edit: mobile formatting is le suck
- Seattle3503 5y agoI've used the other pragma, but does PRAGMA locking_mode = EXCLUSIVE; improve performance much?
- ajnin 5y agoHere's my solution : generate the 1B records .sqlite file once beforehand, then before running each test, copy it over the test DB file ;)
- notatelloshill 5y agoan in memory database is not a database ... you might as well just create billion object array and have your java process constantly running to have that data accessible.
- A4ukYkq6ILl 5y ago7VKs3Dsn
- A4ukYkq6ILl 5y agohttps://www.baidu.com https://www.baidu.com
- polyrand 5y agoReally good article, I’ve experimented with this kind of workloads in SQLite a few times and found it insightful. One note though, using: pragma temp_store = memory; only affects temporary tables and indices, not the main database itself[0] [0] https://sqlite.org/pragma.html#pragma_temp_store https://sqlite.org/pragma.html#pragma_temp_store
- Groxx 5y agoYep - I discovered most of this a while back when I needed to do a bunch of exploring of ~200m rows of data (like 50GB). I chucked it into SQLite and was done with everything in a day, it was absolutely wonderful. This post is a pretty good summary of what to change and why. Prepared statements should be considered the norm for SQLite - they have pretty major performance benefits, and any decent ORM or query engine can probably do it for you implicitly or with a single flag, so it's practically free for many applications. I'm always surprised how often I see applications or benchmarks or "how to be fast with SQLite" not using them, so I'm definitely glad to see them covered here :)
- chovybizzass 5y agoNeed it done in Deno, fren.
- Egret 5y agoHere is an example using Tcl to create an SQL script using recursive common table expressions: # createSQL.tcl set increment 100000 set loop [expr {100000000 / $increment}] puts {PRAGMA journal_mode = OFF; PRAGMA synchronous = 0; PRAGMA cache_size = 1000000; PRAGMA locking_mode = EXCLUSIVE; PRAGMA temp_store = MEMORY; CREATE TABLE user( pk INTEGER PRIMARY KEY, area INTEGER, age INTEGER, active INTEGER );} for {set i 0} {$i < $loop} {incr i} { puts " WITH RECURSIVE tmp(pk,area,age,active) AS ( SELECT [expr {$i * $increment}], 500001, 5, 0 UNION ALL SELECT pk+1, 10000*(abs(random())%9+1)+abs(random())%100000, 5*(abs(random()%3)+1), abs(random()%2) FROM tmp WHERE pk < [expr {($i+1)*$increment-1}] ) INSERT INTO user SELECT * FROM tmp;" } #EOF Core 2 Duo 2.53 GHz, 4GB RAM, SSD: $ time -p tclsh createsql.tcl | sqlite3 test.db off exclusive real 213.37 user 208.10 sys 4.58
- stevage 5y ago>Inserting One Billion Rows in SQLite Under A Minute But the results given are all for inserting 0.1B rows, not 1B rows. What are the full results?
- discordance 5y agoMultiply the reported number by 10
- deleted 5y ago[deleted]
- wuxb 5y agoJust tried to insert 100M KV pairs of the same data format in a KV store. It takes about 26s on a Xeon Silver 4210, including fully writing down the data to the filesystem/SSD. The source code can be found here: https://github.com/wuxb45/remixdb/blob/master/i100m.c https://github.com/wuxb45/remixdb/blob/master/i100m.c $ make i100m.out libremixdb.so $ numactl -N 0 ./i100m.out insert time without sync: 24.865s, with sync: 25.801s
- malkia 5y agoPRAGMA page_size 65536
- malkia 5y agoBut ensure this is actually in effect (e.g. do vacuum, etc.)
- lallysingh 5y ago.. or the throughput of SQLite acting in pure fwrite(3) mode with a big buffer.
- known 5y agoIsn't writing to a MMAP file faster?
- nojvek 5y agoFor the table you have 3 integer rows and a char(6) row. Taking integer as 32 bit that is a total of 18 bytes of data per row. At billion rows, that is 18GB of data. With some overhead for storing page info, let’s call it 20GB flat. A modern SSD can deliver ~500MB/s write speed. That means writing 20GB of data can be done in 40 seconds. Therefore a billion rows in a minute is quite plausible. At-least not bottlenecked by disk speed (if we can saturate disk).
- sharken 5y agoIt's a good insight to know what the hardware is capable of. The SSD in the authors machine can do 1300MB/s and the latest M1 model can do 2100MB/s. For current gen SSDs with PCIe 4.0, that number increases to 6600MB/s for the Sabrent Rocket 4 Plus. That would mean just around ~5 seconds for writing to disk. https://www.pcgamer.com/best-pcie-4-ssd-for-gaming/ https://www.pcgamer.com/best-pcie-4-ssd-for-gaming/
- patrec 5y ago> A modern SSD can deliver ~500MB/s write speed. More like ~5GB/s.
- teekert 5y agoWebapps (or webapp frameworks) like Nextcloud and Django, come, by default, with SQLite. That makes it easy to start, but they all warn you: When you get serious, get a real DB like MariaDB or PostgreSQL... And with stuff like this I wonder, is this really necessary? I really like SQLite, it's well supported in Python, backup-restore is very simple. What is the real difference (probably application depended) between SQLite and a "real" DB engine?
- tcbasche 5y agoI guess the idea is you can scale the app independently of the DB. How do you scale out a single file? (Genuinely curious)
- teekert 5y agoHmm, never thought of this :) I come more from a selfhosting angle. And my django website is prototype only so far… But it is a point I never considered so thanx. I wonder at what scales this starts to count (in human readable units ;))
- discordance 5y agoThis spells out where it’s appropriate and where it isn’t: https://www.sqlite.org/whentouse.html https://www.sqlite.org/whentouse.html
- teekert 5y agoThanx!